Created
May 21, 2023 06:16
-
-
Save nasan730/66291dec0b51248f00b49105aad86b57 to your computer and use it in GitHub Desktop.
Unity ビルド設定に置かれたSceneを元にEnumを作成するScript
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
/** | |
* FileName : SceneTypeGenerator.cs | |
* Description : ビルド設定に置かれたSceneを元にEnumを作成するScript | |
* | |
* Copyright 2023 H.Nakata. All rights reserved. | |
*/ | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using UnityEngine; | |
using UnityEditor; | |
namespace NasanUtility.Editor.SceneTypeGenerator | |
{ | |
public class SceneTypeGenerator : MonoBehaviour | |
{ | |
/// <summary> ESceneType生成 </summary> | |
[InitializeOnLoadMethod] | |
static void SceneTypeGenerate() | |
{ | |
EditorBuildSettings.sceneListChanged += () => | |
{ | |
// コード生成 | |
List<string> writeCodes = new List<string>(); | |
writeCodes.Add("// SceneTypeGenerator.csで生成\n"); | |
writeCodes.Add("namespace NasanUtility.SceneManager"); | |
writeCodes.Add("{"); | |
writeCodes.Add("\t/// <summary> シーンタイプ </summary>"); | |
writeCodes.Add("\t[System.Serializable]"); | |
writeCodes.Add("\tpublic enum ESceneType"); | |
writeCodes.Add("\t{"); | |
writeCodes.Add("\t\tNone = -1,"); | |
// シーン一覧からシーン名と状態を取得 | |
foreach (var scene in EditorBuildSettings.scenes) | |
{ | |
string sceneName = scene.path; | |
sceneName = sceneName.Remove(0, sceneName.LastIndexOf("/") + 1).Replace(".unity", "").Replace(" ", "_"); | |
writeCodes.Add("\t\t" + sceneName + ","); | |
} | |
writeCodes.Add("\t\tMax,"); | |
writeCodes.Add("\t}"); | |
writeCodes.Add("}"); | |
// 生成 | |
string filePath = Application.dataPath + "/NasanUtility/SceneManager/ESceneType.cs"; | |
FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate); | |
StreamWriter sw = new StreamWriter(stream, Encoding.UTF8); | |
sw.Write(""); | |
foreach (var code in writeCodes) | |
{ | |
sw.WriteLine(code); | |
} | |
sw.Close(); | |
AssetDatabase.Refresh(); | |
Debug.Log("シーンリスト更新終了"); | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment