UIBigMapLineEditor.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using UnityEditorInternal;
  8. using UnityEditor.UI;
  9. [CustomEditor(typeof(UIBigMapLine))]
  10. public class UIBigMapLineEditor : GraphicEditor
  11. {
  12. protected SerializedProperty m_PassColor;
  13. protected SerializedProperty m_LineWidth;
  14. protected SerializedProperty m_LineSmooth;
  15. protected SerializedProperty m_Sprite;
  16. protected UIBigMapLine m_Line;
  17. // 为了不对外开放内部数据,则使用反射调用
  18. protected FieldInfo m_PassPointIdxFieldInfo = null;
  19. protected FieldInfo m_PointsFieldInfo = null;
  20. protected List<Vector2> m_Points;
  21. protected ReorderableList m_PointList;
  22. protected static int s_DropAreaHash = "UIBigMapLineDropArea".GetHashCode();
  23. protected override void OnEnable()
  24. {
  25. base.OnEnable();
  26. m_LineWidth = serializedObject.FindProperty("m_LineWidth");
  27. m_LineSmooth = serializedObject.FindProperty("m_LineSmooth");
  28. m_Sprite = serializedObject.FindProperty("m_Sprite");
  29. m_Line = target as UIBigMapLine;
  30. m_PassPointIdxFieldInfo = typeof(UIBigMapLine).GetField("m_PassPointIdx", BindingFlags.Instance | BindingFlags.NonPublic);
  31. m_PointsFieldInfo = typeof(UIBigMapLine).GetField("m_Points", BindingFlags.Instance | BindingFlags.NonPublic);
  32. m_Points = ((List<Vector2>)m_PointsFieldInfo.GetValue(m_Line));
  33. m_PointList = new ReorderableList(m_Points, typeof(Vector2), true, false, true, true);
  34. m_PointList.drawElementCallback = DrawElementCallback;
  35. m_PointList.onChangedCallback = OnChangedCallback;
  36. }
  37. public override void OnInspectorGUI()
  38. {
  39. serializedObject.Update();
  40. EditorGUI.BeginDisabledGroup(true);
  41. EditorGUILayout.PropertyField(m_Script);
  42. EditorGUI.EndDisabledGroup();
  43. EditorGUILayout.Space();
  44. EditorGUILayout.PropertyField(m_LineWidth, EditorGUIUtility.TrTextContent("线宽"));
  45. EditorGUILayout.PropertyField(m_LineSmooth, EditorGUIUtility.TrTextContent("线平滑度"));
  46. EditorGUILayout.PropertyField(m_Sprite, EditorGUIUtility.TrTextContent("Sprite"));
  47. if (m_LineWidth.floatValue < 0)
  48. {
  49. m_LineWidth.floatValue = 0.001f;
  50. }
  51. if (m_LineSmooth.intValue < 1)
  52. {
  53. m_LineSmooth.intValue = 1;
  54. }
  55. m_Line.passPointIdx = EditorGUILayout.IntSlider("通过到点", (int)m_PassPointIdxFieldInfo.GetValue(m_Line), 0, (m_Points != null ? m_Points.Count - 1 : 0));
  56. m_PointList.DoLayoutList();
  57. EditorGUILayout.Space();
  58. if (GUILayout.Button("All Points To Log"))
  59. {
  60. StringBuilder builder = new StringBuilder();
  61. for (int i = 0; i < m_Points.Count; i++)
  62. {
  63. builder.Append(m_Points[i].x);
  64. builder.Append(";");
  65. builder.Append(m_Points[i].y);
  66. builder.Append("\n");
  67. }
  68. builder.Append("\n");
  69. builder.Append("\n");
  70. Debug.LogError(builder.ToString());
  71. }
  72. EditorGUILayout.Space();
  73. if (serializedObject.ApplyModifiedProperties())
  74. {
  75. m_Line.SetAllDirty();
  76. }
  77. }
  78. public void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
  79. {
  80. Rect labelRect = new Rect(rect.x, rect.y, 50, rect.height);
  81. int id = GUIUtility.GetControlID(s_DropAreaHash, FocusType.Passive, labelRect);
  82. labelRect = EditorGUI.PrefixLabel(labelRect, id, GUIContent.none);
  83. Event evt = Event.current;
  84. if (evt.type == EventType.Repaint)
  85. {
  86. EditorStyles.label.Draw(labelRect, EditorGUIUtility.TrTextContent(index.ToString()), id);
  87. }
  88. if (evt.type == EventType.DragUpdated)
  89. {
  90. if (rect.Contains(evt.mousePosition))
  91. {
  92. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  93. }
  94. }
  95. if (evt.type == EventType.DragPerform)
  96. {
  97. if (rect.Contains(evt.mousePosition))
  98. {
  99. DragAndDrop.AcceptDrag();
  100. if (DragAndDrop.objectReferences != null && DragAndDrop.objectReferences.Length > 0)
  101. {
  102. GameObject go = DragAndDrop.objectReferences[0] as GameObject;
  103. if (go && go.activeInHierarchy)
  104. {
  105. m_Points[index] = m_Line.transform.InverseTransformPoint(go.transform.position);
  106. }
  107. }
  108. }
  109. }
  110. EditorGUI.LabelField(new Rect(rect.x, rect.y, 50, rect.height), index.ToString());
  111. Vector2 newPoint = EditorGUI.Vector2Field(new Rect(rect.x + 50, rect.y, rect.width - 100, rect.height), string.Empty, m_Points[index]);
  112. if (m_Points[index] != newPoint)
  113. {
  114. m_Points[index] = newPoint;
  115. m_Line.SetAllDirty();
  116. }
  117. if (GUI.Button(new Rect(rect.x + rect.width - 50, rect.y, 50, rect.height), "Copy"))
  118. {
  119. GUIUtility.systemCopyBuffer = m_Points[index].x + ";" + m_Points[index].y;
  120. }
  121. }
  122. public void OnChangedCallback(ReorderableList list)
  123. {
  124. m_Line.SetAllDirty();
  125. }
  126. }