Advertisement
KpoKec

ReorderableList PropertyDrawer

Jun 16th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.18 KB | None | 0 0
  1.         #region Универсальный фолд
  2.  
  3.         private static readonly Dictionary<object, bool> _foldouts2 = new Dictionary<object, bool>();
  4.  
  5.         public static bool ShowFold(string text, object @object) {
  6.             var result = false;
  7.             if (!_foldouts2.ContainsKey(@object))
  8.                 _foldouts2.Add(@object, false);
  9.             else
  10.                 result = _foldouts2[@object];
  11.             result              = EditorGUILayout.Foldout(result, text, true);
  12.             _foldouts2[@object] = result;
  13.             return result;
  14.         }
  15.  
  16.         public static bool ShowFold(Rect rect, string text, object @object, bool simply = true) {
  17.             var result = false;
  18.             if (!_foldouts2.ContainsKey(@object))
  19.                 _foldouts2.Add(@object, false);
  20.             else
  21.                 result = _foldouts2[@object];
  22.             result              = EditorGUI.Foldout(rect, result, text, simply);
  23.             _foldouts2[@object] = result;
  24.             return result;
  25.         }
  26.  
  27.         public static bool GetFoldState(object @object) {
  28.             if (!_foldouts2.ContainsKey(@object)) return false;
  29.             return _foldouts2[@object];
  30.         }
  31.  
  32.         #endregion
  33.  
  34.         #region Reorderable list
  35.  
  36.         private static readonly Dictionary<string, ReorderableList> rlist      = new Dictionary<string, ReorderableList>();
  37.         private static readonly Dictionary<string, string>          rlistTitle = new Dictionary<string, string>();
  38.         private static readonly Dictionary<string, object>          rlistFold  = new Dictionary<string, object>();
  39.  
  40.         /// <summary>
  41.         /// Add array property to Reorderable array
  42.         /// </summary>
  43.         /// <param name="prop"></param>
  44.         /// <param name="title"></param>
  45.         public static void AddArray(SerializedProperty prop, string title, bool draggable = true, bool add = true) {
  46.             if (prop == null) return;
  47.             if (!rlist.ContainsKey(GetPropPath(prop)) || rlist[GetPropPath(prop)].serializedProperty != prop) {
  48.                 string s            = "{0:D1}";
  49.                 var rl = new ReorderableList(prop.serializedObject, prop, draggable, false, add, add) {
  50.                     drawHeaderCallback = (Rect rect) => { EditorGUI.LabelField(rect, title); },
  51.                     drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => {
  52.                                               EditorGUI.PropertyField(rect, prop.GetArrayElementAtIndex(index), new GUIContent(string.Format(s, index)));
  53.                                           }
  54.                 };
  55.                 rlist[GetPropPath(prop)]      = rl;
  56.                 rlistTitle[GetPropPath(prop)] = title;
  57.                 if (!rlistFold.ContainsKey(GetPropPath(prop)))
  58.                     rlistFold[GetPropPath(prop)] = new object();
  59.             }
  60.         }
  61.  
  62.         /// <summary>
  63.         /// Show array property at Rect
  64.         /// </summary>
  65.         /// <param name="prop"></param>
  66.         /// <param name="rect"></param>
  67.         public static void ShowArray(SerializedProperty prop, Rect rect) {
  68.             if (rlist.ContainsKey(GetPropPath(prop))) {
  69.                 rect.height = EditorGUIUtility.singleLineHeight;
  70.                 if (ShowFold(rect, rlistTitle[GetPropPath(prop)], rlistFold[GetPropPath(prop)])) {
  71.                     rect.height = GetArrayHeight(prop);
  72.                     var lw = EditorGUIUtility.labelWidth;
  73.                     SetLabelWidth(50f, true);
  74.                     rlist[GetPropPath(prop)].DoList(rect);
  75.                     EditorGUIUtility.labelWidth = lw;
  76.                 }
  77.             }
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Return array property rect height
  82.         /// </summary>
  83.         /// <param name="prop"></param>
  84.         /// <returns></returns>
  85.         public static float GetArrayHeight(SerializedProperty prop) {
  86.             if (rlist.ContainsKey(GetPropPath(prop))) {
  87.                 if (!GetFoldState(rlistFold[GetPropPath(prop)]))
  88.                     return EditorGUIUtility.singleLineHeight;
  89.                 return rlist[GetPropPath(prop)].GetHeight();
  90.             }
  91.             return 0f;
  92.         }
  93.  
  94.         public static string GetPropPath(SerializedProperty prop) {
  95.             return $"{prop.serializedObject.targetObject.name}.{prop.propertyPath}";
  96.         }
  97.  
  98.         #endregion
  99. ///////////////////////////////////////////////////
  100. // Реализация проперти дровера
  101. ///////////////////////////////////////////////////
  102. using System.Collections.Generic;
  103. using EasyEditorGUI;
  104. using SO;
  105. using UnityEditor;
  106. using UnityEditorInternal;
  107. using UnityEngine;
  108.  
  109. [CustomPropertyDrawer(typeof(Materials))]
  110. public class MaterialsPropertyDrawer : PropertyDrawer {
  111.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
  112.         var p = property.FindPropertyRelative("paths");
  113.         eGUI.ShowArray(p, position);
  114.     }
  115.  
  116.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
  117.         var p = property.FindPropertyRelative("paths");
  118.         eGUI.AddArray(p, label.text);
  119.         return eGUI.GetArrayHeight(p);
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement