Skip to content

Commit

Permalink
Add UnitySceneTools utility (#15)
Browse files Browse the repository at this point in the history
* 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
FejZa authored Dec 11, 2024
1 parent 085a21d commit 00e5b3b
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
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 ]

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ dist

# TernJS port file
.tern-port

.taskkey
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.12]

### Added

- Added scene utility to create a new empty scene in a project
- Added scene utility to add a scene to a project's build scene configuration file

## [1.0.11]

### Fixed
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export * from './lib/unity-tool-runner';
export * from './lib/unity-path-tools';
export * from './lib/unity-version-tools';
export * from './lib/unity-package-manager-tools';
export * from './lib/unity-scene-tools';
export * from './lib/utilities';
export * from './lib/models';
64 changes: 64 additions & 0 deletions lib/unity-scene-tools.ts
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;
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dinomite-studios/unity-azure-pipelines-tasks-lib",
"version": "1.0.11",
"version": "1.0.12",
"description": "A library containing common implementations for pipeline tasks available in the Unity Tools for Azure DevOps extension.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
9 changes: 9 additions & 0 deletions test/ProjectSettings/EditorBuildSettings.asset
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
19 changes: 19 additions & 0 deletions test/unity-scene-tools.spec.ts
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);
});
});

0 comments on commit 00e5b3b

Please sign in to comment.