Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Универсальный фолд
- private static readonly Dictionary<object, bool> _foldouts2 = new Dictionary<object, bool>();
- public static bool ShowFold(string text, object @object) {
- var result = false;
- if (!_foldouts2.ContainsKey(@object))
- _foldouts2.Add(@object, false);
- else
- result = _foldouts2[@object];
- result = EditorGUILayout.Foldout(result, text, true);
- _foldouts2[@object] = result;
- return result;
- }
- public static bool ShowFold(Rect rect, string text, object @object, bool simply = true) {
- var result = false;
- if (!_foldouts2.ContainsKey(@object))
- _foldouts2.Add(@object, false);
- else
- result = _foldouts2[@object];
- result = EditorGUI.Foldout(rect, result, text, simply);
- _foldouts2[@object] = result;
- return result;
- }
- public static bool GetFoldState(object @object) {
- if (!_foldouts2.ContainsKey(@object)) return false;
- return _foldouts2[@object];
- }
- #endregion
- #region Reorderable list
- private static readonly Dictionary<string, ReorderableList> rlist = new Dictionary<string, ReorderableList>();
- private static readonly Dictionary<string, string> rlistTitle = new Dictionary<string, string>();
- private static readonly Dictionary<string, object> rlistFold = new Dictionary<string, object>();
- /// <summary>
- /// Add array property to Reorderable array
- /// </summary>
- /// <param name="prop"></param>
- /// <param name="title"></param>
- public static void AddArray(SerializedProperty prop, string title, bool draggable = true, bool add = true) {
- if (prop == null) return;
- if (!rlist.ContainsKey(GetPropPath(prop)) || rlist[GetPropPath(prop)].serializedProperty != prop) {
- string s = "{0:D1}";
- var rl = new ReorderableList(prop.serializedObject, prop, draggable, false, add, add) {
- drawHeaderCallback = (Rect rect) => { EditorGUI.LabelField(rect, title); },
- drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => {
- EditorGUI.PropertyField(rect, prop.GetArrayElementAtIndex(index), new GUIContent(string.Format(s, index)));
- }
- };
- rlist[GetPropPath(prop)] = rl;
- rlistTitle[GetPropPath(prop)] = title;
- if (!rlistFold.ContainsKey(GetPropPath(prop)))
- rlistFold[GetPropPath(prop)] = new object();
- }
- }
- /// <summary>
- /// Show array property at Rect
- /// </summary>
- /// <param name="prop"></param>
- /// <param name="rect"></param>
- public static void ShowArray(SerializedProperty prop, Rect rect) {
- if (rlist.ContainsKey(GetPropPath(prop))) {
- rect.height = EditorGUIUtility.singleLineHeight;
- if (ShowFold(rect, rlistTitle[GetPropPath(prop)], rlistFold[GetPropPath(prop)])) {
- rect.height = GetArrayHeight(prop);
- var lw = EditorGUIUtility.labelWidth;
- SetLabelWidth(50f, true);
- rlist[GetPropPath(prop)].DoList(rect);
- EditorGUIUtility.labelWidth = lw;
- }
- }
- }
- /// <summary>
- /// Return array property rect height
- /// </summary>
- /// <param name="prop"></param>
- /// <returns></returns>
- public static float GetArrayHeight(SerializedProperty prop) {
- if (rlist.ContainsKey(GetPropPath(prop))) {
- if (!GetFoldState(rlistFold[GetPropPath(prop)]))
- return EditorGUIUtility.singleLineHeight;
- return rlist[GetPropPath(prop)].GetHeight();
- }
- return 0f;
- }
- public static string GetPropPath(SerializedProperty prop) {
- return $"{prop.serializedObject.targetObject.name}.{prop.propertyPath}";
- }
- #endregion
- ///////////////////////////////////////////////////
- // Реализация проперти дровера
- ///////////////////////////////////////////////////
- using System.Collections.Generic;
- using EasyEditorGUI;
- using SO;
- using UnityEditor;
- using UnityEditorInternal;
- using UnityEngine;
- [CustomPropertyDrawer(typeof(Materials))]
- public class MaterialsPropertyDrawer : PropertyDrawer {
- public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
- var p = property.FindPropertyRelative("paths");
- eGUI.ShowArray(p, position);
- }
- public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
- var p = property.FindPropertyRelative("paths");
- eGUI.AddArray(p, label.text);
- return eGUI.GetArrayHeight(p);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement