在编辑器会话之间存储编辑器值

Rem*_*_rm 2 c# unity-game-engine

我有以下 EditorWindow 脚本可以创建自定义检查器窗口。在此窗口中按下按钮后,id将增加 1。

static int id = 10000;

[MenuItem("Custom/CustomMenu")]
static void Init()
{
    // Get existing open window or if none, make a new one:
    CustomMenu window = (CustomMenu)EditorWindow.GetWindow(typeof(CustomMenu));
    window.Show();
}

if (GUILayout.Button("someButton"))
{
    id++;
    Repaint();
    EditorUtility.SetDirty(this);
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,但是当我启动播放模式或关闭统一编辑器时,增量值id丢失,并将重置回 10000。

使用 的非静态版本int id将使该值在“播放”会话中持续存在,但一旦关闭 unity 仍会丢失。

有没有办法在编辑器/统一会话之间存储这个值,比如playerprefs但是对于编辑器?

der*_*ugo 5

脚本化对象

也许更像是“统一”的方式是使用专用的ScriptableObject(另请参阅ScriptableObjects 简介

您可以将它与 an 结合[InitializeOnLoadMethod]以实现一个加载方法,该方法将在打开编辑器和重新编译后调用以创建 ScriptableObject一次

// we don't need the CreateAssetMenu attribute since the editor window
// will create the ScriptableObject once by itself
public class CustomMenuData : ScriptableObject
{
    public int Id;
}
Run Code Online (Sandbox Code Playgroud)

确保将其放在单独的脚本中。

public class CustomMenu : EditorWindow
{
    // we can go privtae again ;)
    private static CustomMenuData data;

    // This method will be called on load or recompile
    [InitializeOnLoadMethod]
    private static void OnLoad()
    {
        // if no data exists yet create and reference a new instance
        if (!data)
        {
            // as first option check if maybe there is an instance already
            // and only the reference got lost
            // won't work ofcourse if you moved it elsewhere ...
            data = AssetDatabase.LoadAssetAtPath<CustomMenuData>("Assets/CustomMenuData.asset");

            // if that was successful we are done
            if(data) return;

            // otherwise create and reference a new instance
            data = CreateInstance<CustomMenuData>();

            AssetDatabase.CreateAsset(data, "Assets/CustomMenuData.asset");
            AssetDatabase.Refresh();
        }
    }

    [MenuItem("Custom/CustomMenu")]
    private static void Init()
    {
        // Get existing open window or if none, make a new one:
        var window = (CustomMenu)EditorWindow.GetWindow(typeof(CustomMenu));

        window.Show();
    }

    private void OnGUI()
    {
        // Note that going through the SerializedObject
        // and SerilaizedProperties is the better way of doing this!
        // 
        // Not only will Unity automatically handle the set dirty and saving
        // but it also automatically adds Undo/Redo functionality!
        var serializedObject = new SerializedObject(data);

        // fetches the values of the real instance into the serialized one
        serializedObject.Update();

        // get the Id field
        var id = serializedObject.FindProperty("Id");

        // Use PropertyField as much as possible
        // this automaticaly uses the correct layout depending on the type
        // and automatically reads and stores the according value type
        EditorGUILayout.PropertyField(id);

        if (GUILayout.Button("someButton"))
        {
            // Only change the value throgh the serializedProperty
            // unity marks it as dirty automatically
            // also no Repaint required - it will be done .. guess .. automatically ;)
            id.intValue += 1;
        }

        // finally write back all modified values into the real instance
        serializedObject.ApplyModifiedProperties();
    }
}
Run Code Online (Sandbox Code Playgroud)

这样做的巨大优势:

  • 它比使用 FileIO 进行写入和保存更快/更有效,因为 Unity 会自动处理该 ScriptableObject 的(反)序列化。
  • 您不需要“手动”加载和保存数据……它是自动完成的,因为 ScriptableObject 的行为与任何其他预制件一样。
  • 您只需单击资产中的 ScriptableObject 实例并直接更改值!

使用简单的文本文件

一个简单但不是那么有效的替代解决方案是将它存储到一个文件中,例如像这样的 JSON

using System.IO;
using UnityEditor;
using UnityEngine;

public class CustomMenu : EditorWindow
{
    private const string FileName = "Example.txt";

    // shorthand property for getting the filepath
     public static string FilePath
     {
         get { return Path.Combine(Application.streamingAssetsPath, FileName); }
     }

    private static int id = 10000;

    // Serialized backing field for storing the value
    [SerializeField] private int _id;

    [MenuItem("Custom/CustomMenu")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        CustomMenu window = (CustomMenu)EditorWindow.GetWindow(typeof(CustomMenu));

        if (File.Exists(FilePath))
        {
            // read the file content
            var json = File.ReadAllText(FilePath)

            // If the file exists deserialize the JSON and read in the values
            // for only one value ofcourse this is overkill but for multiple values
            // this is easier then parsing it "manually"
            JsonUtility.FromJsonOverwrite(json, window);

            // pass the values on into the static field(s)
            id = window._id;
        }

        window.Show();
    }

    private void OnGUI()
    {
        id = EditorGUILayout.IntField(id);

        if (GUILayout.Button("someButton"))
        {
            id++;
            Repaint();
            EditorUtility.SetDirty(this);

            // do everything in oposide order
            // first fetch the static value(s) into the serialized field(s)
            _id = id;

            // if not exists yet create the StreamingAssets folder
            if (!Directory.Exists(Application.streamingAssetsPath))
            {
                AssetDatabase.CreateFolder("Assets", "StreamingAssets");
            }

            // serialize the values into json
            var json = JsonUtility.ToJson(this);

            // write into the file
            File.WriteAllText(FilePath, json);

            // reload the assets so the created file becomes visible
            AssetDatabase.Refresh();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当前,每次打开窗口时都会读取文件,并在每次单击按钮时写入文件。这仍然可以改进。

同样,您可以使用[InitializeOnLoadMethod]该文件仅读取一次 - 即当您打开编辑器或重新编译时

public class CustomMenu : EditorWindow
{
    // Instead of having the field values directly as static fields
    // rather store the information in a proper serializable class
    [Serializable]
    private class CustomMenuData
    {
        public int Id;
    }

    // made this publlic for the saving process (see below)
    public static readonly CustomMenuData data = new CustomMenuData();

    // InitializeOnLoadMethod makes this method being called everytime
    // you load the project in the editor or after re-compilation
    [InitializeOnLoadMethod]
    private static void OnLoad()
    {
        if (!File.Exists(FilePath)) return;

        // read in the data from the json file
        JsonUtility.FromJsonOverwrite(File.ReadAllText(FilePath), data);
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

为了优化保存并仅在您在 UnityEditor 中保存时执行文件写入,您可以实现一个专用的AssetModificationProcessor

public class CustomMenuSaver : SaveAssetsProcessor
{
    static string[] OnWillSaveAssets(string[] paths)
    {
        // do change nothing in the paths
        // but additionally store the data in the file

        // if not exists yet create the StreamingAssets folder
        if (!Directory.Exists(Application.streamingAssetsPath))
        {
            AssetDatabase.CreateFolder("Assets", "StreamingAssets");
        }

        // serialize the values into json        v That's why I made it public
        var json = JsonUtility.ToJson(CustomMenu.data);

        // write into the file         v needs to be public as well
        File.WriteAllText(CustomMenu.FilePath, json);            

        return paths;
    }
}
Run Code Online (Sandbox Code Playgroud)