源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

101 行
3.5 KiB

  1. using UnityEditor;
  2. using UnityEngine.UI;
  3. using UnityEngine;
  4. public class RaycastTargetChecker : EditorWindow
  5. {
  6. private MaskableGraphic[] graphics;
  7. private bool hideUnchecked = false;
  8. private bool showBorders = true;
  9. private Color borderColor = Color.blue;
  10. private Vector2 scrollPosition = Vector2.zero;
  11. private static RaycastTargetChecker instance = null;
  12. [MenuItem("Tools/RaycastTarget Checker")]
  13. private static void Open()
  14. {
  15. instance = instance ?? EditorWindow.GetWindow<RaycastTargetChecker>("RaycastTargets");
  16. instance.Show();
  17. }
  18. void OnEnable()
  19. {
  20. instance = this;
  21. }
  22. void OnDisable()
  23. {
  24. instance = null;
  25. }
  26. void OnGUI()
  27. {
  28. using (EditorGUILayout.HorizontalScope horizontalScope = new EditorGUILayout.HorizontalScope())
  29. {
  30. showBorders = EditorGUILayout.Toggle("Show Gizmos", showBorders, GUILayout.Width(200.0f));
  31. borderColor = EditorGUILayout.ColorField(borderColor);
  32. }
  33. hideUnchecked = EditorGUILayout.Toggle("Hide Unchecked", hideUnchecked);
  34. GUILayout.Space(12.0f);
  35. Rect rect = GUILayoutUtility.GetLastRect();
  36. GUI.color = new Color(0.0f, 0.0f, 0.0f, 0.25f);
  37. GUI.DrawTexture(new Rect(0.0f, rect.yMin + 6.0f, Screen.width, 4.0f), EditorGUIUtility.whiteTexture);
  38. GUI.DrawTexture(new Rect(0.0f, rect.yMin + 6.0f, Screen.width, 1.0f), EditorGUIUtility.whiteTexture);
  39. GUI.DrawTexture(new Rect(0.0f, rect.yMin + 9.0f, Screen.width, 1.0f), EditorGUIUtility.whiteTexture);
  40. GUI.color = Color.white;
  41. graphics = GameObject.FindObjectsOfType<MaskableGraphic>();
  42. using (GUILayout.ScrollViewScope scrollViewScope = new GUILayout.ScrollViewScope(scrollPosition))
  43. {
  44. scrollPosition = scrollViewScope.scrollPosition;
  45. for (int i = 0; i < graphics.Length; i++)
  46. {
  47. MaskableGraphic graphic = graphics[i];
  48. if (hideUnchecked == false || graphic.raycastTarget == true)
  49. {
  50. DrawElement(graphic);
  51. }
  52. }
  53. }
  54. foreach (var item in graphics)
  55. {
  56. EditorUtility.SetDirty(item);
  57. }
  58. Repaint();
  59. }
  60. private void DrawElement(MaskableGraphic graphic)
  61. {
  62. using (EditorGUILayout.HorizontalScope horizontalScope = new EditorGUILayout.HorizontalScope())
  63. {
  64. Undo.RecordObject(graphic, "Modify RaycastTarget");
  65. graphic.raycastTarget = EditorGUILayout.Toggle(graphic.raycastTarget, GUILayout.Width(20));
  66. EditorGUI.BeginDisabledGroup(true);
  67. EditorGUILayout.ObjectField(graphic, typeof(MaskableGraphic), true);
  68. EditorGUI.EndDisabledGroup();
  69. }
  70. }
  71. [DrawGizmo(GizmoType.Selected | GizmoType.NonSelected)]
  72. private static void DrawGizmos(MaskableGraphic source, GizmoType gizmoType)
  73. {
  74. if (instance != null && instance.showBorders == true && source.raycastTarget == true)
  75. {
  76. Vector3[] corners = new Vector3[4];
  77. source.rectTransform.GetWorldCorners(corners);
  78. Gizmos.color = instance.borderColor;
  79. for (int i = 0; i < 4; i++)
  80. {
  81. Gizmos.DrawLine(corners[i], corners[(i + 1) % 4]);
  82. }
  83. if (Selection.activeGameObject == source.gameObject)
  84. {
  85. Gizmos.DrawLine(corners[0], corners[2]);
  86. Gizmos.DrawLine(corners[1], corners[3]);
  87. }
  88. }
  89. SceneView.RepaintAll();
  90. }
  91. }