DragAreaGUI.cs 4.5 KB

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