Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if UNITY_EDITOR
- internal virtual Rect GUIDrawElement(Rect rect, SerializedProperty serializedProperty, bool isActive, bool isFocused)
- {
- if (serializedProperty == null)
- {
- EditorGUI.HelpBox(rect, "SerializedProperty was null!", MessageType.Error);
- return rect;
- }
- serializedProperty = serializedProperty.Copy();
- int startingDepth = serializedProperty.depth;
- var currentRect = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
- EditorGUI.LabelField(currentRect, serializedProperty.boxedValue.GetType().Name);
- var first = true;
- while (serializedProperty.NextVisible(EnterChildren(serializedProperty, first)) && serializedProperty.depth > startingDepth)
- {
- first = false;
- currentRect = currentRect.MoveDownFor(EditorGUI.GetPropertyHeight(serializedProperty));
- if (serializedProperty.propertyType == SerializedPropertyType.Generic && !serializedProperty.isArray)
- currentRect = currentRect.CutFromTop(EditorGUIUtility.singleLineHeight)[0];
- EditorGUI.indentLevel = serializedProperty.depth;
- EditorGUI.PropertyField(currentRect, serializedProperty);
- }
- return currentRect;
- static bool EnterChildren(SerializedProperty serializedProperty, bool first)
- {
- return first || (
- serializedProperty.propertyType == SerializedPropertyType.Generic
- && !serializedProperty.isArray
- && serializedProperty.isExpanded
- );
- }
- }
- internal virtual float GUIElementHeight(SerializedProperty serializedProperty)
- {
- serializedProperty = serializedProperty.Copy();
- int startingDepth = serializedProperty.depth;
- float height = EditorGUIUtility.singleLineHeight * 2;
- var first = true;
- while (serializedProperty.NextVisible(first) && serializedProperty.depth > startingDepth)
- {
- first = false;
- height += EditorGUI.GetPropertyHeight(serializedProperty);
- }
- return height;
- }
- #endif
Advertisement
Comments
-
- I've used it to draw abstract class in ReorderableList, that can overloade this method just in case it needs some specific thing drawn.
-
- TOTEST Found this on https://discussions.unity.com/t/what-is-the-best-way-to-do-a-reorderable-list/687256:
- using UnityEngine;
- using UnityEditor;
- using UnityEditorInternal;
- using System.Collections.Generic;
- [CustomEditor(typeof(UnityEngine.Object), true, isFallback = true)]
- [CanEditMultipleObjects]
- public class CustomEditorBase : Editor
- {
- private Dictionary<string, ReorderableListProperty> reorderableLists;
- protected virtual void OnEnable()
- {
- this.reorderableLists = new Dictionary<string, ReorderableListProperty>(10);
- }
- ~CustomEditorBase()
- {
- this.reorderableLists.Clear();
- this.reorderableLists = null;
- }
- public override void OnInspectorGUI()
- {
- EditorGUILayout.LabelField("Custom Editor", EditorStyles.centeredGreyMiniLabel);
- Color cachedGuiColor = GUI.color;
- serializedObject.Update();
- var property = serializedObject.GetIterator();
- var next = property.NextVisible(true);
- if (next)
- do
- {
- GUI.color = cachedGuiColor;
- this.HandleProperty(property);
- } while (property.NextVisible(false));
- serializedObject.ApplyModifiedProperties();
- }
- protected void HandleProperty(SerializedProperty property)
- {
- //Debug.LogFormat("name: {0}, displayName: {1}, type: {2}, propertyType: {3}, path: {4}", property.name, property.displayName, property.type, property.propertyType, property.propertyPath);
- bool isdefaultScriptProperty = property.name.Equals("m_Script") && property.type.Equals("PPtr<MonoScript>") && property.propertyType == SerializedPropertyType.ObjectReference && property.propertyPath.Equals("m_Script");
- bool cachedGUIEnabled = GUI.enabled;
- if (isdefaultScriptProperty)
- GUI.enabled = false;
- //var attr = this.GetPropertyAttributes(property);
- if (property.isArray && property.propertyType != SerializedPropertyType.String)
- this.HandleArray(property);
- else
- EditorGUILayout.PropertyField(property, property.isExpanded);
- if (isdefaultScriptProperty)
- GUI.enabled = cachedGUIEnabled;
- }
- protected void HandleArray(SerializedProperty property)
- {
- var listData = this.GetReorderableList(property);
- // if (!property.isExpanded)
- // {
- // EditorGUILayout.BeginHorizontal(EditorStyles.foldout);
- // property.isExpanded = EditorGUILayout.ToggleLeft(string.Format("{0}[]", property.displayName), property.isExpanded, EditorStyles.boldLabel);
- // EditorGUILayout.LabelField(string.Format("size: {0}", property.arraySize));
- // EditorGUILayout.EndHorizontal();
- // }
- // else {
- listData.List.DoLayoutList();
- // }
- }
- protected object[] GetPropertyAttributes(SerializedProperty property)
- {
- return this.GetPropertyAttributes<PropertyAttribute>(property);
- }
- protected object[] GetPropertyAttributes<T>(SerializedProperty property) where T : System.Attribute
- {
- System.Reflection.BindingFlags bindingFlags = System.Reflection.BindingFlags.GetField
- | System.Reflection.BindingFlags.GetProperty
- | System.Reflection.BindingFlags.IgnoreCase
- | System.Reflection.BindingFlags.Instance
- | System.Reflection.BindingFlags.NonPublic
- | System.Reflection.BindingFlags.Public;
- if (property.serializedObject.targetObject == null)
- return null;
- var targetType = property.serializedObject.targetObject.GetType();
- var field = targetType.GetField(property.name, bindingFlags);
- if (field != null)
- return field.GetCustomAttributes(typeof(T), true);
- return null;
- }
- private ReorderableListProperty GetReorderableList(SerializedProperty property)
- {
- ReorderableListProperty ret = null;
- if (this.reorderableLists.TryGetValue(property.name, out ret))
- {
- ret.Property = property;
- return ret;
- }
- ret = new ReorderableListProperty(property);
- this.reorderableLists.Add(property.name, ret);
- return ret;
- }
- #region Inner-class ReorderableListProperty
- private class ReorderableListProperty
- {
- /// <summary>
- /// ref http://va.lent.in/unity-make-your-lists-functional-with-reorderablelist/
- /// </summary>
- public ReorderableList List { get; private set; }
- private SerializedProperty _property;
- public SerializedProperty Property
- {
- get { return this._property; }
- set
- {
- this._property = value;
- this.List.serializedProperty = this._property;
- }
- }
- public ReorderableListProperty(SerializedProperty property)
- {
- this._property = property;
- this.CreateList();
- }
- ~ReorderableListProperty()
- {
- this._property = null;
- this.List = null;
- }
- private void CreateList()
- {
- bool dragable = true, header = true, add = true, remove = true;
- this.List = new ReorderableList(this.Property.serializedObject, this.Property, dragable, header, add, remove);
- this.List.drawHeaderCallback += rect => EditorGUI.LabelField (rect, this._property.displayName);// EditorGUI.ToggleLeft(rect, this._property.displayName, this._property.isExpanded, EditorStyles.boldLabel);
- this.List.onCanRemoveCallback += (list) => { return this.List.count > 0; };
- this.List.drawElementCallback += this.drawElement;
- this.List.elementHeightCallback += (idx) => { return Mathf.Max(EditorGUIUtility.singleLineHeight, EditorGUI.GetPropertyHeight(this._property.GetArrayElementAtIndex(idx), GUIContent.none, true)) + 4.0f; };
- }
- private void drawElement(Rect rect, int index, bool active, bool focused)
- {
- if (this._property.GetArrayElementAtIndex(index).propertyType == SerializedPropertyType.Generic)
- {
- EditorGUI.LabelField(rect, this._property.GetArrayElementAtIndex(index).displayName);
- }
- //rect.height = 16;
- rect.height = EditorGUI.GetPropertyHeight(this._property.GetArrayElementAtIndex(index), GUIContent.none, true);
- rect.y += 1;
- EditorGUI.PropertyField(rect, this._property.GetArrayElementAtIndex(index), GUIContent.none, true);
- this.List.elementHeight = rect.height + 4.0f;
- }
- }
- #endregion
- }
Add Comment
Please, Sign In to add comment
Advertisement