DragAreaGUI.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. namespace XGame.Editor.ObjectCollection
  5. {
  6. public class DragAreaGUI
  7. {
  8. public static UnityEngine.Object ObjectField(Rect position, Type objType, Type[] blackTypes = null)
  9. {
  10. return ObjectField(position, objType, null, null, null, blackTypes);
  11. }
  12. public static UnityEngine.Object ObjectField(Rect position, Type objType, GUIContent label)
  13. {
  14. return ObjectField(position, objType, label, null, null);
  15. }
  16. public static UnityEngine.Object ObjectField(Rect position, Type objType, string meg)
  17. {
  18. return ObjectField(position, objType, null, null, meg);
  19. }
  20. public static UnityEngine.Object ObjectField(Rect position, Type objType, string meg, GUIStyle style)
  21. {
  22. return ObjectField(position, objType, null, style, meg);
  23. }
  24. /// <summary>
  25. /// DragArea
  26. /// </summary>
  27. /// <param name="objectType">限定拖拽进来的类型</param>
  28. /// <param name="meg">显示信息</param>
  29. /// <returns>返回拖拽进来的Object,不符合条件为空</returns>
  30. public static UnityEngine.Object ObjectField(Rect position, Type objectType,
  31. GUIContent title = null, GUIStyle style = null, string meg = null, Type[] blackTypes = null)
  32. {
  33. Event aEvent;
  34. aEvent = Event.current;
  35. UnityEngine.Object temp = null;
  36. var dragArea = position;
  37. if (title == null)
  38. {
  39. if (string.IsNullOrEmpty(meg))
  40. {
  41. meg = "Drag here to add";
  42. }
  43. title = new GUIContent(meg + $"\n({objectType.Name})");
  44. }
  45. if (style == null)
  46. {
  47. style = new GUIStyle("Button")
  48. {
  49. alignment = TextAnchor.MiddleCenter,
  50. fontStyle = FontStyle.BoldAndItalic,
  51. fontSize = 12,
  52. };
  53. }
  54. GUI.Box(dragArea, title, style);
  55. switch (aEvent.type)
  56. {
  57. case EventType.DragUpdated:
  58. if (!dragArea.Contains(aEvent.mousePosition))
  59. {
  60. return null;
  61. }
  62. if (Selection.activeObject)
  63. {
  64. UnityEngine.Object judge = TryReadComponentFromGameObject(DragAndDrop.objectReferences[0], objectType, blackTypes);
  65. if (judge == null)
  66. {
  67. DragAndDrop.visualMode = DragAndDropVisualMode.None;
  68. }
  69. else
  70. {
  71. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  72. }
  73. }
  74. break;
  75. case EventType.DragPerform:
  76. if (!dragArea.Contains(aEvent.mousePosition))
  77. {
  78. return null;
  79. }
  80. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  81. if (aEvent.type == EventType.DragPerform)
  82. {
  83. if (Selection.activeObject)
  84. {
  85. DragAndDrop.AcceptDrag();
  86. temp = TryReadComponentFromGameObject(DragAndDrop.objectReferences[0], objectType, blackTypes);
  87. }
  88. }
  89. Event.current.Use();
  90. break;
  91. default:
  92. break;
  93. }
  94. return temp;
  95. }
  96. private static UnityEngine.Object TryReadComponentFromGameObject(UnityEngine.Object obj, Type objectType, Type[] blackTypes)
  97. {
  98. //Debug.Log($"TryReadComponentFromGameObject obj:{obj.name} objectType:{objectType} blackTypes:{blackTypes != null}");
  99. var go = obj as GameObject;
  100. if (blackTypes != null)
  101. {
  102. var comps = go.GetComponents(typeof(Component));
  103. foreach (var comp in comps)
  104. {
  105. foreach (var item in blackTypes)
  106. {
  107. if (item.IsAssignableFrom(comp.GetType()))
  108. {
  109. return null;
  110. }
  111. }
  112. }
  113. }
  114. if (go != null && objectType == typeof(GameObject))
  115. {
  116. return go;
  117. }
  118. if (go != null && objectType != null && objectType.IsSubclassOf(typeof(Component)))
  119. {
  120. var comps = go.GetComponents(typeof(Component));
  121. foreach (var comp in comps)
  122. {
  123. if (objectType.IsAssignableFrom(comp.GetType()))
  124. {
  125. return comp;
  126. }
  127. }
  128. }
  129. return null;
  130. }
  131. }
  132. }