RelevantGraphSurface.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEngine;
  2. namespace Pathfinding {
  3. /// <summary>
  4. /// Pruning of recast navmesh regions.
  5. /// A RelevantGraphSurface component placed in the scene specifies that
  6. /// the navmesh region it is inside should be included in the navmesh.
  7. ///
  8. /// See: Pathfinding.RecastGraph.relevantGraphSurfaceMode
  9. /// </summary>
  10. [AddComponentMenu("Pathfinding/Navmesh/RelevantGraphSurface")]
  11. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_relevant_graph_surface.php")]
  12. public class RelevantGraphSurface : VersionedMonoBehaviour {
  13. private static RelevantGraphSurface root;
  14. public float maxRange = 1;
  15. private RelevantGraphSurface prev;
  16. private RelevantGraphSurface next;
  17. private Vector3 position;
  18. public Vector3 Position {
  19. get { return position; }
  20. }
  21. public RelevantGraphSurface Next {
  22. get { return next; }
  23. }
  24. public RelevantGraphSurface Prev {
  25. get { return prev; }
  26. }
  27. public static RelevantGraphSurface Root {
  28. get { return root; }
  29. }
  30. public void UpdatePosition () {
  31. position = transform.position;
  32. }
  33. void OnEnable () {
  34. UpdatePosition();
  35. if (root == null) {
  36. root = this;
  37. } else {
  38. next = root;
  39. root.prev = this;
  40. root = this;
  41. }
  42. }
  43. void OnDisable () {
  44. if (root == this) {
  45. root = next;
  46. if (root != null) root.prev = null;
  47. } else {
  48. if (prev != null) prev.next = next;
  49. if (next != null) next.prev = prev;
  50. }
  51. prev = null;
  52. next = null;
  53. }
  54. /// <summary>
  55. /// Updates the positions of all relevant graph surface components.
  56. /// Required to be able to use the position property reliably.
  57. /// </summary>
  58. public static void UpdateAllPositions () {
  59. RelevantGraphSurface c = root;
  60. while (c != null) { c.UpdatePosition(); c = c.Next; }
  61. }
  62. public static void FindAllGraphSurfaces () {
  63. var srf = GameObject.FindObjectsOfType(typeof(RelevantGraphSurface)) as RelevantGraphSurface[];
  64. for (int i = 0; i < srf.Length; i++) {
  65. srf[i].OnDisable();
  66. srf[i].OnEnable();
  67. }
  68. }
  69. public void OnDrawGizmos () {
  70. Gizmos.color = new Color(57/255f, 211/255f, 46/255f, 0.4f);
  71. Gizmos.DrawLine(transform.position - Vector3.up*maxRange, transform.position + Vector3.up*maxRange);
  72. }
  73. public void OnDrawGizmosSelected () {
  74. Gizmos.color = new Color(57/255f, 211/255f, 46/255f);
  75. Gizmos.DrawLine(transform.position - Vector3.up*maxRange, transform.position + Vector3.up*maxRange);
  76. }
  77. }
  78. }