2016-04-28 434 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 
}