1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| using UnityEngine; using UnityEditor;
[CustomEditor(typeof(PathComponent))] public class PathComponentEditor : Editor { private PathComponent path; private SerializedProperty waypointsProperty; private SerializedProperty loopProperty; private SerializedProperty pathColorProperty;
void OnEnable() { path = (PathComponent)target; waypointsProperty = serializedObject.FindProperty("waypoints"); loopProperty = serializedObject.FindProperty("loop"); pathColorProperty = serializedObject.FindProperty("pathColor"); }
public override void OnInspectorGUI() { serializedObject.Update();
EditorGUILayout.LabelField("路径编辑器", EditorStyles.boldLabel); EditorGUILayout.Space();
EditorGUILayout.PropertyField(loopProperty); EditorGUILayout.PropertyField(pathColorProperty); EditorGUILayout.PropertyField(waypointsProperty, true);
if (path.waypoints != null && path.waypoints.Length > 1) { float length = CalculatePathLength(); EditorGUILayout.HelpBox($"路径总长度: {length:F2} 单位", MessageType.Info); }
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("添加当前相机位置")) { AddWaypointFromCamera(); } if (GUILayout.Button("清除所有点")) { if (EditorUtility.DisplayDialog("确认", "确定要清除所有路径点吗?", "确定", "取消")) { waypointsProperty.ClearArray(); serializedObject.ApplyModifiedProperties(); } } EditorGUILayout.EndHorizontal();
serializedObject.ApplyModifiedProperties(); }
void OnSceneGUI() { if (path.waypoints == null || path.waypoints.Length == 0) return;
Handles.color = path.pathColor;
for (int i = 0; i < path.waypoints.Length - 1; i++) { Handles.DrawDottedLine( path.waypoints[i], path.waypoints[i + 1], 2f ); }
if (path.loop && path.waypoints.Length > 2) { Handles.DrawDottedLine( path.waypoints[path.waypoints.Length - 1], path.waypoints[0], 2f ); }
for (int i = 0; i < path.waypoints.Length; i++) { EditorGUI.BeginChangeCheck();
Vector3 newPosition = Handles.PositionHandle( path.waypoints[i], Quaternion.identity );
Handles.Label( path.waypoints[i] + Vector3.up * 0.5f, $"<color=yellow>{i}</color>" );
if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(path, "Move Waypoint"); path.waypoints[i] = newPosition; EditorUtility.SetDirty(path); } }
DrawPathDirection(); }
private float CalculatePathLength() { if (path.waypoints.Length < 2) return 0f;
float length = 0f; for (int i = 0; i < path.waypoints.Length - 1; i++) { length += Vector3.Distance(path.waypoints[i], path.waypoints[i + 1]); }
if (path.loop && path.waypoints.Length > 2) { length += Vector3.Distance( path.waypoints[path.waypoints.Length - 1], path.waypoints[0] ); }
return length; }
private void AddWaypointFromCamera() { Vector3 cameraPos = SceneView.lastActiveSceneView.camera.transform.position; waypointsProperty.arraySize++; waypointsProperty.GetArrayElementAtIndex(waypointsProperty.arraySize - 1).vector3Value = cameraPos; serializedObject.ApplyModifiedProperties(); }
private void DrawPathDirection() { if (path.waypoints.Length < 2) return;
Handles.color = Color.yellow;
for (int i = 0; i < path.waypoints.Length - 1; i++) { Vector3 dir = (path.waypoints[i + 1] - path.waypoints[i]).normalized; Vector3 mid = (path.waypoints[i] + path.waypoints[i + 1]) * 0.5f; Handles.ArrowCap(0, mid, Quaternion.LookRotation(dir), 0.5f); }
if (path.loop && path.waypoints.Length > 2) { Vector3 dir = (path.waypoints[0] - path.waypoints[path.waypoints.Length - 1]).normalized; Vector3 mid = (path.waypoints[0] + path.waypoints[path.waypoints.Length - 1]) * 0.5f; Handles.ArrowCap(0, mid, Quaternion.LookRotation(dir), 0.5f); } } }
|