【Unity】ScriptableSingletonでエディタの状態を保存する

投稿者: | 2023-05-12

ScriptableSingletonを使って、スクリプトの再読み込みやプロジェクトの再起動してもエディタの状態が保存されるようにしてみました。

まずUnityマニュアルの以下のコードを使ってみます。https://docs.unity3d.com/ja/2021.2/ScriptReference/ScriptableSingleton_1.html

ScriptableSingletonを使う

マニュアルのサンプルコードのスクリプトを作ると、メインメニューからスクリプトのModifyとLogメソッドを実行できるようになります。

Logをクリックすると、float型の変数の値が42であることがConsoleに表示されます。この変数はScriptableSingletonクラスを継承したクラスに定義されています。

Modifyをクリックすると保存されて、もう一度Logをクリックすると値が2倍になっていることがわかります。string型のリストにも値が追加されています。

プロジェクトを再起動してもう一度Logを押すと、変更後の値が表示されました。

クラスにFilePath属性をつけると、データが保存されて再起動しても変数の値がリセットされません。

  1. // 「ScriptableSingleton - Unity スクリプトリファレンス」より引用
  2. // https://docs.unity3d.com/ja/2021.2/ScriptReference/ScriptableSingleton_1.html
  3. [FilePath("SomeSubFolder/StateFile.foo", FilePathAttribute.Location.PreferencesFolder)]
  4. public class MySingleton : ScriptableSingleton

属性の引数でファイルの保存場所やファイル名を指定します。第一引数の相対パスが第二引数で選択するルートフォルダと組み合わされます。

第二引数にFilePathAttribute.Location.PreferencesFolderを渡すと、環境設定フォルダが使われます。Windowsの場合は「C:/Users/[username]/AppData/Roaming/Unity/Editor-5.x/Preferences/」です。

FilePathAttribute.Location.ProjectFolderの場合は、AssetsフォルダやLibraryフォルダのある場所と相対パスが結合されます。

ファイルパスはScriptableSingleton<T0>.GetFilePathメソッドで取得できます。ファイルはYAML形式です。

ログを表示するときには、JsonUtility.ToJsonメソッドを使って、publicフィールドのJSON形式のテキストデータを生成しています。

プロパティの場合は表示されませんでした。

これでプロジェクトを開いたときに、保存されたbool値によって自動で開くエディタウィンドウを作ってみました。

ScriptableSingletonの派生クラス

まず、ScriptableSingleton<T0>の派生クラスを作ります。

  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4. [FilePath("SubFolder/File.foo", FilePathAttribute.Location.ProjectFolder)]
  5. public class TestSingleton : ScriptableSingleton<TestSingleton>
  6. {
  7. [SerializeField] bool getWindowOnLoad;
  8. public bool GetWindowOnLoad { get => getWindowOnLoad; }
  9.  
  10. public void Modify(bool getWindowOnLoad)
  11. {
  12. this.getWindowOnLoad = getWindowOnLoad;
  13.  
  14. Save(true);
  15. Debug.Log("Saved to: " + GetFilePath());
  16. }
  17. }

bool値のフィールドとゲッタープロパティ、値をセットして保存するメソッドがあります。

エディタウィンドウ

