EditorGUILayout.TextField 不是 Changing/Working Unity 2017.1.1f1

EditorGUILayout.TextField Not Changing/Working Unity 2017.1.1f1

问题
使用 EditorGUILayout.TextField 编辑货币名称允许我修改字段中的文本,但不会修改变量或保存我所做的更改。看不懂就看视频:

https://www.youtube.com/watch?v=fr3cA0_YhXM

代码不工作:

for(int cnt = 0; cnt < Script.CurrencyLevels; cnt++)
{
    EditorGUILayout.LabelField("Currency " + cnt + ": ");
    Script.CurrencyName[cnt] = EditorGUILayout.TextField(Script.CurrencyName[cnt]);
}

完整代码链接

https://hastebin.com/omajulihor.cs
https://hastebin.com/eqefuvotog.cs 



语言:C#
引擎:Unity 2017.1.1f1

提前感谢您的帮助<3

首先,在您的代码中,OnEditorGUI 的开头有一行:

Script.CurrencyName = new string[Script.CurrencyLevels];

这实际上设置了 CurrencyName 没有 给定大小的新空数组,即使您不更改大小也是如此。并擦除您在此行中设置的所有内容:

Script.CurrencyName[cnt] = EditorGUILayout.TextField(Script.CurrencyName[cnt]);

要解决此问题,您应该编写一些代码将值从 'old' 数组复制到 'new' 数组。

此外,当您在自定义编辑器中更改某些值时 class 您需要在 OnInspectorGUI() 方法中添加两行代码,开头和结尾各有一行:

    public override void OnInspectorGUI()
    {
        //update the editor's representation of the object
        // which you're using the editor for
        serializedObject.Update();

        // Here goes your editor's custom logic

        //Save the changes you or editor's user has made to 
        // the target object
        serializedObject.ApplyModifiedProperties();
     }

有关 serializedObject 的详细信息,请参阅 link