TooltipInspector.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEditorInternal;
  6. [CustomEditor(typeof(Modules_TooltipCtrl))]
  7. public class TooltipInspector : Editor
  8. {
  9. private ReorderableList _tooltipList;
  10. private void OnEnable()
  11. {
  12. _tooltipList = new ReorderableList(serializedObject, serializedObject.FindProperty("tooltipList")
  13. , true, true, true, true);
  14. //自定义列表名称
  15. _tooltipList.drawHeaderCallback = (Rect rect) =>
  16. {
  17. GUI.Label(rect, "Toottip List");
  18. };
  19. //定义元素的高度
  20. //_tooltipList.elementHeight = 68;
  21. //自定义绘制列表元素
  22. _tooltipList.drawElementCallback = (Rect rect, int index, bool selected, bool focused) =>
  23. {
  24. //根据index获取对应元素
  25. SerializedProperty item = _tooltipList.serializedProperty.GetArrayElementAtIndex(index);
  26. rect.height -= 4;
  27. rect.y += 2;
  28. EditorGUI.PropertyField(rect, item, new GUIContent("Index " + index));
  29. };
  30. //点击添加按钮的同时添加一个Tooltip
  31. _tooltipList.onAddDropdownCallback = (Rect rect, ReorderableList list) =>
  32. {
  33. int index = _tooltipList.serializedProperty.arraySize;
  34. _tooltipList.serializedProperty.arraySize++;
  35. _tooltipList.index = index;
  36. SerializedProperty element = _tooltipList.serializedProperty.GetArrayElementAtIndex(index);
  37. Modules_TooltipCtrl tooltipCtrl = (Modules_TooltipCtrl)target;
  38. GameObject tooltip = Resources.Load<GameObject>("Prefabs/Tooltip");
  39. tooltip = tooltipCtrl.InstantiateNewTooltip(tooltip);
  40. SerializedProperty prefabPreperty = element.FindPropertyRelative("tooltip");
  41. prefabPreperty.objectReferenceValue = tooltip;
  42. SerializedProperty trigger = element.FindPropertyRelative("trigger");
  43. trigger.enumValueIndex = (int)Modules_TooltipCtrl.TooltipShowTrigger.AlwaysShow;
  44. serializedObject.ApplyModifiedProperties();
  45. };
  46. _tooltipList.onRemoveCallback = (ReorderableList list) =>
  47. {
  48. //当删除元素时候的回调函数,实现删除元素时,有提示框跳出
  49. if (EditorUtility.DisplayDialog("Warnning", "Do you want to remove this element?", "Remove", "Cancel"))
  50. {
  51. SerializedProperty element = _tooltipList.serializedProperty.GetArrayElementAtIndex(list.index);
  52. SerializedProperty prefabPreperty = element.FindPropertyRelative("tooltip");
  53. GameObject obj = (GameObject)prefabPreperty.objectReferenceValue;
  54. DestroyImmediate(obj);
  55. ReorderableList.defaultBehaviours.DoRemoveButton(list);
  56. }
  57. };
  58. }
  59. public override void OnInspectorGUI()
  60. {
  61. serializedObject.Update();
  62. //自动布局绘制列表
  63. _tooltipList.DoLayoutList();
  64. serializedObject.ApplyModifiedProperties();
  65. }
  66. }