Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Editor Arry Sorting Script

Discussion in 'Scripting' started by fffMalzbier, Aug 18, 2011.

  1. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    I found myself often with the problem that i need to add tons of items into a Array.
    Normally you expect so see the same order you selected the items.
    But Unity does not follow the order.

    So i tinker a scrip that sort a Array by name in the editor.
    In the inspector of the scrip in which the Array is needed to sort a button will appear.
    Tested with textures, but should work for the most other stuff. You can easily add all the data type you need.

    ArraySort.cs
    Code (csharp):
    1.  
    2.  
    3. using UnityEditor;
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. [CustomEditor( typeof(name_of_script_which_contains_the_array) )]
    8. public class ArraySort : Editor {
    9.      name_of_script_which_contains_the_array m_Instance;
    10.      public override void OnInspectorGUI () {
    11.           this.DrawDefaultInspector();
    12.           if(GUILayout.Button("Sort the array by name"))
    13.           {
    14.                name_of_script_which_contains_the_array current_target = target as name_of_script_which_contains_the_array;
    15.                Debug.Log("The Array name_of_sorted_array got sorted by Name.");
    16.                System.Array.Sort( current_target.name_of_sorted_array, CompareObNames );
    17.           }
    18.      }
    19.  
    20.      int CompareObNames( Texture x, Texture y )
    21.      {
    22.      return x.name.CompareTo( y.name );
    23.      }
    24.      int CompareObNames( Texture2D x, Texture2D y )
    25.      {
    26.      return x.name.CompareTo( y.name );
    27.      }
    28.      int CompareObNames( GameObject x, GameObject y )
    29.      {
    30.      return x.name.CompareTo( y.name );
    31.      }
    32.      int CompareObNames( Transform x, Transform y )
    33.      {
    34.      return x.name.CompareTo( y.name );
    35.      }
    36.      int CompareObNames( GUIText x, GUIText y )
    37.      {
    38.      return x.name.CompareTo( y.name );
    39.      }
    40.      int CompareObNames( GUITexture x, GUITexture y )
    41.      {
    42.      return x.name.CompareTo( y.name );
    43.      }
    44.      int CompareObNames( Material x, Material y )
    45.      {
    46.      return x.name.CompareTo( y.name );
    47.      }
    48.      int CompareObNames( PhysicMaterial x, PhysicMaterial y )
    49.      {
    50.      return x.name.CompareTo( y.name );
    51.      }
    52.      int CompareObNames( ProceduralMaterial x, ProceduralMaterial y )
    53.      {
    54.      return x.name.CompareTo( y.name );
    55.      }
    56. }
     
  2. mike-hwang

    mike-hwang

    Joined:
    Mar 27, 2013
    Posts:
    7
    Not Really smooth when i test the ArraySort.cs
    No Button exist in Inspector but I move "DrawDefaultInspector()" below "GUILayout.Button..."
    Even Inspector Button exist normally.
    It's still not work. When I click the Sorting Button the Array is sorted looks good. But Run project It's back to original array(Not sorted).

    Anyone Knew Whats happen?

    Also try serializedObject.ApplyModifiedProperties() serializedObject.Update() . It's not work too.
     
    Last edited: Jan 15, 2014
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    I retested it because i thought the upgrade from 3.2 to 4.3 could have broke it but it works fine for me(so far)(i tested it with 20 Different Materials).
    I did a debug at runtime of the whole array to see if it correct at runtime and it was.

    Could you give me an not working example ?
    Of what type of content is your array?

    I created it to import and sort textures but if you have a custom type you may have to add a new version of the "CompareObNames" function matching your type.
     
  4. mike-hwang

    mike-hwang

    Joined:
    Mar 27, 2013
    Posts:
    7
    Thank you for your reply. Here is the code.
    My Unity version is 4.0.0f7 . I'll upgrade to new version to testing same code again.

    Code (csharp):
    1.  
    2. GuiExplore .cs
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class GuiExplore : MonoBehaviour {
    8.     public Texture [] image;  // 90 textures drag in
    9.     int     index=-1;
    10.  
    11.     // Initial index when enable
    12.     void Start () {
    13.         index = -1;
    14.     }
    15.    
    16.     void Update () {
    17.         // change texture on GUI( as GUI background)
    18.         // this object needs a GUITexture
    19.     }
    20. }
    21.  
    22. =====================================
    23. GuiExploreEditor .cs
    24.  
    25. using UnityEngine;
    26. using System.Collections;
    27. using UnityEditor;
    28.  
    29. [CustomEditor(typeof(GuiExplore))]
    30. public class GuiExploreEditor : Editor
    31. {
    32.     int SortByName( Texture o1, Texture o2)
    33.     {
    34.         return o1.name.CompareTo( o2.name);
    35.     }
    36.    
    37.     public override void OnInspectorGUI()
    38.     {
    39.         if( GUILayout.Button("Editor: Sorting array"))
    40.                     System.Array.Sort( ((GuiExplore)target).image, SortByName);
    41.  
    42.         DrawDefaultInspector();
    43.     }
    44. }
    45.  
     
    Last edited: Jan 16, 2014
  5. mike-hwang

    mike-hwang

    Joined:
    Mar 27, 2013
    Posts:
    7
    Sorry! It's Working fine at version 4.1.0f4.