public override void OnInspectorGUI() { GraphUpdateScene script = target as GraphUpdateScene; #if !UNITY_LE_4_3 Undo.RecordObject(script, "modify settings on GraphUpdateObject"); #endif if (script.points == null) { script.points = new Vector3[0]; } if (script.points == null || script.points.Length == 0) { if (script.GetComponent <Collider>() != null) { EditorGUILayout.HelpBox("No points, using collider.bounds", MessageType.Info); } else if (script.GetComponent <Renderer>() != null) { EditorGUILayout.HelpBox("No points, using renderer.bounds", MessageType.Info); } else { EditorGUILayout.HelpBox("No points and no collider or renderer attached, will not affect anything\nPoints can be added using the transform tool and holding shift", MessageType.Warning); } } Vector3[] prePoints = script.points; #if UNITY_4 EditorGUILayout.PropertyField(serializedObject.FindProperty("points"), true); if (GUI.changed) { serializedObject.ApplyModifiedProperties(); } #else DrawDefaultInspector(); #endif #if UNITY_LE_4_3 EditorGUI.indentLevel = 1; #else EditorGUI.indentLevel = 0; #endif script.updatePhysics = EditorGUILayout.Toggle(new GUIContent("Update Physics", "Perform similar calculations on the nodes as during scan.\n" + "Grid Graphs will update the position of the nodes and also check walkability using collision.\nSee online documentation for more info."), script.updatePhysics); if (script.updatePhysics) { EditorGUI.indentLevel++; script.resetPenaltyOnPhysics = EditorGUILayout.Toggle(new GUIContent("Reset Penalty On Physics", "Will reset the penalty to the default value during the update."), script.resetPenaltyOnPhysics); EditorGUI.indentLevel--; } script.updateErosion = EditorGUILayout.Toggle(new GUIContent("Update Erosion", "Recalculate erosion for grid graphs.\nSee online documentation for more info"), script.updateErosion); if (prePoints != script.points) { script.RecalcConvex(); HandleUtility.Repaint(); } bool preConvex = script.convex; script.convex = EditorGUILayout.Toggle(new GUIContent("Convex", "Sets if only the convex hull of the points should be used or the whole polygon"), script.convex); if (script.convex != preConvex) { script.RecalcConvex(); HandleUtility.Repaint(); } script.minBoundsHeight = EditorGUILayout.FloatField(new GUIContent("Min Bounds Height", "Defines a minimum height to be used for the bounds of the GUO.\nUseful if you define points in 2D (which would give height 0)"), script.minBoundsHeight); script.applyOnStart = EditorGUILayout.Toggle("Apply On Start", script.applyOnStart); script.applyOnScan = EditorGUILayout.Toggle("Apply On Scan", script.applyOnScan); script.modifyWalkability = EditorGUILayout.Toggle(new GUIContent("Modify walkability", "If true, walkability of all nodes will be modified"), script.modifyWalkability); if (script.modifyWalkability) { EditorGUI.indentLevel++; script.setWalkability = EditorGUILayout.Toggle(new GUIContent("Walkability", "Nodes' walkability will be set to this value"), script.setWalkability); EditorGUI.indentLevel--; } script.penaltyDelta = EditorGUILayout.IntField(new GUIContent("Penalty Delta", "A penalty will be added to the nodes, usually you need very large values, at least 1000-10000.\n" + "A higher penalty will mean that agents will try to avoid those nodes."), script.penaltyDelta); if (script.penaltyDelta < 0) { EditorGUILayout.HelpBox("Be careful when lowering the penalty. Negative penalties are not supported and will instead underflow and get really high.\n" + "You can set an initial penalty on graphs (see their settings) and then lower them like this to get regions which are easier to traverse.", MessageType.Warning); } script.modifyTag = EditorGUILayout.Toggle(new GUIContent("Modify Tags", "Should the tags of the nodes be modified"), script.modifyTag); if (script.modifyTag) { EditorGUI.indentLevel++; script.setTag = EditorGUILayout.Popup("Set Tag", script.setTag, AstarPath.FindTagNames()); EditorGUI.indentLevel--; } if (GUILayout.Button("Tags can be used to restrict which units can walk on what ground. Click here for more info", "HelpBox")) { Application.OpenURL(AstarPathEditor.GetURL("tags")); } EditorGUILayout.Separator(); bool worldSpace = EditorGUILayout.Toggle(new GUIContent("Use World Space", "Specify coordinates in world space or local space. When using local space you can move the GameObject " + "around and the points will follow.\n" + "Some operations, like calculating the convex hull, and snapping to Y will change axis depending on how the object is rotated if world space is not used." ), script.useWorldSpace); if (worldSpace != script.useWorldSpace) { #if !UNITY_LE_4_3 Undo.RecordObject(script, "switch use-world-space"); #endif script.ToggleUseWorldSpace(); } #if UNITY_4 EditorGUI.BeginChangeCheck(); #endif script.lockToY = EditorGUILayout.Toggle("Lock to Y", script.lockToY); if (script.lockToY) { EditorGUI.indentLevel++; script.lockToYValue = EditorGUILayout.FloatField("Lock to Y value", script.lockToYValue); EditorGUI.indentLevel--; #if !UNITY_LE_4_3 if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(script, "change Y locking"); } #endif script.LockToY(); } EditorGUILayout.Separator(); if (GUILayout.Button("Clear all points")) { #if UNITY_LE_4_3 Undo.RegisterUndo(script, "Removed All Points"); #endif script.points = new Vector3[0]; EditorUtility.SetDirty(target); script.RecalcConvex(); } if (GUI.changed) { #if UNITY_LE_4_3 Undo.RegisterUndo(script, "Modify Settings on GraphUpdateObject"); #endif EditorUtility.SetDirty(target); } }
public static void GetGUO(this GraphUpdateScene gus, ref GraphUpdateObject guo) { if (gus.points == null || gus.points.Length == 0) { var polygonCollider = gus.GetComponent <PolygonCollider2D>(); if (polygonCollider != null) { var points2D = polygonCollider.points; Vector3[] pts = new Vector3[points2D.Length]; for (int i = 0; i < pts.Length; i++) { var p = points2D[i] + polygonCollider.offset; pts[i] = new Vector3(p.x, 0, p.y); } var mat = gus.transform.localToWorldMatrix * Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 0, 0), Vector3.one); var shape = new GraphUpdateShape(gus.points, gus.convex, mat, gus.minBoundsHeight); if (guo == null) { guo = new GraphUpdateObject(); } guo.bounds = shape.GetBounds(); guo.shape = shape; } else { var bounds = gus.GetBounds(); if (bounds.center == Vector3.zero && bounds.size == Vector3.zero) { Debug.LogError("Cannot apply GraphUpdateScene, no points defined and no renderer or collider attached", gus); } if (guo == null) { guo = new GraphUpdateObject(bounds); } else { guo.bounds = bounds; } guo.shape = null; } } else { GraphUpdateShape shape; // Used for compatibility with older versions var worldPoints = new Vector3[gus.points.Length]; for (int i = 0; i < gus.points.Length; i++) { worldPoints[i] = gus.transform.TransformPoint(gus.points[i]); } shape = new GraphUpdateShape(worldPoints, gus.convex, Matrix4x4.identity, gus.minBoundsHeight); if (guo == null) { guo = new GraphUpdateObject(); } guo.bounds = shape.GetBounds(); guo.shape = shape; } guo.nnConstraint = NNConstraint.None; guo.modifyWalkability = gus.modifyWalkability; guo.setWalkability = gus.setWalkability; guo.addPenalty = gus.penaltyDelta; guo.updatePhysics = gus.updatePhysics; guo.updateErosion = gus.updateErosion; guo.resetPenaltyOnPhysics = gus.resetPenaltyOnPhysics; guo.modifyTag = gus.modifyTag; guo.setTag = gus.setTag; }