private void load_road_data() { this.road_data_set = new road_data[this.monster_roads.Length]; for (int i = 0; i < this.road_data_set.Length; i++) { this.road_data_set[i].path_sideA = WaypointManager.GetCurved(this.monster_roads[i].GetPathPoints()); //复制sideB路径点 int len = this.road_data_set[i].path_sideA.Length; this.road_data_set[i].path_sideB = new Vector3[len]; for (int j = 0; j < len; j++) { this.road_data_set[i].path_sideB[len - j - 1] = this.road_data_set[i].path_sideA[j]; } } }
private void LoadRoadData() { this._roadDataSet = new RoadData[this.monsterRoads.Length]; for (int i = 0; i < this._roadDataSet.Length; i++) { this._roadDataSet[i].pathSideA = WaypointManager.GetCurved(this .monsterRoads[i].GetPathPoints()); //复制SideB的路径 int len = this._roadDataSet[i].pathSideA.Length; this._roadDataSet[i].pathSideB = new Vector3[len]; for (int j = 0; j < len; j++) { this._roadDataSet[i].pathSideB[len - 1 - j] = this ._roadDataSet[i].pathSideA[j]; } } }
//if this path is selected, display small info boxes above all waypoint positions //also display handles for the waypoints void OnSceneGUI() { //again, get waypoint array var waypoints = GetWaypointArray(); //do not execute further code if we have no waypoints defined //(just to make sure, practically this can not occur) if (waypoints.Length == 0) { return; } Vector3 wpPos = Vector3.zero; float size = 1f; //loop through waypoint array for (int i = 0; i < waypoints.Length; i++) { if (!waypoints[i]) { continue; } wpPos = waypoints[i].position; size = HandleUtility.GetHandleSize(wpPos) * 0.4f; //do not draw waypoint header if too far away if (size < 3f) { //begin 2D GUI block Handles.BeginGUI(); //translate waypoint vector3 position in world space into a position on the screen var guiPoint = HandleUtility.WorldToGUIPoint(wpPos); //create rectangle with that positions and do some offset var rect = new Rect(guiPoint.x - 50.0f, guiPoint.y - 40, 100, 20); //draw box at position with current waypoint name GUI.Box(rect, waypoints[i].name); Handles.EndGUI(); //end GUI block } //draw handles per waypoint, clamp size Handles.color = color2.colorValue; size = Mathf.Clamp(size, 0, 1.2f); #if UNITY_5_6_OR_NEWER Handles.FreeMoveHandle(wpPos, Quaternion.identity, size, Vector3.zero, (controlID, position, rotation, hSize, eventType) => { Handles.SphereHandleCap(controlID, position, rotation, hSize, eventType); if (controlID == GUIUtility.hotControl && GUIUtility.hotControl != 0) { activeNode = i; } }); #else Handles.FreeMoveHandle(wpPos, Quaternion.identity, size, Vector3.zero, (controlID, position, rotation, hSize) => { Handles.SphereCap(controlID, position, rotation, hSize); if (controlID == GUIUtility.hotControl && GUIUtility.hotControl != 0) { activeNode = i; } }); #endif Handles.RadiusHandle(waypoints[i].rotation, wpPos, size / 2); } if (activeNode > -1) { wpPos = waypoints[activeNode].position; Quaternion wpRot = waypoints[activeNode].rotation; switch (Tools.current) { case Tool.Move: if (Tools.pivotRotation == PivotRotation.Global) { wpRot = Quaternion.identity; } Vector3 newPos = Handles.PositionHandle(wpPos, wpRot); if (wpPos != newPos) { Undo.RecordObject(waypoints[activeNode], "Move Handle"); waypoints[activeNode].position = newPos; } break; case Tool.Rotate: Quaternion newRot = Handles.RotationHandle(wpRot, wpPos); if (wpRot != newRot) { Undo.RecordObject(waypoints[activeNode], "Rotate Handle"); waypoints[activeNode].rotation = newRot; } break; } } //waypoint direction handles drawing Vector3[] pathPoints = new Vector3[waypoints.Length + 1]; for (int i = 0; i < pathPoints.Length - 1; i++) { pathPoints[i] = waypoints[i].position; } pathPoints[pathPoints.Length - 1] = waypoints[0].position; //create list of path segments (list of Vector3 list) List <List <Vector3> > segments = new List <List <Vector3> >(); int curIndex = 0; float lerpVal = 0f; //differ between linear and curved display switch (check1.boolValue) { case true: //convert waypoints to curved path points pathPoints = WaypointManager.GetCurved(pathPoints); //calculate approximate path point amount per segment int detail = Mathf.FloorToInt((pathPoints.Length - 1f) / (waypoints.Length - 1f)); for (int i = 0; i < waypoints.Length - 1; i++) { float dist = Mathf.Infinity; //loop over path points to find single segments segments.Add(new List <Vector3>()); //we are not checking for absolute path points on standard paths, because //path points could also be located before or after waypoint positions. //instead a minimum distance is searched which marks the nearest path point for (int j = curIndex; j < pathPoints.Length; j++) { //add path point to current segment segments[i].Add(pathPoints[j]); //start looking for distance after a certain amount of path points of this segment if (j >= (i + 1) * detail) { //calculate distance of current path point to waypoint float pointDist = Vector3.Distance(waypoints[i].position, pathPoints[j]); //we are getting closer to the waypoint if (pointDist < dist) { dist = pointDist; } else { //current path point is more far away than the last one //the segment ends here, continue with new segment curIndex = j + 1; break; } } } } break; case false: //detail for arrows between waypoints int lerpMax = 16; //loop over waypoints to add intermediary points for (int i = 0; i < waypoints.Length - 1; i++) { segments.Add(new List <Vector3>()); for (int j = 0; j < lerpMax; j++) { //linear lerp between waypoints to get additional points for drawing arrows at segments[i].Add(Vector3.Lerp(pathPoints[i], pathPoints[i + 1], j / (float)lerpMax)); } } break; } //loop over segments for (int i = 0; i < segments.Count; i++) { //loop over single positions on the segment for (int j = 0; j < segments[i].Count; j++) { //get current lerp value for interpolating rotation //draw arrow handle on current position with interpolated rotation lerpVal = j / (float)segments[i].Count; #if UNITY_5_6_OR_NEWER Handles.SphereHandleCap(0, segments[i][j], Quaternion.Lerp(waypoints[i].rotation, waypoints[i + 1].rotation, lerpVal), 0.35f, EventType.Repaint); #else Handles.SphereCap(0, segments[i][j], Quaternion.Lerp(waypoints[i].rotation, waypoints[i + 1].rotation, lerpVal), size); #endif } } }