EditorGUILayout弹出窗口在下拉选项更改后不接受更改

时间:2016-04-28 05:42:35

标签: unity3d popup unity5

我正在尝试创建一个自定义编辑器,其中包含一个扩展文件类型列表,允许用户选择要放入的“文件夹”。我遇到的问题是弹出窗口的选定索引(我有一个基于需要移动的扩展数量的数组)不会改变并保持在最后位置。

enter image description here

<div class="f">
  <div class="s">
    <div class="t">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id impedit accusamus nulla facilis unde sed ipsum maiores adipisci, eius excepturi saepe perspiciatis sequi optio ipsam odio quibusdam quo libero repudiandae.
    </div>
  </div>
</div>

Screen grab of Code errors

2 个答案:

答案 0 :(得分:0)

似乎这里的原因是你将{0}作为EditorGUILayout.Popup方法的'selectedIndex'参数传递。 此参数应包含存储中当前选定的索引。 像那样:

index = EditorGUILayout.Popup("Label", index, displayedOptions);

想法是将'selectedIndex'参数作为您用来从方法获取返回值的相同变量传递。

private List<CharacterState> selected;

    void OnGUI()
    {
        if (selected == null)
        {
            selected = new List<CharacterState>();
            foreach (var value in Enum.GetValues(typeof(CharacterState)))
            {
                selected.Add((CharacterState) value);
            }
        }

        int[] index = selected.Select(x=>(int)x).ToArray();

        GUILayout.BeginArea(new Rect(0, 0, 300, 300));
        EditorGUILayout.BeginVertical();
        scrollPosition =
        EditorGUILayout.BeginScrollView(scrollPosition);

        var vals = Enum.GetNames(typeof (CharacterState));

        for (int i = 0; i < index.Length; i++)
        {
            var v = EditorGUILayout.Popup(((CharacterState)index[i]).ToString(), index[i], vals, GUILayout.Width(275));
            index[i] = v;
            EditorGUILayout.Space();
        }

        var j = 0;
        foreach (var i in index)
            selected[j++] = (CharacterState)i;

        EditorGUILayout.EndScrollView();
        EditorGUILayout.EndVertical();
        GUILayout.EndArea();
    }

正如您所看到的,我将所有枚举值转换为int [],但是在Popup方法调用之后,我会使用索引对数组进行另一次迭代,以便将值存储在“selected”列表中。 / p>

答案 1 :(得分:0)

每次调用方法时都会出现创建索引数组的问题,因此无论您为index [i]分配什么,下次调用该帧时都会重置为零。所以我要做的是将值分配给你的extBank或​​使索引数组成为成员变量而不是局部变量。

int[] index = new int[0];
void OnGUI()
{
    //extToggles = new bool[extBank.GetNumOfExtensions];
    if(index.Length != extBank.GetNumOfExtensions)
        System.Array.ReSize(ref index, extBank.GetNumOfExtensions);
    int selectedIndex = 0;

    GUILayout.BeginArea(new Rect(0, 0, 300, 300));
    EditorGUILayout.BeginVertical();
    scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

    for (int i = 0; i < index.Length; i++)
    {
        index[i] = EditorGUILayout.Popup(extBank.GetExtensions(i), index[i], categories.ToArray(), GUILayout.Width(275));
       EditorGUILayout.Space();
    }
    //rest of your code
}