【问题标题】:Cannot save a variable of a editor script无法保存编辑器脚本的变量
【发布时间】:2020-04-16 06:56:50
【问题描述】:

您好,我正在尝试保存自定义编辑器值的字段设置在组合框中,我从 Internet 获取代码,但我意识到在组合框中所做的更改根本没有保存,我尝试使用“保存”按钮,我尝试制作序列化字段,但我是 C# 和统一的新手。感谢您的光临,祝您有美好的一天。

    using UnityEditor;
    using UnityEngine;


    [CustomEditor(typeof(DoorScript))]
    [System.Serializable]
    public class ButtonDropDownMenu : Editor
    {
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            DoorScript script = (DoorScript)target;

            GUIContent arrayLabel = new GUIContent("Color");
            script.index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
            /*serializedObject.FindProperty("index").intValue = script.index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
            script.index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);

            var properlyToSave = serializedObject.FindProperty("Index");
            EditorGUILayout.PropertyField(properlyToSave);
            serializedObject.ApplyModifiedProperties();¨*/

        }
    }



using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;

public class DoorScript : MonoBehaviour
{
    private SpriteRenderer myColor;
    public Light2D doorLight;
    public Light2D doorPLight;

    [HideInInspector][SerializeField]
    public int index = 0;
    [HideInInspector][SerializeField]
    public string[] options = new string[]
    {
        "White",
        "Red",
        "Blue",
        "Cyan"

    };

    private void Awake()
    {
        myColor = GetComponent<SpriteRenderer>();
        ColorChanger();
    }

    void Start()
    {

    }

    private void ColorChanger()
    {
        if (index <= 0)
        {
            DoorsLights(Color.grey);
        }
        else if (index == 1)
        {
            DoorsLights(Color.red);
        }
        else if (index == 2)
        {
            DoorsLights(Color.blue);
        }
        else if (index >= 3)
        {
            DoorsLights(Color.green);
        }
    }

    private void DoorsLights(Color color)
    {
        myColor.color = color;
        doorLight.color = color;
        doorPLight.color = color;
    }
}

编辑:

我工作完美,我做了改变而不是

index = EditorGUILayout.Popup(arrayLabel, script.index, script.options); 

我用过

 index.intValue = EditorGUILayout.Popup(arrayLabel, script.index, script.options); 

【问题讨论】:

  • 当你取消注释serializedObject.ApplyModifiedProperties();时它仍然没有保存吗...那行应该是必要的
  • 我会再试一次,请稍等编辑:还没有工作

标签: c# unity3d


【解决方案1】:

如果您想从自定义编辑器更新属性,您需要使用SerializedProperty。像这样:

[CustomEditor(typeof(DoorScript))]
public class ButtonDropDownMenu : Editor
{
    SerializedProperty index;
    void OnEnable()
    {
        index = serializedObject.FindProperty("index");
    }
...

然后确保您在完成后拨打serializedObject.ApplyModifiedProperties();

public override void OnInspectorGUI()
{
    base.OnInspectorGUI();
    ...
    index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
    ...
    serializedObject.ApplyModifiedProperties();
}

希望这会有所帮助!如果这不起作用,请告诉我

【讨论】:

  • 我工作得很好,我做了一个改变而不是'code' index = EditorGUILayout.Popup(arrayLabel, script.index, script.options); '代码'
  • 很高兴!顺便说一句,您可以使用 ` 来正确格式化“代码”,我的符号按钮位于键盘的左上角
猜你喜欢
  • 2019-02-16
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 2019-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多