Unity Custom Editor GUI

时间:2018-05-04 13:16:45

标签: c# unity3d unity-editor

screenshot

我在编辑器中创建新值后在字段中添加新值时出现问题,他们会继续回来,如果我输入新值,它将显示第一个值。任何人都可以给我一些代码片段如何添加将自动保存的值。

谢谢!

    GUILayout.BeginHorizontal();
    GUILayout.BeginVertical();
    addInteger = GUILayout.Toggle(addInteger, "Integers");
    howMuchIntegers = EditorGUILayout.IntField(howMuchIntegers);
    intNames = new string[howMuchIntegers];
    if (addInteger)
    {
        if (howMuchIntegers != 0)
        {
            GUILayout.BeginVertical("box");
            for (int i = 0; i < howMuchIntegers; i++)
            {
                intNames[i] = i.ToString();
                intNames[i] = EditorGUILayout.TextField(intNames[i]);
            }
            GUILayout.BeginVertical("box");
        }
    }
    GUILayout.EndVertical();
    GUILayout.EndHorizontal();

1 个答案:

答案 0 :(得分:1)

我认为你的数据会在两个地方被覆盖。

下面,

intNames = new string[howMuchIntegers];

在这里,

intNames[i] = i.ToString();

所以这是一个解决方案。基本上我只是建议你提取不需要每秒运行60次的部件,你可以随心所欲地做到这一点。

 bool firstTimeRun = true;

 void OnGUI()
 {    
    GUILayout.BeginHorizontal();
    GUILayout.BeginVertical();
    addInteger = GUILayout.Toggle(addInteger, "Integers");
    howMuchIntegers = EditorGUILayout.IntField(howMuchIntegers);

    if(firstTimeRun)
    {
        intNames = new string[howMuchIntegers];
        if (addInteger)
        {
            if (howMuchIntegers != 0)
            {
                GUILayout.BeginVertical("box");
                for (int i = 0; i < howMuchIntegers; i++)
                {
                    intNames[i] = i.ToString();
                    intNames[i] = EditorGUILayout.TextField(intNames[i]);
                }
                GUILayout.BeginVertical("box");
            }
        }

        firstTimeRun = false;
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();
    }
    else
    {
        if (addInteger)
        {
            if (howMuchIntegers != 0)
            {
                GUILayout.BeginVertical("box");
                for (int i = 0; i < howMuchIntegers; i++)
                {
                    intNames[i] = EditorGUILayout.TextField(intNames[i]);
                }
                GUILayout.BeginVertical("box");
            }
        }
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();
    }
 }