-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement basic scene tools * Fix need to ensure the path exists before crating the file * Add test for modifying build scenes * Trigger workflow for prs
- Loading branch information
Showing
8 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
name: CI | ||
|
||
on: | ||
pull_request: | ||
branches: [ main, development ] | ||
push: | ||
branches: [ main, development ] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,5 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
.taskkey |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) Dinomite. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
import tl = require('azure-pipelines-task-lib/task'); | ||
import path = require('path'); | ||
import fs from 'fs'; | ||
|
||
export class UnitySceneTools { | ||
|
||
private static readonly unityProjectSettingsFolder = 'ProjectSettings'; | ||
private static readonly unityEditorBuildSettingsFile = 'EditorBuildSettings.asset'; | ||
|
||
/** | ||
* Creates a new empty scene asset file at a given path. | ||
* | ||
* @param projectPath The root path of the Unity project. | ||
* @param scenePath The path of the scene within the project, relative to the project path. | ||
* @param addToBuildSettings If set, the scene is also added to the editor build scenes list. | ||
*/ | ||
public static createSceneAt(projectPath: string, scenePath: string, addToBuildSettings: boolean): Error | null | undefined { | ||
try { | ||
const sceneFilePath = path.join(projectPath, scenePath); | ||
const sceneMetaFilePath = path.join(projectPath, `${scenePath}.meta`) | ||
const sceneGuid = crypto.randomUUID().replace(/-/g, ''); | ||
|
||
tl.mkdirP(path.dirname(sceneFilePath)); | ||
tl.writeFile(sceneFilePath, ''); | ||
tl.writeFile(sceneMetaFilePath, `fileFormatVersion: 2\nguid: ${sceneGuid}`) | ||
|
||
if (addToBuildSettings) { | ||
UnitySceneTools.addSceneToBuildSettings(projectPath, true, scenePath, sceneGuid); | ||
} | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
return e; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Adds a scene configuration to a project's build scenes configuration list. | ||
* | ||
* @param projectPath The root path of the Unity project. | ||
* @param sceneEnabled Should the scene be added as enabled? | ||
* @param scenePath The path of the scene within the project, relative to the project path. | ||
* @param sceneGuid The GUID of the scene asset. | ||
*/ | ||
public static addSceneToBuildSettings(projectPath: string, sceneEnabled: boolean, scenePath: string, sceneGuid: string): Error | null | undefined { | ||
try { | ||
const editorBuildSettingsFilePath = path.join(projectPath, UnitySceneTools.unityProjectSettingsFolder, UnitySceneTools.unityEditorBuildSettingsFile); | ||
const editorBuildSettingsFileContent = fs.readFileSync(editorBuildSettingsFilePath, { encoding: 'utf8' }); | ||
|
||
const scenesRegex = /(m_Scenes:\s*\[\])/; | ||
const replacement = `m_Scenes:\n - enabled: ${sceneEnabled ? 1 : 0}\n path: ${scenePath}\n guid: ${sceneGuid}`; | ||
const updatedContent = editorBuildSettingsFileContent.replace(scenesRegex, replacement); | ||
|
||
fs.writeFileSync(editorBuildSettingsFilePath, updatedContent, 'utf-8'); | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
return e; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
%YAML 1.1 | ||
%TAG !u! tag:unity3d.com,2011: | ||
--- !u!1045 &1 | ||
EditorBuildSettings: | ||
m_ObjectHideFlags: 0 | ||
serializedVersion: 2 | ||
m_Scenes: [] | ||
m_configObjects: {} | ||
m_UseUCBPForAssetBundles: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Dinomite. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
import 'mocha'; | ||
import { expect } from 'chai'; | ||
import { UnitySceneTools } from '../dist'; | ||
|
||
describe('UnitySceneTools', | ||
() => { | ||
it('Create new empty scene', () => { | ||
const error = UnitySceneTools.createSceneAt(__dirname, 'Assets/DummyDinoScene.unity', false); | ||
expect(!error); | ||
}); | ||
|
||
it('Add scene to editor build configuration', () => { | ||
const error = UnitySceneTools.addSceneToBuildSettings(__dirname, true, 'Assets/DummyDinoScene.unity', 'testguid'); | ||
expect(!error); | ||
}); | ||
}); |