次に値を変更するトグルや保存ボタンを持つエディタウィンドウを作ります。

  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class ScriptableSingletonTest : EditorWindow
  5. {
  6. [MenuItem("Window/Test/ScriptableSingletonTest")]
  7. static void CreateWindow()
  8. {
  9. EditorWindow.GetWindow<ScriptableSingletonTest>();
  10. }
  11.  
  12. bool getWindowOnLoad;
  13.  
  14. void OnEnable()
  15. {
  16. // ScriptableSingletonから値を取得
  17. getWindowOnLoad = TestSingleton.instance.GetWindowOnLoad;
  18. }
  19.  
  20. private void OnGUI()
  21. {
  22. // トグルを表示
  23. getWindowOnLoad = EditorGUILayout.Toggle(getWindowOnLoad);
  24.  
  25. // ボタンを押すと
  26. if (GUILayout.Button("保存"))
  27. {
  28. // 値を保存
  29. TestSingleton.instance.Modify(getWindowOnLoad);
  30. }
  31. }
  32.  
  33. // エディタを読み込んだときに実行
  34. [InitializeOnLoadMethod]
  35. static void OnProjectLoadedInEditor()
  36. {
  37. if (!TestSingleton.instance.GetWindowOnLoad) return;
  38.  
  39. // アップデートのためのデリゲートに追加
  40. EditorApplication.update += OnEditorUpdate;
  41. }
  42.  
  43. // コールバック関数
  44. static void OnEditorUpdate()
  45. {
  46. // デリゲートの解除
  47. EditorApplication.update -= OnEditorUpdate;
  48.  
  49. // エディタウィンドウを作成
  50. CreateWindow();
  51. }
  52. }

エディタウィンドウを取得する静的メソッドにMenuItem属性を付けてメインメニューから実行できるようにしています。

  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class ScriptableSingletonTest : EditorWindow
  5. {
  6. [MenuItem("Window/Test/ScriptableSingletonTest")]
  7. static void CreateWindow()
  8. {
  9. EditorWindow.GetWindow<ScriptableSingletonTest>();
  10. }

オブジェクトが有効になったときに、ScriptableSingletonの派生クラスからbool値を取得します。インスタンスは、ScriptableSingleton<T0>.instanceに格納されています。

  1. bool getWindowOnLoad;
  2.  
  3. void OnEnable()
  4. {
  5. // ScriptableSingletonから値を取得
  6. getWindowOnLoad = TestSingleton.instance.GetWindowOnLoad;
  7. }

エディタウィンドウの内容はOnGUIメソッドに実装します。トグルでbool値を変更して、保存ボタンを押すとシングルトンの値をセットして保存するメソッドを呼びます。

  1. private void OnGUI()
  2. {
  3. // トグルを表示
  4. getWindowOnLoad = EditorGUILayout.Toggle(getWindowOnLoad);
  5.  
  6. // ボタンを押すと
  7. if (GUILayout.Button("保存"))
  8. {
  9. // 値を保存
  10. TestSingleton.instance.Modify(getWindowOnLoad);
  11. }
  12. }

プロジェクトが開かれたときに自動的にエディタウィンドウを開くには、エディタウィンドウを作成するメソッドを、更新処理のためのデリゲートに登録します。

  1. [InitializeOnLoadMethod]
  2. static void OnProjectLoadedInEditor()
  3. {
  4. if (!TestSingleton.instance.GetWindowOnLoad) return;
  5.  
  6. // アップデートのためのデリゲートに追加
  7. EditorApplication.update += OnEditorUpdate;
  8. }

OnProjectLoadedInEditorメソッドにはInitializeOnLoadMethod属性がついているので、Unityエディタを読み込んだときに実行されます。保存されたbool値がfalseだとデリゲートに登録しません。

コールバック関数では、デリゲートを解除してエディタウィンドウを作成します。

  1. static void OnEditorUpdate()
  2. {
  3. // デリゲートの解除
  4. EditorApplication.update -= OnEditorUpdate;
  5.  
  6. // エディタウィンドウを作成
  7. CreateWindow();
  8. }
  9. }

エディタウィンドウを開き、トグルをオンにして保存ボタンを押します。

エディタウィンドウを閉じてからプロジェクトを再起動すると、エディタウィンドウが自動で開きました。トグルがオフだと開きません。

これで、プロジェクトを再起動しても状態が保存されるようになりました。

参考:https://docs.unity3d.com/ja/2021.2/ScriptReference/ScriptableSingleton_1.html
https://docs.unity3d.com/ja/2021.2/ScriptReference/FilePathAttribute-ctor.html
https://answers.unity.com/questions/1418351/how-to-run-a-script-once-when-a-project-is-first-l.html

コメントを残す

メールアドレスが公開されることはありません。