123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using UnityEditorInternal;
- [CustomEditor(typeof(Modules_TooltipCtrl))]
- public class TooltipInspector : Editor
- {
- private ReorderableList _tooltipList;
- private void OnEnable()
- {
- _tooltipList = new ReorderableList(serializedObject, serializedObject.FindProperty("tooltipList")
- , true, true, true, true);
- //自定义列表名称
- _tooltipList.drawHeaderCallback = (Rect rect) =>
- {
- GUI.Label(rect, "Toottip List");
- };
- //定义元素的高度
- //_tooltipList.elementHeight = 68;
- //自定义绘制列表元素
- _tooltipList.drawElementCallback = (Rect rect, int index, bool selected, bool focused) =>
- {
- //根据index获取对应元素
- SerializedProperty item = _tooltipList.serializedProperty.GetArrayElementAtIndex(index);
- rect.height -= 4;
- rect.y += 2;
- EditorGUI.PropertyField(rect, item, new GUIContent("Index " + index));
- };
- //点击添加按钮的同时添加一个Tooltip
- _tooltipList.onAddDropdownCallback = (Rect rect, ReorderableList list) =>
- {
- int index = _tooltipList.serializedProperty.arraySize;
- _tooltipList.serializedProperty.arraySize++;
- _tooltipList.index = index;
- SerializedProperty element = _tooltipList.serializedProperty.GetArrayElementAtIndex(index);
- Modules_TooltipCtrl tooltipCtrl = (Modules_TooltipCtrl)target;
- GameObject tooltip = Resources.Load<GameObject>("Prefabs/Tooltip");
- tooltip = tooltipCtrl.InstantiateNewTooltip(tooltip);
- SerializedProperty prefabPreperty = element.FindPropertyRelative("tooltip");
- prefabPreperty.objectReferenceValue = tooltip;
- SerializedProperty trigger = element.FindPropertyRelative("trigger");
- trigger.enumValueIndex = (int)Modules_TooltipCtrl.TooltipShowTrigger.AlwaysShow;
- serializedObject.ApplyModifiedProperties();
- };
- _tooltipList.onRemoveCallback = (ReorderableList list) =>
- {
- //当删除元素时候的回调函数,实现删除元素时,有提示框跳出
- if (EditorUtility.DisplayDialog("Warnning", "Do you want to remove this element?", "Remove", "Cancel"))
- {
- SerializedProperty element = _tooltipList.serializedProperty.GetArrayElementAtIndex(list.index);
- SerializedProperty prefabPreperty = element.FindPropertyRelative("tooltip");
- GameObject obj = (GameObject)prefabPreperty.objectReferenceValue;
- DestroyImmediate(obj);
- ReorderableList.defaultBehaviours.DoRemoveButton(list);
- }
- };
- }
- public override void OnInspectorGUI()
- {
- serializedObject.Update();
- //自动布局绘制列表
- _tooltipList.DoLayoutList();
- serializedObject.ApplyModifiedProperties();
- }
- }
|