自定义检查器枚举在播放时重置

威士忌高尔夫

我有一个自定义编辑器脚本,它有一个枚举来定义应用程序是否应该启动到设置模式或实时模式。如果我将枚举设置为 no,当我点击播放时,应用程序在没有选择的情况下正常工作,但随后枚举恢复为 yes 状态,当我退出播放模式时,枚举返回到 yes 状态,并且不是播放前选择的状态。这是我的脚本。

[CustomEditor(typeof(MainController))]
public class MainControllerEditor : Editor
{
    public enum IsSetupEnabled
    {
        Yes,
        No
    };

    public IsSetupEnabled SetupEnabled;

    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        GUI.changed = false;
        var mainController = (MainController)target;

        SetupEnabled = (IsSetupEnabled)EditorGUILayout.EnumPopup("Setup Enabled", SetupEnabled);
        if (GUI.changed)
        {
            switch (SetupEnabled)
            {
                case IsSetupEnabled.Yes:
                    mainController.SetupEnabled(true);
                    break;
                case IsSetupEnabled.No:
                    mainController.SetupEnabled(false);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        if (GUILayout.Button("Validate Configuration"))
        {
            mainController.Validate();
        }
    }
}

当我找到其他人遇到的这个问题的解决方案时,我尝试将枚举标记为 Serializable 并将 mainController 标记为脏在 Gui.Changed 中,但它似乎对我没有任何作用。

Jihun Song

您可以将 SetupEnabled 变量从 MainControllerEditor 移动到 MainController。

然后它会被内置的序列化,保存和加载。

public enum IsSetupEnabled
{
    Yes,
    No
};

public IsSetupEnabled SetupEnabled;

为了使您的变量不显示在检查器中(但被序列化)并且不在运行时执行:

#if UNITY_EDITOR
public enum IsSetupEnabled
{
    Yes,
    No
};

[HideInInspector]
public IsSetupEnabled SetupEnabled;
#endif

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章