123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- using UnityEngine;
- namespace Pathfinding {
- [AddComponentMenu("Pathfinding/GraphUpdateScene")]
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_graph_update_scene.php")]
- public class GraphUpdateScene : GraphModifier {
-
- public Vector3[] points;
-
- private Vector3[] convexPoints;
-
-
-
-
-
- public bool convex = true;
-
-
-
-
-
- public float minBoundsHeight = 1;
-
-
-
-
-
-
-
-
-
- public int penaltyDelta;
-
- public bool modifyWalkability;
-
- public bool setWalkability;
-
- public bool applyOnStart = true;
-
- public bool applyOnScan = true;
-
-
-
-
-
-
-
-
- public bool updatePhysics;
-
- public bool resetPenaltyOnPhysics = true;
-
- public bool updateErosion = true;
-
-
-
-
- public bool modifyTag;
-
- public int setTag;
-
- [HideInInspector]
- public bool legacyMode = false;
-
-
-
-
- private int setTagInvert;
-
-
-
-
- private bool firstApplied;
- [SerializeField]
- private int serializedVersion = 0;
-
-
-
-
-
-
- [SerializeField]
- [UnityEngine.Serialization.FormerlySerializedAs("useWorldSpace")]
- private bool legacyUseWorldSpace;
-
- public void Start () {
- if (!Application.isPlaying) return;
-
-
- if (!firstApplied && applyOnStart) {
- Apply();
- }
- }
- public override void OnPostScan () {
- if (applyOnScan) Apply();
- }
-
-
-
-
-
-
-
-
-
-
-
- public virtual void InvertSettings () {
- setWalkability = !setWalkability;
- penaltyDelta = -penaltyDelta;
- if (setTagInvert == 0) {
- setTagInvert = setTag;
- setTag = 0;
- } else {
- setTag = setTagInvert;
- setTagInvert = 0;
- }
- }
-
-
-
-
- public void RecalcConvex () {
- convexPoints = convex ? Polygon.ConvexHullXZ(points) : null;
- }
-
-
-
-
- [System.ObsoleteAttribute("World space can no longer be used as it does not work well with rotated graphs. Use transform.InverseTransformPoint to transform points to local space.", true)]
- void ToggleUseWorldSpace () {
- }
-
-
-
-
- [System.ObsoleteAttribute("The Y coordinate is no longer important. Use the position of the object instead", true)]
- public void LockToY () {
- }
-
-
-
-
-
- public Bounds GetBounds () {
- if (points == null || points.Length == 0) {
- Bounds bounds;
- var coll = GetComponent<Collider>();
- var coll2D = GetComponent<Collider2D>();
- var rend = GetComponent<Renderer>();
- if (coll != null) bounds = coll.bounds;
- else if (coll2D != null) {
- bounds = coll2D.bounds;
- bounds.size = new Vector3(bounds.size.x, bounds.size.y, Mathf.Max(bounds.size.z, 1f));
- } else if (rend != null) {
- bounds = rend.bounds;
- } else {
- return new Bounds(Vector3.zero, Vector3.zero);
- }
- if (legacyMode && bounds.size.y < minBoundsHeight) bounds.size = new Vector3(bounds.size.x, minBoundsHeight, bounds.size.z);
- return bounds;
- } else {
- return GraphUpdateShape.GetBounds(convex ? convexPoints : points, legacyMode && legacyUseWorldSpace ? Matrix4x4.identity : transform.localToWorldMatrix, minBoundsHeight);
- }
- }
-
-
-
-
-
-
- public void Apply () {
- if (AstarPath.active == null) {
- Debug.LogError("There is no AstarPath object in the scene", this);
- return;
- }
- GraphUpdateObject guo;
- if (points == null || points.Length == 0) {
- var polygonCollider = 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 = transform.localToWorldMatrix * Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 0, 0), Vector3.one);
- var shape = new GraphUpdateShape(pts, convex, mat, minBoundsHeight);
- guo = new GraphUpdateObject(GetBounds());
- guo.shape = shape;
- } else {
- var bounds = GetBounds();
- if (bounds.center == Vector3.zero && bounds.size == Vector3.zero) {
- Debug.LogError("Cannot apply GraphUpdateScene, no points defined and no renderer or collider attached", this);
- return;
- }
- guo = new GraphUpdateObject(bounds);
- }
- } else {
- GraphUpdateShape shape;
- if (legacyMode && !legacyUseWorldSpace) {
-
- var worldPoints = new Vector3[points.Length];
- for (int i = 0; i < points.Length; i++) worldPoints[i] = transform.TransformPoint(points[i]);
- shape = new GraphUpdateShape(worldPoints, convex, Matrix4x4.identity, minBoundsHeight);
- } else {
- shape = new GraphUpdateShape(points, convex, legacyMode && legacyUseWorldSpace ? Matrix4x4.identity : transform.localToWorldMatrix, minBoundsHeight);
- }
- var bounds = shape.GetBounds();
- guo = new GraphUpdateObject(bounds);
- guo.shape = shape;
- }
- firstApplied = true;
- guo.modifyWalkability = modifyWalkability;
- guo.setWalkability = setWalkability;
- guo.addPenalty = penaltyDelta;
- guo.updatePhysics = updatePhysics;
- guo.updateErosion = updateErosion;
- guo.resetPenaltyOnPhysics = resetPenaltyOnPhysics;
- guo.modifyTag = modifyTag;
- guo.setTag = setTag;
- AstarPath.active.UpdateGraphs(guo);
- }
-
- void OnDrawGizmos () {
- OnDrawGizmos(false);
- }
-
- void OnDrawGizmosSelected () {
- OnDrawGizmos(true);
- }
-
- void OnDrawGizmos (bool selected) {
- Color c = selected ? new Color(227/255f, 61/255f, 22/255f, 1.0f) : new Color(227/255f, 61/255f, 22/255f, 0.9f);
- if (selected) {
- Gizmos.color = Color.Lerp(c, new Color(1, 1, 1, 0.2f), 0.9f);
- Bounds b = GetBounds();
- Gizmos.DrawCube(b.center, b.size);
- Gizmos.DrawWireCube(b.center, b.size);
- }
- if (points == null) return;
- if (convex) c.a *= 0.5f;
- Gizmos.color = c;
- Matrix4x4 matrix = legacyMode && legacyUseWorldSpace ? Matrix4x4.identity : transform.localToWorldMatrix;
- if (convex) {
- c.r -= 0.1f;
- c.g -= 0.2f;
- c.b -= 0.1f;
- Gizmos.color = c;
- }
- if (selected || !convex) {
- for (int i = 0; i < points.Length; i++) {
- Gizmos.DrawLine(matrix.MultiplyPoint3x4(points[i]), matrix.MultiplyPoint3x4(points[(i+1)%points.Length]));
- }
- }
- if (convex) {
- if (convexPoints == null) RecalcConvex();
- Gizmos.color = selected ? new Color(227/255f, 61/255f, 22/255f, 1.0f) : new Color(227/255f, 61/255f, 22/255f, 0.9f);
- for (int i = 0; i < convexPoints.Length; i++) {
- Gizmos.DrawLine(matrix.MultiplyPoint3x4(convexPoints[i]), matrix.MultiplyPoint3x4(convexPoints[(i+1)%convexPoints.Length]));
- }
- }
-
- var pts = convex ? convexPoints : points;
- if (selected && pts != null && pts.Length > 0) {
- Gizmos.color = new Color(1, 1, 1, 0.2f);
- float miny = pts[0].y, maxy = pts[0].y;
- for (int i = 0; i < pts.Length; i++) {
- miny = Mathf.Min(miny, pts[i].y);
- maxy = Mathf.Max(maxy, pts[i].y);
- }
- var extraHeight = Mathf.Max(minBoundsHeight - (maxy - miny), 0) * 0.5f;
- miny -= extraHeight;
- maxy += extraHeight;
- for (int i = 0; i < pts.Length; i++) {
- var next = (i+1) % pts.Length;
- var p1 = matrix.MultiplyPoint3x4(pts[i] + Vector3.up*(miny - pts[i].y));
- var p2 = matrix.MultiplyPoint3x4(pts[i] + Vector3.up*(maxy - pts[i].y));
- var p1n = matrix.MultiplyPoint3x4(pts[next] + Vector3.up*(miny - pts[next].y));
- var p2n = matrix.MultiplyPoint3x4(pts[next] + Vector3.up*(maxy - pts[next].y));
- Gizmos.DrawLine(p1, p2);
- Gizmos.DrawLine(p1, p1n);
- Gizmos.DrawLine(p2, p2n);
- }
- }
- }
-
-
-
-
- public void DisableLegacyMode () {
- if (legacyMode) {
- legacyMode = false;
- if (legacyUseWorldSpace) {
- legacyUseWorldSpace = false;
- for (int i = 0; i < points.Length; i++) {
- points[i] = transform.InverseTransformPoint(points[i]);
- }
- RecalcConvex();
- }
- }
- }
- protected override void Awake () {
- if (serializedVersion == 0) {
-
- if (points != null && points.Length > 0) legacyMode = true;
- serializedVersion = 1;
- }
- base.Awake();
- }
- }
- }
|