gpt4 book ai didi

c# - Unity 自定义编辑器 GUI

转载 作者:行者123 更新时间:2023-11-30 17:30:18 26 4
gpt4 key购买 nike

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();

最佳答案

我认为您的数据在两个地方被覆盖了。

这里,

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();
}
}

关于c# - Unity 自定义编辑器 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50175918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com