2016-04-28 429 views
0

我正在尝试创建一个自定义编辑器,其中包含允许用户选择要放入哪个“文件夹”的扩展文件类型列表。因为弹出窗口的选定索引(根据需要移动的扩展的数量,我有一组索引)不会改变,并保持在最后位置。EditorGUILayout Popup在下拉选项更改后不接受更改

enter image description here

public class ExtensionWindow : EditorWindow 
{ 
    ExtensionBank extBank; 

    string[] exts; 
    public bool[] extToggles; 

    public static List<string> extensions; 
    public static List<string> categories; 

    public static Vector2 scrollPosition; 

    [MenuItem("Build Master's Dream/Organize Files")] 
    static void Organize() 
    { 
     ExtensionWindow myWindow = (ExtensionWindow)EditorWindow.GetWindow(typeof(ExtensionWindow), true, "Extensions"); 
     myWindow.minSize = new Vector2(300.0f, 300.0f); 
     myWindow.maxSize = new Vector2(300.0f, 300.0f); 
    } 

    void Awake() 
    { 
     //Get all Category names and store them in a List<> 
     LoadFolderNames(); 
     LoadFolders(); 

     extBank = new ExtensionBank(); 

     //Populate the bank of Extensions from the .txt file 
     bool success; 
     success = extBank.PopulateList(); 

     exts = new string[extBank.GetNumOfExtensions]; 

     if (success) 
     { 
      for (int i = 0; i < extBank.GetNumOfExtensions; i++) 
       string shortExtensions = extBank.GetExtensions(i); 
     } 
    } 

    void OnGUI() 
    { 
     //extToggles = new bool[extBank.GetNumOfExtensions]; 

     int[] index = new int[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(); 
     } 
     if (GUILayout.Button("Make Your Dream Come True", EditorStyles.miniButtonLeft)) 
     { 
      string[] shortCategories = categories.ToArray(); 

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

    void LoadFolders() 
    { 
     foreach (string cat in categories) 
     { 
      string projectPath = Application.dataPath + "/"; 
      if (cat != "Select Category") ; 
      { 
       Directory.CreateDirectory(projectPath + cat); 
      } 
     } 
     AssetDatabase.Refresh(); 
    } 

    void BeginMoveOperation(string ext, string cat) 
    { 
     Debug.Log("Data path is: " + Application.dataPath.ToString()); 

     string localPath = Application.dataPath; 

     DirectoryInfo dir = new DirectoryInfo(localPath); 
     FileInfo[] info = dir.GetFiles("*.*"); 
     foreach (FileInfo f in info) 
     { 
      Debug.Log("Name is: " + f.Name); 
      Debug.Log("Extension is: " + f.Extension); 
      MoveAssetsIntoFolders(f.Name, f.Extension, f.FullName); 
     } 
    } 

    static void MoveAssetsIntoFolders(string fileName, string extension, string oldPath) 
    { 

    } 

    void LoadFolderNames() 
    { 
     var path = "Assets/Scripts/Editor/Extensions.txt"; 

     if (File.Exists(path)) 
     { 
      try 
      { 
       var fileContent = File.ReadAllLines(path); 
       categories = new List<string>(); 
       foreach (var line in fileContent) 
       { 
        if (line != "") 
        { 
         if ((line.Substring(0, 1) != "" || line.Substring(0, 1) != null) && line.Substring(0, 1) != ".") 
         { 
          categories.Add(line); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Debug.Log(ex); 
      } 
     } 
    } 
    void OrganizeScripts()     
    { 
     Assembly _assembly = Assembly.Load("Assembly-CSharp"); 

     foreach (Type type in _assembly.GetTypes()) 
     { 
      if (type.IsClass) 
      { 
       if (type.BaseType.FullName.Contains("MonoBehaviour"))   //Standard Unity Scripts 
       { 

       } 
       else if (type.BaseType.FullName.Contains("Editor"))    //Unity Editor Files 
       { 

       } 
      else               //All others, likely .js scripts 
       { 

Screen grab of Code errors

回答

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 []为你做什么,但弹出方法调用之后我做索引的数组的另一次迭代,以存储中的值“选择'列表。

+0

我试过了,同样的错误 –

+0

你可以发布你的代码在哪里你试图这么做吗? –

+0

这里是整个班级,所有连接到它的项目,我也可以通过。 –

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 
}