Advertisement
caLLowCreation

Editor Window ReorderableList

Feb 5th, 2016
1,593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEditorInternal;
  3. using UnityEngine;
  4.  
  5. public class AnimalsEditorWindow : EditorWindow
  6. {
  7.     const int TOP_PADDING = 2;
  8.     const string HELP_TEXT = "Can not find 'Animals' component on any GameObject in the Scene.";
  9.  
  10.     static Vector2 s_WindowsMinSize = Vector2.one * 300.0f;
  11.     static Rect s_HelpRect = new Rect(0.0f, 0.0f, 300.0f, 100.0f);
  12.     static Rect s_ListRect = new Rect(Vector2.zero, s_WindowsMinSize);
  13.  
  14.     [MenuItem("Window/Animals/Open Window")]
  15.     static void Initialize()
  16.     {
  17.         AnimalsEditorWindow window = EditorWindow.GetWindow<AnimalsEditorWindow>(true, "Make Animal List");
  18.         window.minSize = s_WindowsMinSize;
  19.     }
  20.  
  21.     SerializedObject m_AnimalsSO = null;
  22.     ReorderableList m_ReorderableList = null;
  23.  
  24.     void OnEnable()
  25.     {
  26.         Animals animals = FindObjectOfType<Animals>();
  27.         if(animals)
  28.         {
  29.             m_AnimalsSO = new SerializedObject(animals);
  30.             m_ReorderableList = new ReorderableList(m_AnimalsSO, m_AnimalsSO.FindProperty("names"), true, true, true, true);
  31.            
  32.             m_ReorderableList.drawHeaderCallback = (rect) => EditorGUI.LabelField(rect, "Animal Names");
  33.             m_ReorderableList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
  34.             {
  35.                 rect.y += TOP_PADDING;
  36.                 rect.height = EditorGUIUtility.singleLineHeight;
  37.                 EditorGUI.PropertyField(rect, m_ReorderableList.serializedProperty.GetArrayElementAtIndex(index));
  38.             };
  39.         }
  40.     }
  41.  
  42.     void OnInspectorUpdate()
  43.     {
  44.         Repaint();
  45.     }
  46.  
  47.     void OnGUI()
  48.     {      
  49.         if(m_AnimalsSO != null)
  50.         {
  51.             m_AnimalsSO.Update();
  52.             m_ReorderableList.DoList(s_ListRect);
  53.             m_AnimalsSO.ApplyModifiedProperties();
  54.         }
  55.         else
  56.         {
  57.             EditorGUI.HelpBox(s_HelpRect, HELP_TEXT, MessageType.Warning);
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement