public static GridPivot PivotPointSelector(GridPivot pivot) { // Find required styles gridPivotSelectBackground = gridPivotSelectBackground ?? AstarPathEditor.astarSkin.FindStyle("GridPivotSelectBackground"); gridPivotSelectButton = gridPivotSelectButton ?? AstarPathEditor.astarSkin.FindStyle("GridPivotSelectButton"); Rect r = GUILayoutUtility.GetRect(19, 19, gridPivotSelectBackground); // I have no idea why... but this is required for it to work well r.y -= 14; r.width = 19; r.height = 19; if (gridPivotSelectBackground == null) { return(pivot); } if (Event.current.type == EventType.Repaint) { gridPivotSelectBackground.Draw(r, false, false, false, false); } if (GUI.Toggle(new Rect(r.x, r.y, 7, 7), pivot == GridPivot.TopLeft, "", gridPivotSelectButton)) { pivot = GridPivot.TopLeft; } if (GUI.Toggle(new Rect(r.x + 12, r.y, 7, 7), pivot == GridPivot.TopRight, "", gridPivotSelectButton)) { pivot = GridPivot.TopRight; } if (GUI.Toggle(new Rect(r.x + 12, r.y + 12, 7, 7), pivot == GridPivot.BottomRight, "", gridPivotSelectButton)) { pivot = GridPivot.BottomRight; } if (GUI.Toggle(new Rect(r.x, r.y + 12, 7, 7), pivot == GridPivot.BottomLeft, "", gridPivotSelectButton)) { pivot = GridPivot.BottomLeft; } if (GUI.Toggle(new Rect(r.x + 6, r.y + 6, 7, 7), pivot == GridPivot.Center, "", gridPivotSelectButton)) { pivot = GridPivot.Center; } return(pivot); }
void DrawPositionField(GridGraph graph) { Vector3 pivotPoint; Vector3 diff; GUILayout.BeginHorizontal(); switch (pivot) { case GridPivot.Center: graph.center = RoundVector3(graph.center); graph.center = EditorGUILayout.Vector3Field("Center", graph.center); break; case GridPivot.TopLeft: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(0, 0, graph.depth)); pivotPoint = RoundVector3(pivotPoint); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Top-Left", pivotPoint); graph.center = pivotPoint - diff; break; case GridPivot.TopRight: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(graph.width, 0, graph.depth)); pivotPoint = RoundVector3(pivotPoint); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Top-Right", pivotPoint); graph.center = pivotPoint - diff; break; case GridPivot.BottomLeft: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(0, 0, 0)); pivotPoint = RoundVector3(pivotPoint); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Bottom-Left", pivotPoint); graph.center = pivotPoint - diff; break; case GridPivot.BottomRight: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(graph.width, 0, 0)); pivotPoint = RoundVector3(pivotPoint); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Bottom-Right", pivotPoint); graph.center = pivotPoint - diff; break; } graph.GenerateMatrix(); pivot = PivotPointSelector(pivot); GUILayout.EndHorizontal(); }
public static GridPivot PivotPointSelector(GridPivot pivot) { GUISkin skin = AstarPathEditor.astarSkin; GUIStyle background = skin.FindStyle("GridPivotSelectBackground"); Rect r = GUILayoutUtility.GetRect(19, 19, background); #if !UNITY_LE_4_3 // I have no idea... but it is required for it to work well r.y -= 14; #endif r.width = 19; r.height = 19; if (background == null) { return(pivot); } if (Event.current.type == EventType.Repaint) { background.Draw(r, false, false, false, false); } if (GUI.Toggle(new Rect(r.x, r.y, 7, 7), pivot == GridPivot.TopLeft, "", skin.FindStyle("GridPivotSelectButton"))) { pivot = GridPivot.TopLeft; } if (GUI.Toggle(new Rect(r.x + 12, r.y, 7, 7), pivot == GridPivot.TopRight, "", skin.FindStyle("GridPivotSelectButton"))) { pivot = GridPivot.TopRight; } if (GUI.Toggle(new Rect(r.x + 12, r.y + 12, 7, 7), pivot == GridPivot.BottomRight, "", skin.FindStyle("GridPivotSelectButton"))) { pivot = GridPivot.BottomRight; } if (GUI.Toggle(new Rect(r.x, r.y + 12, 7, 7), pivot == GridPivot.BottomLeft, "", skin.FindStyle("GridPivotSelectButton"))) { pivot = GridPivot.BottomLeft; } if (GUI.Toggle(new Rect(r.x + 6, r.y + 6, 7, 7), pivot == GridPivot.Center, "", skin.FindStyle("GridPivotSelectButton"))) { pivot = GridPivot.Center; } return(pivot); }
public Grid(int sizeX, int sizeY, float cellSize, Vector3 origin, GridPivot pivot, GridDrawPlane drawPlane, bool debug = false) { _size = new Vector2Int(sizeX, sizeY); _cells = new TGridObject[sizeX, sizeY]; _cellSize = cellSize; _origin = origin; _pivot = pivot; _drawPlane = drawPlane; if (debug) { DrawDebug(); } }
void DrawPositionField(GridGraph graph) { GUILayout.BeginHorizontal(); var normalizedPivotPoint = NormalizedPivotPoint(graph, pivot); var worldPoint = RoundVector3(graph.CalculateTransform().Transform(normalizedPivotPoint)); var newWorldPoint = EditorGUILayout.Vector3Field(ObjectNames.NicifyVariableName(pivot.ToString()), worldPoint); var delta = newWorldPoint - worldPoint; if (delta.magnitude > 0.001f) { graph.center += delta; } pivot = PivotPointSelector(pivot); GUILayout.EndHorizontal(); }
public Grid(int sizeX, int sizeY, float cellSize, Vector3 origin, GridPivot pivot, GridDrawPlane drawPlane, Func <int, int, TGridObject> creator, bool debug = false) { _size = new Vector2Int(sizeX, sizeY); _cells = new TGridObject[sizeX, sizeY]; _cellSize = cellSize; _origin = origin; _pivot = pivot; _drawPlane = drawPlane; _drawContainer = new GameObject("Grid Container").transform; //// create objects for (int x = 0; x < _size.x; x++) { for (int y = 0; y < _size.y; y++) { var go = creator(x, y); _cells[x, y] = go; if (go is IGridCellNeighbour goGCN) { ApplyNeighbour(goGCN, x, y); } if (go is IGridCellDrawer drawer) { var TileObject = GameObject.Instantiate(drawer.Prefab).transform; TileObject.SetParent(_drawContainer); TileObject.localPosition = Grid2World(x, y); } if (go is IGridCellWorldPos wp) { wp.SetWorldPos(Grid2World(x, y)); } } } if (debug) { DrawDebug(); } }
static Vector3 NormalizedPivotPoint(GridGraph graph, GridPivot pivot) { switch (pivot) { case GridPivot.Center: default: return(new Vector3(graph.width / 2f, 0, graph.depth / 2f)); case GridPivot.TopLeft: return(new Vector3(0, 0, graph.depth)); case GridPivot.TopRight: return(new Vector3(graph.width, 0, graph.depth)); case GridPivot.BottomLeft: return(new Vector3(0, 0, 0)); case GridPivot.BottomRight: return(new Vector3(graph.width, 0, 0)); } }
public static GridPivot PivotPointSelector(GridPivot pivot) { // Find required styles gridPivotSelectBackground = gridPivotSelectBackground ?? AstarPathEditor.astarSkin.FindStyle("GridPivotSelectBackground"); gridPivotSelectButton = gridPivotSelectButton ?? AstarPathEditor.astarSkin.FindStyle("GridPivotSelectButton"); Rect r = GUILayoutUtility.GetRect(19, 19, gridPivotSelectBackground); // I have no idea why... but this is required for it to work well r.y -= 14; r.width = 19; r.height = 19; if (gridPivotSelectBackground == null) { return pivot; } if (Event.current.type == EventType.Repaint) { gridPivotSelectBackground.Draw(r, false, false, false, false); } if (GUI.Toggle(new Rect(r.x, r.y, 7, 7), pivot == GridPivot.TopLeft, "", gridPivotSelectButton)) pivot = GridPivot.TopLeft; if (GUI.Toggle(new Rect(r.x+12, r.y, 7, 7), pivot == GridPivot.TopRight, "", gridPivotSelectButton)) pivot = GridPivot.TopRight; if (GUI.Toggle(new Rect(r.x+12, r.y+12, 7, 7), pivot == GridPivot.BottomRight, "", gridPivotSelectButton)) pivot = GridPivot.BottomRight; if (GUI.Toggle(new Rect(r.x, r.y+12, 7, 7), pivot == GridPivot.BottomLeft, "", gridPivotSelectButton)) pivot = GridPivot.BottomLeft; if (GUI.Toggle(new Rect(r.x+6, r.y+6, 7, 7), pivot == GridPivot.Center, "", gridPivotSelectButton)) pivot = GridPivot.Center; return pivot; }
public static GridPivot PivotPointSelector(GridPivot pivot) { GUISkin skin = AstarPathEditor.astarSkin; GUIStyle background = skin.FindStyle ("GridPivotSelectBackground"); Rect r = GUILayoutUtility.GetRect (19,19,background); r.width = 19; r.height = 19; if (background == null) { return pivot; } if (Event.current.type == EventType.Repaint) { background.Draw (r,false,false,false,false); } if (GUI.Toggle (new Rect (r.x,r.y,7,7),pivot == GridPivot.TopLeft, "",skin.FindStyle ("GridPivotSelectButton"))) pivot = GridPivot.TopLeft; if (GUI.Toggle (new Rect (r.x+12,r.y,7,7),pivot == GridPivot.TopRight,"",skin.FindStyle ("GridPivotSelectButton"))) pivot = GridPivot.TopRight; if (GUI.Toggle (new Rect (r.x+12,r.y+12,7,7),pivot == GridPivot.BottomRight,"",skin.FindStyle ("GridPivotSelectButton"))) pivot = GridPivot.BottomRight; if (GUI.Toggle (new Rect (r.x,r.y+12,7,7),pivot == GridPivot.BottomLeft,"",skin.FindStyle ("GridPivotSelectButton"))) pivot = GridPivot.BottomLeft; if (GUI.Toggle (new Rect (r.x+6,r.y+6,7,7),pivot == GridPivot.Center,"",skin.FindStyle ("GridPivotSelectButton"))) pivot = GridPivot.Center; return pivot; }
// ctor public GenericGrid(int width, int height, float cellSize, Vector3 spawnLocation = default(Vector3), GridPivot pivot = GridPivot.BottomLeft, bool debug = false, Func <int, int, GridObjectType> createGridObject = null) { if (width <= 0) { throw new ArgumentException("width", "width must be positive"); } if (height <= 0) { throw new ArgumentException("height", "height must be positive"); } this.width = width; this.height = height; this.cellSize = cellSize; switch (pivot) { case GridPivot.Center: origin = spawnLocation - new Vector3(width, height) * cellSize / 2f; break; case GridPivot.BottomLeft: origin = spawnLocation; break; default: break; } gridArray = new GridObjectType[width, height]; if (createGridObject == null) { createGridObject = (int x, int y) => Activator.CreateInstance <GridObjectType>(); } for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { gridArray[x, y] = createGridObject(x, y); } } if (debug) { Transform debugTextParent = new GameObject("Debug text").transform; debugTextArray = new TextMeshPro[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { debugTextArray[x, y] = WorldObjects.CreateWorldText(gridArray[x, y]?.ToString(), debugTextParent.transform, GridToWorldPosition(x, y) + new Vector3(.5f, .5f) * cellSize, 40, Color.white, TextAlignmentOptions.Center); Debug.DrawLine(GridToWorldPosition(x, y), GridToWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GridToWorldPosition(x, y), GridToWorldPosition(x + 1, y), Color.white, 100f); } } Debug.DrawLine(GridToWorldPosition(0, height), GridToWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GridToWorldPosition(width, 0), GridToWorldPosition(width, height), Color.white, 100f); } }
public override void OnInspectorGUI (NavGraph target) { GridGraph graph = target as GridGraph; //GUILayout.BeginHorizontal (); //GUILayout.BeginVertical (); Rect lockRect; GUIStyle lockStyle = AstarPathEditor.astarSkin.FindStyle ("GridSizeLock"); if (lockStyle == null) { lockStyle = new GUIStyle (); } #if !UNITY_LE_4_3 || true GUILayout.BeginHorizontal (); GUILayout.BeginVertical (); int newWidth = EditorGUILayout.IntField (new GUIContent ("Width (nodes)","Width of the graph in nodes"), graph.width); int newDepth = EditorGUILayout.IntField (new GUIContent ("Depth (nodes)","Depth (or height you might also call it) of the graph in nodes"), graph.depth); GUILayout.EndVertical (); lockRect = GUILayoutUtility.GetRect (lockStyle.fixedWidth,lockStyle.fixedHeight); // Add a small offset to make it better centred around the controls lockRect.y += 3; GUILayout.EndHorizontal (); // All the layouts mess up the margin to the next control, so add it manually GUILayout.Space (2); #elif UNITY_4 Rect tmpLockRect; int newWidth = IntField (new GUIContent ("Width (nodes)","Width of the graph in nodes"),graph.width,100,0, out lockRect, out sizeSelected1); int newDepth = IntField (new GUIContent ("Depth (nodes)","Depth (or height you might also call it) of the graph in nodes"),graph.depth,100,0, out tmpLockRect, out sizeSelected2); #else Rect tmpLockRect; int newWidth = IntField (new GUIContent ("Width (nodes)","Width of the graph in nodes"),graph.width,50,0, out lockRect, out sizeSelected1); int newDepth = IntField (new GUIContent ("Depth (nodes)","Depth (or height you might also call it) of the graph in nodes"),graph.depth,50,0, out tmpLockRect, out sizeSelected2); #endif lockRect.width = lockStyle.fixedWidth; lockRect.height = lockStyle.fixedHeight; lockRect.x += lockStyle.margin.left; lockRect.y += lockStyle.margin.top; locked = GUI.Toggle (lockRect,locked,new GUIContent ("","If the width and depth values are locked, changing the node size will scale the grid which keeping the number of nodes consistent instead of keeping the size the same and changing the number of nodes in the graph"),lockStyle); //GUILayout.EndHorizontal (); if (newWidth != graph.width || newDepth != graph.depth) { SnapSizeToNodes (newWidth,newDepth,graph); } GUI.SetNextControlName ("NodeSize"); newNodeSize = EditorGUILayout.FloatField (new GUIContent ("Node size","The size of a single node. The size is the side of the node square in world units"),graph.nodeSize); newNodeSize = newNodeSize <= 0.01F ? 0.01F : newNodeSize; float prevRatio = graph.aspectRatio; graph.aspectRatio = EditorGUILayout.FloatField (new GUIContent ("Aspect Ratio","Scaling of the nodes width/depth ratio. Good for isometric games"),graph.aspectRatio); if (graph.nodeSize != newNodeSize || prevRatio != graph.aspectRatio) { if (!locked) { graph.nodeSize = newNodeSize; Matrix4x4 oldMatrix = graph.matrix; graph.GenerateMatrix (); if (graph.matrix != oldMatrix) { //Rescann the graphs //AstarPath.active.AutoScan (); GUI.changed = true; } } else { float delta = newNodeSize / graph.nodeSize; graph.nodeSize = newNodeSize; graph.unclampedSize = new Vector2 (newWidth*graph.nodeSize,newDepth*graph.nodeSize); Vector3 newCenter = graph.matrix.MultiplyPoint3x4 (new Vector3 ((newWidth/2F)*delta,0,(newDepth/2F)*delta)); graph.center = newCenter; graph.GenerateMatrix (); //Make sure the width & depths stay the same graph.width = newWidth; graph.depth = newDepth; AstarPath.active.AutoScan (); } } Vector3 pivotPoint; Vector3 diff; #if UNITY_LE_4_3 EditorGUIUtility.LookLikeControls (); #endif #if !UNITY_4 EditorGUILayoutx.BeginIndent (); #else GUILayout.BeginHorizontal (); #endif switch (pivot) { case GridPivot.Center: graph.center = RoundVector3 ( graph.center ); graph.center = EditorGUILayout.Vector3Field ("Center",graph.center); break; case GridPivot.TopLeft: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (0,0,graph.depth)); pivotPoint = RoundVector3 ( pivotPoint ); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Top-Left",pivotPoint); graph.center = pivotPoint-diff; break; case GridPivot.TopRight: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (graph.width,0,graph.depth)); pivotPoint = RoundVector3 ( pivotPoint ); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Top-Right",pivotPoint); graph.center = pivotPoint-diff; break; case GridPivot.BottomLeft: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (0,0,0)); pivotPoint = RoundVector3 ( pivotPoint ); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Bottom-Left",pivotPoint); graph.center = pivotPoint-diff; break; case GridPivot.BottomRight: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (graph.width,0,0)); pivotPoint = RoundVector3 ( pivotPoint ); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Bottom-Right",pivotPoint); graph.center = pivotPoint-diff; break; } graph.GenerateMatrix (); pivot = PivotPointSelector (pivot); #if !UNITY_4 EditorGUILayoutx.EndIndent (); EditorGUILayoutx.BeginIndent (); #else GUILayout.EndHorizontal (); #endif graph.rotation = EditorGUILayout.Vector3Field ("Rotation",graph.rotation); #if UNITY_LE_4_3 //Add some space to make the Rotation and postion fields be better aligned (instead of the pivot point selector) //GUILayout.Space (19+7); #endif //GUILayout.EndHorizontal (); #if !UNITY_4 EditorGUILayoutx.EndIndent (); #endif #if UNITY_LE_4_3 EditorGUIUtility.LookLikeInspector (); #endif if (GUILayout.Button (new GUIContent ("Snap Size","Snap the size to exactly fit nodes"),GUILayout.MaxWidth (100),GUILayout.MaxHeight (16))) { SnapSizeToNodes (newWidth,newDepth,graph); } Separator (); graph.cutCorners = EditorGUILayout.Toggle (new GUIContent ("Cut Corners","Enables or disables cutting corners. See docs for image example"),graph.cutCorners); graph.neighbours = (NumNeighbours)EditorGUILayout.EnumPopup (new GUIContent ("Connections","Sets how many connections a node should have to it's neighbour nodes."),graph.neighbours); //GUILayout.BeginHorizontal (); //EditorGUILayout.PrefixLabel ("Max Climb"); graph.maxClimb = EditorGUILayout.FloatField (new GUIContent ("Max Climb","How high, relative to the graph, should a climbable level be. A zero (0) indicates infinity"),graph.maxClimb); if ( graph.maxClimb < 0 ) graph.maxClimb = 0; EditorGUI.indentLevel++; graph.maxClimbAxis = EditorGUILayout.IntPopup (new GUIContent ("Climb Axis","Determines which axis the above setting should test on"),graph.maxClimbAxis,new GUIContent[3] {new GUIContent ("X"),new GUIContent ("Y"),new GUIContent ("Z")},new int[3] {0,1,2}); EditorGUI.indentLevel--; if ( graph.maxClimb > 0 && Mathf.Abs((Quaternion.Euler (graph.rotation) * new Vector3 (graph.nodeSize,0,graph.nodeSize))[graph.maxClimbAxis]) > graph.maxClimb ) { EditorGUILayout.HelpBox ("Nodes are spaced further apart than this in the grid. You might want to increase this value or change the axis", MessageType.Warning ); } //GUILayout.EndHorizontal (); graph.maxSlope = EditorGUILayout.Slider (new GUIContent ("Max Slope","Sets the max slope in degrees for a point to be walkable. Only enabled if Height Testing is enabled."),graph.maxSlope,0,90F); graph.erodeIterations = EditorGUILayout.IntField (new GUIContent ("Erosion iterations","Sets how many times the graph should be eroded. This adds extra margin to objects. This will not work when using Graph Updates, so if you can, use the Diameter setting in collision settings instead"),graph.erodeIterations); graph.erodeIterations = graph.erodeIterations < 0 ? 0 : (graph.erodeIterations > 16 ? 16 : graph.erodeIterations); //Clamp iterations to [0,16] if ( graph.erodeIterations > 0 ) { EditorGUI.indentLevel++; graph.erosionUseTags = EditorGUILayout.Toggle (new GUIContent ("Erosion Uses Tags","Instead of making nodes unwalkable, " + "nodes will have their tag set to a value corresponding to their erosion level, " + "which is a quite good measurement of their distance to the closest wall.\nSee online documentation for more info."), graph.erosionUseTags); if (graph.erosionUseTags) { EditorGUI.indentLevel++; graph.erosionFirstTag = EditorGUILayoutx.SingleTagField ("First Tag",graph.erosionFirstTag); EditorGUI.indentLevel--; } EditorGUI.indentLevel--; } DrawCollisionEditor (graph.collision); if ( graph.collision.use2D ) { if ( Mathf.Abs ( Vector3.Dot ( Vector3.forward, Quaternion.Euler (graph.rotation) * Vector3.up ) ) < 0.9f ) { EditorGUILayout.HelpBox ("When using 2D it is recommended to rotate the graph so that it aligns with the 2D plane.", MessageType.Warning ); } } Separator (); showExtra = EditorGUILayout.Foldout (showExtra, "Extra"); if (showExtra) { EditorGUI.indentLevel+=2; graph.penaltyAngle = ToggleGroup (new GUIContent ("Angle Penalty","Adds a penalty based on the slope of the node"),graph.penaltyAngle); //bool preGUI = GUI.enabled; //GUI.enabled = graph.penaltyAngle && GUI.enabled; if (graph.penaltyAngle) { EditorGUI.indentLevel++; graph.penaltyAngleFactor = EditorGUILayout.FloatField (new GUIContent ("Factor","Scale of the penalty. A negative value should not be used"),graph.penaltyAngleFactor); //GUI.enabled = preGUI; HelpBox ("Applies penalty to nodes based on the angle of the hit surface during the Height Testing"); EditorGUI.indentLevel--; } graph.penaltyPosition = ToggleGroup ("Position Penalty",graph.penaltyPosition); //EditorGUILayout.Toggle ("Position Penalty",graph.penaltyPosition); //preGUI = GUI.enabled; //GUI.enabled = graph.penaltyPosition && GUI.enabled; if (graph.penaltyPosition) { EditorGUI.indentLevel++; graph.penaltyPositionOffset = EditorGUILayout.FloatField ("Offset",graph.penaltyPositionOffset); graph.penaltyPositionFactor = EditorGUILayout.FloatField ("Factor",graph.penaltyPositionFactor); HelpBox ("Applies penalty to nodes based on their Y coordinate\nSampled in Int3 space, i.e it is multiplied with Int3.Precision first ("+Int3.Precision+")\n" + "Be very careful when using negative values since a negative penalty will underflow and instead get really high"); //GUI.enabled = preGUI; EditorGUI.indentLevel--; } if (textureVisible) { DrawTextureData (graph.textureData, graph); } EditorGUI.indentLevel-=2; } }
public override void OnInspectorGUI(NavGraph target) { GridGraph graph = target as GridGraph; //GUILayout.BeginHorizontal (); //GUILayout.BeginVertical (); Rect lockRect; Rect tmpLockRect; GUIStyle lockStyle = AstarPathEditor.astarSkin.FindStyle("GridSizeLock"); if (lockStyle == null) { lockStyle = new GUIStyle(); } bool sizeSelected1 = false; bool sizeSelected2 = false; #if UNITY_4 int newWidth = IntField(new GUIContent("Width (nodes)", "Width of the graph in nodes"), graph.width, 100, 0, out lockRect, out sizeSelected1); int newDepth = IntField(new GUIContent("Depth (nodes)", "Depth (or height you might also call it) of the graph in nodes"), graph.depth, 100, 0, out tmpLockRect, out sizeSelected2); #else int newWidth = IntField(new GUIContent("Width (nodes)", "Width of the graph in nodes"), graph.width, 50, 0, out lockRect, out sizeSelected1); int newDepth = IntField(new GUIContent("Depth (nodes)", "Depth (or height you might also call it) of the graph in nodes"), graph.depth, 50, 0, out tmpLockRect, out sizeSelected2); #endif //Rect r = GUILayoutUtility.GetRect (0,0,lockStyle); lockRect.width = lockStyle.fixedWidth; lockRect.height = lockStyle.fixedHeight; lockRect.x += lockStyle.margin.left; lockRect.y += lockStyle.margin.top; locked = GUI.Toggle(lockRect, locked, new GUIContent("", "If the width and depth values are locked, changing the node size will scale the grid which keeping the number of nodes consistent instead of keeping the size the same and changing the number of nodes in the graph"), lockStyle); //GUILayout.EndHorizontal (); if (newWidth != graph.width || newDepth != graph.depth) { SnapSizeToNodes(newWidth, newDepth, graph); } GUI.SetNextControlName("NodeSize"); newNodeSize = EditorGUILayout.FloatField(new GUIContent("Node size", "The size of a single node. The size is the side of the node square in world units"), graph.nodeSize); newNodeSize = newNodeSize <= 0.01F ? 0.01F : newNodeSize; float prevRatio = graph.aspectRatio; graph.aspectRatio = EditorGUILayout.FloatField(new GUIContent("Aspect Ratio", "Scaling of the nodes width/depth ratio. Good for isometric games"), graph.aspectRatio); //if ((GUI.GetNameOfFocusedControl () != "NodeSize" && Event.current.type == EventType.Repaint) || Event.current.keyCode == KeyCode.Return) { //Debug.Log ("Node Size Not Selected " + Event.current.type); if (graph.nodeSize != newNodeSize || prevRatio != graph.aspectRatio) { if (!locked) { graph.nodeSize = newNodeSize; Matrix4x4 oldMatrix = graph.matrix; graph.GenerateMatrix(); if (graph.matrix != oldMatrix) { //Rescann the graphs //AstarPath.active.AutoScan (); GUI.changed = true; } } else { float delta = newNodeSize / graph.nodeSize; graph.nodeSize = newNodeSize; graph.unclampedSize = new Vector2(newWidth * graph.nodeSize, newDepth * graph.nodeSize); Vector3 newCenter = graph.matrix.MultiplyPoint3x4(new Vector3((newWidth / 2F) * delta, 0, (newDepth / 2F) * delta)); graph.center = newCenter; graph.GenerateMatrix(); //Make sure the width & depths stay the same graph.width = newWidth; graph.depth = newDepth; AstarPath.active.AutoScan(); } } //} Vector3 pivotPoint; Vector3 diff; EditorGUIUtility.LookLikeControls(); #if !UNITY_4 EditorGUILayoutx.BeginIndent(); #else GUILayout.BeginHorizontal(); #endif switch (pivot) { case GridPivot.Center: graph.center = EditorGUILayout.Vector3Field("Center", graph.center); break; case GridPivot.TopLeft: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(0, 0, graph.depth)); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Top-Left", pivotPoint); graph.center = pivotPoint - diff; break; case GridPivot.TopRight: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(graph.width, 0, graph.depth)); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Top-Right", pivotPoint); graph.center = pivotPoint - diff; break; case GridPivot.BottomLeft: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(0, 0, 0)); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Bottom-Left", pivotPoint); graph.center = pivotPoint - diff; break; case GridPivot.BottomRight: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(graph.width, 0, 0)); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Bottom-Right", pivotPoint); graph.center = pivotPoint - diff; break; } graph.GenerateMatrix(); pivot = PivotPointSelector(pivot); #if !UNITY_4 EditorGUILayoutx.EndIndent(); EditorGUILayoutx.BeginIndent(); #else GUILayout.EndHorizontal(); #endif graph.rotation = EditorGUILayout.Vector3Field("Rotation", graph.rotation); //Add some space to make the Rotation and postion fields be better aligned (instead of the pivot point selector) GUILayout.Space(19 + 7); //GUILayout.EndHorizontal (); #if !UNITY_4 EditorGUILayoutx.EndIndent(); #endif EditorGUIUtility.LookLikeInspector(); if (GUILayout.Button(new GUIContent("Snap Size", "Snap the size to exactly fit nodes"), GUILayout.MaxWidth(100), GUILayout.MaxHeight(16))) { SnapSizeToNodes(newWidth, newDepth, graph); } Separator(); graph.cutCorners = EditorGUILayout.Toggle(new GUIContent("Cut Corners", "Enables or disables cutting corners. See docs for image example"), graph.cutCorners); graph.neighbours = (NumNeighbours)EditorGUILayout.EnumPopup(new GUIContent("Connections", "Sets how many connections a node should have to it's neighbour nodes."), graph.neighbours); //GUILayout.BeginHorizontal (); //EditorGUILayout.PrefixLabel ("Max Climb"); graph.maxClimb = EditorGUILayout.FloatField(new GUIContent("Max Climb", "How high, relative to the graph, should a climbable level be. A zero (0) indicates infinity"), graph.maxClimb); EditorGUI.indentLevel++; graph.maxClimbAxis = EditorGUILayout.IntPopup(new GUIContent("Climb Axis", "Determines which axis the above setting should test on"), graph.maxClimbAxis, new GUIContent[3] { new GUIContent("X"), new GUIContent("Y"), new GUIContent("Z") }, new int[3] { 0, 1, 2 }); EditorGUI.indentLevel--; //GUILayout.EndHorizontal (); graph.maxSlope = EditorGUILayout.Slider(new GUIContent("Max Slope", "Sets the max slope in degrees for a point to be walkable. Only enabled if Height Testing is enabled."), graph.maxSlope, 0, 90F); graph.erodeIterations = EditorGUILayout.IntField(new GUIContent("Erode iterations", "Sets how many times the graph should be eroded. This adds extra margin to objects. This will not work when using Graph Updates, so if you can, use the Diameter setting in collision settings instead"), graph.erodeIterations); graph.erodeIterations = graph.erodeIterations > 16 ? 16 : graph.erodeIterations; //Clamp iterations to 16 graph.erosionUseTags = EditorGUILayout.Toggle(new GUIContent("Erosion Uses Tags", "Instead of making nodes unwalkable, " + "nodes will have their tag set to a value corresponding to their erosion level, " + "which is a quite good measurement of their distance to the closest wall."), graph.erosionUseTags); if (graph.erosionUseTags) { EditorGUI.indentLevel++; graph.erosionFirstTag = EditorGUILayoutx.SingleTagField("First Tag", graph.erosionFirstTag); EditorGUI.indentLevel--; } DrawCollisionEditor(graph.collision); Separator(); showExtra = EditorGUILayout.Foldout(showExtra, "Extra"); if (showExtra) { EditorGUI.indentLevel += 2; graph.penaltyAngle = ToggleGroup(new GUIContent("Angle Penalty", "Adds a penalty based on the slope of the node"), graph.penaltyAngle); //bool preGUI = GUI.enabled; //GUI.enabled = graph.penaltyAngle && GUI.enabled; if (graph.penaltyAngle) { EditorGUI.indentLevel++; graph.penaltyAngleFactor = EditorGUILayout.FloatField(new GUIContent("Factor", "Scale of the penalty. A negative value should not be used"), graph.penaltyAngleFactor); //GUI.enabled = preGUI; HelpBox("Applies penalty to nodes based on the angle of the hit surface during the Height Testing"); EditorGUI.indentLevel--; } graph.penaltyPosition = ToggleGroup("Position Penalty", graph.penaltyPosition); //EditorGUILayout.Toggle ("Position Penalty",graph.penaltyPosition); //preGUI = GUI.enabled; //GUI.enabled = graph.penaltyPosition && GUI.enabled; if (graph.penaltyPosition) { EditorGUI.indentLevel++; graph.penaltyPositionOffset = EditorGUILayout.FloatField("Offset", graph.penaltyPositionOffset); graph.penaltyPositionFactor = EditorGUILayout.FloatField("Factor", graph.penaltyPositionFactor); HelpBox("Applies penalty to nodes based on their Y coordinate\nSampled in Int3 space, i.e it is multiplied with Int3.Precision first (" + Int3.Precision + ")\n" + "Be very careful when using negative values since a negative penalty will underflow and instead get really high"); //GUI.enabled = preGUI; EditorGUI.indentLevel--; } if (textureVisible) { DrawTextureData(graph.textureData, graph); } EditorGUI.indentLevel -= 2; } }
public void DeSerializeSettings(NavGraph target, AstarSerializer serializer) { pivot = (GridPivot)serializer.GetValue("pivot", typeof(int), GridPivot.BottomLeft); locked = (bool)serializer.GetValue("locked", typeof(bool), true); showExtra = (bool)serializer.GetValue("showExtra", typeof(bool)); }
public void DeSerializeSettings (NavGraph target, AstarSerializer serializer) { pivot = (GridPivot)serializer.GetValue ("pivot",typeof(int),GridPivot.BottomLeft); locked = (bool)serializer.GetValue ("locked",typeof(bool),true); showExtra = (bool)serializer.GetValue ("showExtra",typeof(bool)); }
public override void OnInspectorGUI(NavGraph target) { GridGraph graph = target as GridGraph; //GUILayout.BeginHorizontal (); //GUILayout.BeginVertical (); Rect lockRect; Rect tmpLockRect; GUIStyle lockStyle = AstarPathEditor.astarSkin.FindStyle ("GridSizeLock"); if (lockStyle == null) { lockStyle = new GUIStyle (); } bool sizeSelected1 = false; bool sizeSelected2 = false; int newWidth = IntField (new GUIContent ("Width (nodes)","Width of the graph in nodes"),graph.width,50,0, out lockRect, out sizeSelected1); int newDepth = IntField (new GUIContent ("Depth (nodes)","Depth (or height you might also call it) of the graph in nodes"),graph.depth,50,0, out tmpLockRect, out sizeSelected2); //Rect r = GUILayoutUtility.GetRect (0,0,lockStyle); lockRect.width = lockStyle.fixedWidth; lockRect.height = lockStyle.fixedHeight; lockRect.x += lockStyle.margin.left; lockRect.y += lockStyle.margin.top; locked = GUI.Toggle (lockRect,locked,new GUIContent ("","If the width and depth values are locked, changing the node size will scale the grid which keeping the number of nodes consistent instead of keeping the size the same and changing the number of nodes in the graph"),lockStyle); //GUILayout.EndHorizontal (); if (newWidth != graph.width || newDepth != graph.depth) { SnapSizeToNodes (newWidth,newDepth,graph); } GUI.SetNextControlName ("NodeSize"); newNodeSize = EditorGUILayout.FloatField (new GUIContent ("Node size","The size of a single node. The size is the side of the node square in world units"),graph.nodeSize); newNodeSize = newNodeSize <= 0.01F ? 0.01F : newNodeSize; float prevRatio = graph.aspectRatio; graph.aspectRatio = EditorGUILayout.FloatField (new GUIContent ("Aspect Ratio","Scaling of the nodes width/depth ratio. Good for isometric games"),graph.aspectRatio); //if ((GUI.GetNameOfFocusedControl () != "NodeSize" && Event.current.type == EventType.Repaint) || Event.current.keyCode == KeyCode.Return) { //Debug.Log ("Node Size Not Selected " + Event.current.type); if (graph.nodeSize != newNodeSize || prevRatio != graph.aspectRatio) { if (!locked) { graph.nodeSize = newNodeSize; Matrix4x4 oldMatrix = graph.matrix; graph.GenerateMatrix (); if (graph.matrix != oldMatrix) { //Rescann the graphs //AstarPath.active.AutoScan (); GUI.changed = true; } } else { float delta = newNodeSize / graph.nodeSize; graph.nodeSize = newNodeSize; graph.unclampedSize = new Vector2 (newWidth*graph.nodeSize,newDepth*graph.nodeSize); Vector3 newCenter = graph.matrix.MultiplyPoint3x4 (new Vector3 ((newWidth/2F)*delta,0,(newDepth/2F)*delta)); graph.center = newCenter; graph.GenerateMatrix (); //Make sure the width & depths stay the same graph.width = newWidth; graph.depth = newDepth; AstarPath.active.AutoScan (); } } //} Vector3 pivotPoint; Vector3 diff; EditorGUIUtility.LookLikeControls (); EditorGUILayoutx.BeginIndent (); switch (pivot) { case GridPivot.Center: graph.center = EditorGUILayout.Vector3Field ("Center",graph.center); break; case GridPivot.TopLeft: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (0,0,graph.depth)); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Top-Left",pivotPoint); graph.center = pivotPoint-diff; break; case GridPivot.TopRight: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (graph.width,0,graph.depth)); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Top-Right",pivotPoint); graph.center = pivotPoint-diff; break; case GridPivot.BottomLeft: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (0,0,0)); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Bottom-Left",pivotPoint); graph.center = pivotPoint-diff; break; case GridPivot.BottomRight: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (graph.width,0,0)); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Bottom-Right",pivotPoint); graph.center = pivotPoint-diff; break; } graph.GenerateMatrix (); pivot = PivotPointSelector (pivot); EditorGUILayoutx.EndIndent (); EditorGUILayoutx.BeginIndent (); graph.rotation = EditorGUILayout.Vector3Field ("Rotation",graph.rotation); //Add some space to make the Rotation and postion fields be better aligned (instead of the pivot point selector) GUILayout.Space (19+7); //GUILayout.EndHorizontal (); EditorGUILayoutx.EndIndent (); EditorGUIUtility.LookLikeInspector (); if (GUILayout.Button (new GUIContent ("Snap Size","Snap the size to exactly fit nodes"),GUILayout.MaxWidth (100),GUILayout.MaxHeight (16))) { SnapSizeToNodes (newWidth,newDepth,graph); } Separator (); graph.cutCorners = EditorGUILayout.Toggle (new GUIContent ("Cut Corners","Enables or disables cutting corners. See docs for image example"),graph.cutCorners); graph.neighbours = (NumNeighbours)EditorGUILayout.EnumPopup (new GUIContent ("Connections","Sets how many connections a node should have to it's neighbour nodes."),graph.neighbours); //GUILayout.BeginHorizontal (); //EditorGUILayout.PrefixLabel ("Max Climb"); graph.maxClimb = EditorGUILayout.FloatField (new GUIContent ("Max Climb","How high, relative to the graph, should a climbable level be. A zero (0) indicates infinity"),graph.maxClimb); EditorGUI.indentLevel++; graph.maxClimbAxis = EditorGUILayout.IntPopup (new GUIContent ("Climb Axis","Determines which axis the above setting should test on"),graph.maxClimbAxis,new GUIContent[3] {new GUIContent ("X"),new GUIContent ("Y"),new GUIContent ("Z")},new int[3] {0,1,2}); EditorGUI.indentLevel--; //GUILayout.EndHorizontal (); GUILayout.BeginHorizontal (); //bool preEnabled = GUI.enabled; //GUI.enabled = graph.useRaycastNormal; graph.maxSlope = EditorGUILayout.Slider (new GUIContent ("Max Slope","Sets the max slope in degrees for a point to be walkable"),graph.maxSlope,0,90F); //GUI.enabled = preEnabled; //graph.useRaycastNormal = Mathf.Abs (90-graph.maxSlope) > float.Epsilon; //GUILayout.Toggle (graph.useRaycastNormal,new GUIContent ("","Use the heigh raycast's normal to figure out the slope of the ground and check if it flat enough to stand on"),GUILayout.Width (10)); GUILayout.EndHorizontal (); graph.erodeIterations = EditorGUILayout.IntField (new GUIContent ("Erode iterations","Sets how many times the graph should be eroded. This adds extra margin to objects. This will not work when using Graph Updates, so if you can, use the Diameter setting in collision settings instead"),graph.erodeIterations); graph.erodeIterations = graph.erodeIterations > 16 ? 16 : graph.erodeIterations; //Clamp iterations to 16 graph.erosionUseTags = EditorGUILayout.Toggle (new GUIContent ("Erosion Uses Tags","Instead of making nodes unwalkable, " + "nodes will have their tag set to a value corresponding to their erosion level, " + "which is a quite good measurement of their distance to the closest wall."), graph.erosionUseTags); if (graph.erosionUseTags) { EditorGUI.indentLevel++; graph.erosionFirstTag = EditorGUILayoutx.SingleTagField ("First Tag",graph.erosionFirstTag); EditorGUI.indentLevel--; } DrawCollisionEditor (graph.collision); Separator (); showExtra = EditorGUILayout.Foldout (showExtra, "Extra"); if (showExtra) { EditorGUI.indentLevel+=2; graph.penaltyAngle = ToggleGroup (new GUIContent ("Angle Penalty","Adds a penalty based on the slope of the node"),graph.penaltyAngle); //bool preGUI = GUI.enabled; //GUI.enabled = graph.penaltyAngle && GUI.enabled; if (graph.penaltyAngle) { EditorGUI.indentLevel++; graph.penaltyAngleFactor = EditorGUILayout.FloatField (new GUIContent ("Factor","Scale of the penalty. A negative value should not be used"),graph.penaltyAngleFactor); //GUI.enabled = preGUI; HelpBox ("Applies penalty to nodes based on the angle of the hit surface during the Height Testing"); EditorGUI.indentLevel--; } graph.penaltyPosition = ToggleGroup ("Position Penalty",graph.penaltyPosition); //EditorGUILayout.Toggle ("Position Penalty",graph.penaltyPosition); //preGUI = GUI.enabled; //GUI.enabled = graph.penaltyPosition && GUI.enabled; if (graph.penaltyPosition) { EditorGUI.indentLevel++; graph.penaltyPositionOffset = EditorGUILayout.FloatField ("Offset",graph.penaltyPositionOffset); graph.penaltyPositionFactor = EditorGUILayout.FloatField ("Factor",graph.penaltyPositionFactor); HelpBox ("Applies penalty to nodes based on their Y coordinate\nSampled in Int3 space, i.e it is multiplied with Int3.Precision first ("+Int3.Precision+")\n" + "Be very careful when using negative values since a negative penalty will underflow and instead get really high"); //GUI.enabled = preGUI; EditorGUI.indentLevel--; } GUI.enabled = false; ToggleGroup (new GUIContent ("Use Texture",AstarPathEditor.AstarProTooltip),false); GUI.enabled = true; EditorGUI.indentLevel-=2; } }
void DrawPositionField (GridGraph graph) { Vector3 pivotPoint; Vector3 diff; GUILayout.BeginHorizontal (); switch (pivot) { case GridPivot.Center: graph.center = RoundVector3 ( graph.center ); graph.center = EditorGUILayout.Vector3Field ("Center",graph.center); break; case GridPivot.TopLeft: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (0,0,graph.depth)); pivotPoint = RoundVector3 ( pivotPoint ); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Top-Left",pivotPoint); graph.center = pivotPoint-diff; break; case GridPivot.TopRight: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (graph.width,0,graph.depth)); pivotPoint = RoundVector3 ( pivotPoint ); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Top-Right",pivotPoint); graph.center = pivotPoint-diff; break; case GridPivot.BottomLeft: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (0,0,0)); pivotPoint = RoundVector3 ( pivotPoint ); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Bottom-Left",pivotPoint); graph.center = pivotPoint-diff; break; case GridPivot.BottomRight: pivotPoint = graph.matrix.MultiplyPoint3x4 (new Vector3 (graph.width,0,0)); pivotPoint = RoundVector3 ( pivotPoint ); diff = pivotPoint-graph.center; pivotPoint = EditorGUILayout.Vector3Field ("Bottom-Right",pivotPoint); graph.center = pivotPoint-diff; break; } graph.GenerateMatrix (); pivot = PivotPointSelector (pivot); GUILayout.EndHorizontal (); }
private void DrawGridsList() { if (pathfinder.gridsCount == 0) { EditorGUILayout.HelpBox("SAP2D Manager has't none grids." + "\nYou should create new grid by click right mouse button on inspector window and select 'Add New Grid'", MessageType.Info); return; } serializedObject.Update(); for (int i = 0; i < pathfinder.gridsCount; i++) { SAP_GridSource grid = pathfinder.GetGrid(i); SAP_GraphDrawer drawer = pathfinder.GraphDrawers[i]; SerializedProperty obstacleLayer = grids.GetArrayElementAtIndex(i).FindPropertyRelative("ObstaclesLayer"); EditorGUILayout.BeginVertical("box"); EditorGUI.indentLevel = 1; DrawHeaderPanel(grid, drawer); if (drawer.ShowGridInfo) { #region GridParametersDrawing EditorGUILayout.BeginVertical("box"); drawer.ShowGridParameters = EditorGUILayout.Foldout(drawer.ShowGridParameters, "Grid Parameters", true); if (drawer.ShowGridParameters) { EditorGUI.indentLevel = 0; EditorGUI.BeginChangeCheck(); int gridWidth = EditorGUILayout.IntField("Grid Width", grid.Width); if (EditorGUI.EndChangeCheck()) { grid.CreateGrid(gridWidth, grid.Height); SceneView.RepaintAll(); if (grid.UserGridData != null) { grid.UserGridData.UnwalkableTiles.Clear(); } } EditorGUI.BeginChangeCheck(); int gridHeight = EditorGUILayout.IntField("Grid Height", grid.Height); if (EditorGUI.EndChangeCheck()) { grid.CreateGrid(grid.Width, gridHeight); SceneView.RepaintAll(); if (grid.UserGridData != null) { grid.UserGridData.UnwalkableTiles.Clear(); } } EditorGUI.BeginChangeCheck(); float tileDiameter = EditorGUILayout.FloatField("Tile Diameter", grid.TileDiameter); if (EditorGUI.EndChangeCheck()) { grid.TileDiameter = tileDiameter; SceneView.RepaintAll(); } EditorGUI.BeginChangeCheck(); GridPivot pivot = (GridPivot)EditorGUILayout.EnumPopup("Grid Pivot", grid.GridPivot); if (EditorGUI.EndChangeCheck()) { grid.GridPivot = pivot; SceneView.RepaintAll(); } EditorGUI.BeginChangeCheck(); Vector3 gridPos = EditorGUILayout.Vector3Field("Grid Position", grid.Position); if (EditorGUI.EndChangeCheck()) { grid.Position = gridPos; SceneView.RepaintAll(); } EditorGUI.indentLevel = 1; } EditorGUILayout.EndVertical(); #endregion #region PathfindingSettingsDrawing EditorGUILayout.BeginVertical("box"); drawer.ShowPathfindingSettings = EditorGUILayout.Foldout(drawer.ShowPathfindingSettings, "Pathfinding Settings", true); if (drawer.ShowPathfindingSettings) { EditorGUI.indentLevel = 0; bool usePhysics2D = EditorGUILayout.Toggle("Use Physics 2D", grid.UsePhysics2D); if (usePhysics2D != grid.UsePhysics2D) { grid.UsePhysics2D = usePhysics2D; pathfinder.CalculateColliders(); SceneView.RepaintAll(); } EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(obstacleLayer); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); } EditorGUI.indentLevel = 1; } EditorGUILayout.EndVertical(); #endregion } EditorGUILayout.EndVertical(); } if (GUILayout.Button("Calculate Colliders (All Grids)", GUILayout.Height(25))) { for (int i = 0; i < pathfinder.gridsCount; i++) { SAP_GridSource grid = pathfinder.GetGrid(i); } pathfinder.CalculateColliders(); SceneView.RepaintAll(); } }
public override void OnInspectorGUI(NavGraph target) { GridGraph graph = target as GridGraph; //GUILayout.BeginHorizontal (); //GUILayout.BeginVertical (); Rect lockRect; GUIStyle lockStyle = AstarPathEditor.astarSkin.FindStyle("GridSizeLock"); if (lockStyle == null) { lockStyle = new GUIStyle(); } #if !UNITY_LE_4_3 || true GUILayout.BeginHorizontal(); GUILayout.BeginVertical(); int newWidth = EditorGUILayout.IntField(new GUIContent("Width (nodes)", "Width of the graph in nodes"), graph.width); int newDepth = EditorGUILayout.IntField(new GUIContent("Depth (nodes)", "Depth (or height you might also call it) of the graph in nodes"), graph.depth); GUILayout.EndVertical(); lockRect = GUILayoutUtility.GetRect(lockStyle.fixedWidth, lockStyle.fixedHeight); // Add a small offset to make it better centred around the controls lockRect.y += 3; GUILayout.EndHorizontal(); // All the layouts mess up the margin to the next control, so add it manually GUILayout.Space(2); #elif UNITY_4 Rect tmpLockRect; int newWidth = IntField(new GUIContent("Width (nodes)", "Width of the graph in nodes"), graph.width, 100, 0, out lockRect, out sizeSelected1); int newDepth = IntField(new GUIContent("Depth (nodes)", "Depth (or height you might also call it) of the graph in nodes"), graph.depth, 100, 0, out tmpLockRect, out sizeSelected2); #else Rect tmpLockRect; int newWidth = IntField(new GUIContent("Width (nodes)", "Width of the graph in nodes"), graph.width, 50, 0, out lockRect, out sizeSelected1); int newDepth = IntField(new GUIContent("Depth (nodes)", "Depth (or height you might also call it) of the graph in nodes"), graph.depth, 50, 0, out tmpLockRect, out sizeSelected2); #endif lockRect.width = lockStyle.fixedWidth; lockRect.height = lockStyle.fixedHeight; lockRect.x += lockStyle.margin.left; lockRect.y += lockStyle.margin.top; locked = GUI.Toggle(lockRect, locked, new GUIContent("", "If the width and depth values are locked, changing the node size will scale the grid which keeping the number of nodes consistent instead of keeping the size the same and changing the number of nodes in the graph"), lockStyle); //GUILayout.EndHorizontal (); if (newWidth != graph.width || newDepth != graph.depth) { SnapSizeToNodes(newWidth, newDepth, graph); } GUI.SetNextControlName("NodeSize"); newNodeSize = EditorGUILayout.FloatField(new GUIContent("Node size", "The size of a single node. The size is the side of the node square in world units"), graph.nodeSize); newNodeSize = newNodeSize <= 0.01F ? 0.01F : newNodeSize; float prevRatio = graph.aspectRatio; graph.aspectRatio = EditorGUILayout.FloatField(new GUIContent("Aspect Ratio", "Scaling of the nodes width/depth ratio. Good for isometric games"), graph.aspectRatio); graph.isometricAngle = EditorGUILayout.FloatField(new GUIContent("Isometric Angle", "For an isometric 2D game, you can use this parameter to scale the graph correctly."), graph.isometricAngle); if (graph.nodeSize != newNodeSize || prevRatio != graph.aspectRatio) { if (!locked) { graph.nodeSize = newNodeSize; Matrix4x4 oldMatrix = graph.matrix; graph.GenerateMatrix(); if (graph.matrix != oldMatrix) { //Rescann the graphs //AstarPath.active.AutoScan (); GUI.changed = true; } } else { float delta = newNodeSize / graph.nodeSize; graph.nodeSize = newNodeSize; graph.unclampedSize = new Vector2(newWidth * graph.nodeSize, newDepth * graph.nodeSize); Vector3 newCenter = graph.matrix.MultiplyPoint3x4(new Vector3((newWidth / 2F) * delta, 0, (newDepth / 2F) * delta)); graph.center = newCenter; graph.GenerateMatrix(); //Make sure the width & depths stay the same graph.width = newWidth; graph.depth = newDepth; AutoScan(); } } Vector3 pivotPoint; Vector3 diff; #if UNITY_LE_4_3 EditorGUIUtility.LookLikeControls(); #endif #if !UNITY_4 EditorGUILayoutx.BeginIndent(); #else GUILayout.BeginHorizontal(); #endif switch (pivot) { case GridPivot.Center: graph.center = RoundVector3(graph.center); graph.center = EditorGUILayout.Vector3Field("Center", graph.center); break; case GridPivot.TopLeft: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(0, 0, graph.depth)); pivotPoint = RoundVector3(pivotPoint); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Top-Left", pivotPoint); graph.center = pivotPoint - diff; break; case GridPivot.TopRight: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(graph.width, 0, graph.depth)); pivotPoint = RoundVector3(pivotPoint); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Top-Right", pivotPoint); graph.center = pivotPoint - diff; break; case GridPivot.BottomLeft: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(0, 0, 0)); pivotPoint = RoundVector3(pivotPoint); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Bottom-Left", pivotPoint); graph.center = pivotPoint - diff; break; case GridPivot.BottomRight: pivotPoint = graph.matrix.MultiplyPoint3x4(new Vector3(graph.width, 0, 0)); pivotPoint = RoundVector3(pivotPoint); diff = pivotPoint - graph.center; pivotPoint = EditorGUILayout.Vector3Field("Bottom-Right", pivotPoint); graph.center = pivotPoint - diff; break; } graph.GenerateMatrix(); pivot = PivotPointSelector(pivot); #if !UNITY_4 EditorGUILayoutx.EndIndent(); EditorGUILayoutx.BeginIndent(); #else GUILayout.EndHorizontal(); #endif graph.rotation = EditorGUILayout.Vector3Field("Rotation", graph.rotation); #if UNITY_LE_4_3 //Add some space to make the Rotation and postion fields be better aligned (instead of the pivot point selector) //GUILayout.Space (19+7); #endif //GUILayout.EndHorizontal (); #if !UNITY_4 EditorGUILayoutx.EndIndent(); #endif #if UNITY_LE_4_3 EditorGUIUtility.LookLikeInspector(); #endif if (GUILayout.Button(new GUIContent("Snap Size", "Snap the size to exactly fit nodes"), GUILayout.MaxWidth(100), GUILayout.MaxHeight(16))) { SnapSizeToNodes(newWidth, newDepth, graph); } Separator(); graph.cutCorners = EditorGUILayout.Toggle(new GUIContent("Cut Corners", "Enables or disables cutting corners. See docs for image example"), graph.cutCorners); graph.neighbours = (NumNeighbours)EditorGUILayout.EnumPopup(new GUIContent("Connections", "Sets how many connections a node should have to it's neighbour nodes."), graph.neighbours); graph.maxClimb = EditorGUILayout.FloatField(new GUIContent("Max Climb", "How high, relative to the graph, should a climbable level be. A zero (0) indicates infinity"), graph.maxClimb); if (graph.maxClimb < 0) { graph.maxClimb = 0; } EditorGUI.indentLevel++; graph.maxClimbAxis = EditorGUILayout.IntPopup(new GUIContent("Climb Axis", "Determines which axis the above setting should test on"), graph.maxClimbAxis, new GUIContent[3] { new GUIContent("X"), new GUIContent("Y"), new GUIContent("Z") }, new int[3] { 0, 1, 2 }); EditorGUI.indentLevel--; if (graph.maxClimb > 0 && Mathf.Abs((Quaternion.Euler(graph.rotation) * new Vector3(graph.nodeSize, 0, graph.nodeSize))[graph.maxClimbAxis]) > graph.maxClimb) { EditorGUILayout.HelpBox("Nodes are spaced further apart than this in the grid. You might want to increase this value or change the axis", MessageType.Warning); } //GUILayout.EndHorizontal (); graph.maxSlope = EditorGUILayout.Slider(new GUIContent("Max Slope", "Sets the max slope in degrees for a point to be walkable. Only enabled if Height Testing is enabled."), graph.maxSlope, 0, 90F); graph.erodeIterations = EditorGUILayout.IntField(new GUIContent("Erosion iterations", "Sets how many times the graph should be eroded. This adds extra margin to objects. This will not work when using Graph Updates, so if you can, use the Diameter setting in collision settings instead"), graph.erodeIterations); graph.erodeIterations = graph.erodeIterations < 0 ? 0 : (graph.erodeIterations > 16 ? 16 : graph.erodeIterations); //Clamp iterations to [0,16] if (graph.erodeIterations > 0) { EditorGUI.indentLevel++; graph.erosionUseTags = EditorGUILayout.Toggle(new GUIContent("Erosion Uses Tags", "Instead of making nodes unwalkable, " + "nodes will have their tag set to a value corresponding to their erosion level, " + "which is a quite good measurement of their distance to the closest wall.\nSee online documentation for more info."), graph.erosionUseTags); if (graph.erosionUseTags) { EditorGUI.indentLevel++; graph.erosionFirstTag = EditorGUILayoutx.SingleTagField("First Tag", graph.erosionFirstTag); EditorGUI.indentLevel--; } EditorGUI.indentLevel--; } DrawCollisionEditor(graph.collision); if (graph.collision.use2D) { if (Mathf.Abs(Vector3.Dot(Vector3.forward, Quaternion.Euler(graph.rotation) * Vector3.up)) < 0.9f) { EditorGUILayout.HelpBox("When using 2D it is recommended to rotate the graph so that it aligns with the 2D plane.", MessageType.Warning); } } Separator(); GUILayout.Label(new GUIContent("Advanced"), EditorStyles.boldLabel); showExtra = EditorGUILayout.Foldout(showExtra, "Penalty Modifications"); if (showExtra) { EditorGUI.indentLevel += 2; graph.penaltyAngle = ToggleGroup(new GUIContent("Angle Penalty", "Adds a penalty based on the slope of the node"), graph.penaltyAngle); //bool preGUI = GUI.enabled; //GUI.enabled = graph.penaltyAngle && GUI.enabled; if (graph.penaltyAngle) { EditorGUI.indentLevel++; graph.penaltyAngleFactor = EditorGUILayout.FloatField(new GUIContent("Factor", "Scale of the penalty. A negative value should not be used"), graph.penaltyAngleFactor); graph.penaltyAnglePower = EditorGUILayout.Slider("Power", graph.penaltyAnglePower, 0.1f, 10f); //GUI.enabled = preGUI; HelpBox("Applies penalty to nodes based on the angle of the hit surface during the Height Testing\nPenalty applied is: P=(1-cos(angle)^power)*factor."); EditorGUI.indentLevel--; } graph.penaltyPosition = ToggleGroup("Position Penalty", graph.penaltyPosition); //EditorGUILayout.Toggle ("Position Penalty",graph.penaltyPosition); //preGUI = GUI.enabled; //GUI.enabled = graph.penaltyPosition && GUI.enabled; if (graph.penaltyPosition) { EditorGUI.indentLevel++; graph.penaltyPositionOffset = EditorGUILayout.FloatField("Offset", graph.penaltyPositionOffset); graph.penaltyPositionFactor = EditorGUILayout.FloatField("Factor", graph.penaltyPositionFactor); HelpBox("Applies penalty to nodes based on their Y coordinate\nSampled in Int3 space, i.e it is multiplied with Int3.Precision first (" + Int3.Precision + ")\n" + "Be very careful when using negative values since a negative penalty will underflow and instead get really high"); //GUI.enabled = preGUI; EditorGUI.indentLevel--; } GUI.enabled = false; ToggleGroup(new GUIContent("Use Texture", AstarPathEditor.AstarProTooltip), false); GUI.enabled = true; EditorGUI.indentLevel -= 2; } }