NavMeshLink.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System.Collections.Generic;
  2. namespace UnityEngine.AI
  3. {
  4. [ExecuteInEditMode]
  5. [DefaultExecutionOrder(-101)]
  6. [AddComponentMenu("Navigation/NavMeshLink", 33)]
  7. [HelpURL("https://github.com/Unity-Technologies/NavMeshComponents#documentation-draft")]
  8. public class NavMeshLink : MonoBehaviour
  9. {
  10. [SerializeField]
  11. int m_AgentTypeID;
  12. public int agentTypeID { get { return m_AgentTypeID; } set { m_AgentTypeID = value; UpdateLink(); } }
  13. [SerializeField]
  14. Vector3 m_StartPoint = new Vector3(0.0f, 0.0f, -2.5f);
  15. public Vector3 startPoint { get { return m_StartPoint; } set { m_StartPoint = value; UpdateLink(); } }
  16. [SerializeField]
  17. Vector3 m_EndPoint = new Vector3(0.0f, 0.0f, 2.5f);
  18. public Vector3 endPoint { get { return m_EndPoint; } set { m_EndPoint = value; UpdateLink(); } }
  19. [SerializeField]
  20. float m_Width;
  21. public float width { get { return m_Width; } set { m_Width = value; UpdateLink(); } }
  22. [SerializeField]
  23. int m_CostModifier = -1;
  24. public int costModifier { get { return m_CostModifier; } set { m_CostModifier = value; UpdateLink(); } }
  25. [SerializeField]
  26. bool m_Bidirectional = true;
  27. public bool bidirectional { get { return m_Bidirectional; } set { m_Bidirectional = value; UpdateLink(); } }
  28. [SerializeField]
  29. bool m_AutoUpdatePosition;
  30. public bool autoUpdate { get { return m_AutoUpdatePosition; } set { SetAutoUpdate(value); } }
  31. [SerializeField]
  32. int m_Area;
  33. public int area { get { return m_Area; } set { m_Area = value; UpdateLink(); } }
  34. NavMeshLinkInstance m_LinkInstance = new NavMeshLinkInstance();
  35. Vector3 m_LastPosition = Vector3.zero;
  36. Quaternion m_LastRotation = Quaternion.identity;
  37. static readonly List<NavMeshLink> s_Tracked = new List<NavMeshLink>();
  38. void OnEnable()
  39. {
  40. AddLink();
  41. if (m_AutoUpdatePosition && m_LinkInstance.valid)
  42. AddTracking(this);
  43. }
  44. void OnDisable()
  45. {
  46. RemoveTracking(this);
  47. m_LinkInstance.Remove();
  48. }
  49. public void UpdateLink()
  50. {
  51. m_LinkInstance.Remove();
  52. AddLink();
  53. }
  54. static void AddTracking(NavMeshLink link)
  55. {
  56. #if UNITY_EDITOR
  57. if (s_Tracked.Contains(link))
  58. {
  59. Debug.LogError("Link is already tracked: " + link);
  60. return;
  61. }
  62. #endif
  63. if (s_Tracked.Count == 0)
  64. NavMesh.onPreUpdate += UpdateTrackedInstances;
  65. s_Tracked.Add(link);
  66. }
  67. static void RemoveTracking(NavMeshLink link)
  68. {
  69. s_Tracked.Remove(link);
  70. if (s_Tracked.Count == 0)
  71. NavMesh.onPreUpdate -= UpdateTrackedInstances;
  72. }
  73. void SetAutoUpdate(bool value)
  74. {
  75. if (m_AutoUpdatePosition == value)
  76. return;
  77. m_AutoUpdatePosition = value;
  78. if (value)
  79. AddTracking(this);
  80. else
  81. RemoveTracking(this);
  82. }
  83. void AddLink()
  84. {
  85. #if UNITY_EDITOR
  86. if (m_LinkInstance.valid)
  87. {
  88. Debug.LogError("Link is already added: " + this);
  89. return;
  90. }
  91. #endif
  92. var link = new NavMeshLinkData();
  93. link.startPosition = m_StartPoint;
  94. link.endPosition = m_EndPoint;
  95. link.width = m_Width;
  96. link.costModifier = m_CostModifier;
  97. link.bidirectional = m_Bidirectional;
  98. link.area = m_Area;
  99. link.agentTypeID = m_AgentTypeID;
  100. m_LinkInstance = NavMesh.AddLink(link, transform.position, transform.rotation);
  101. if (m_LinkInstance.valid)
  102. m_LinkInstance.owner = this;
  103. m_LastPosition = transform.position;
  104. m_LastRotation = transform.rotation;
  105. }
  106. bool HasTransformChanged()
  107. {
  108. if (m_LastPosition != transform.position) return true;
  109. if (m_LastRotation != transform.rotation) return true;
  110. return false;
  111. }
  112. void OnDidApplyAnimationProperties()
  113. {
  114. UpdateLink();
  115. }
  116. static void UpdateTrackedInstances()
  117. {
  118. foreach (var instance in s_Tracked)
  119. {
  120. if (instance.HasTransformChanged())
  121. instance.UpdateLink();
  122. }
  123. }
  124. #if UNITY_EDITOR
  125. void OnValidate()
  126. {
  127. m_Width = Mathf.Max(0.0f, m_Width);
  128. if (!m_LinkInstance.valid)
  129. return;
  130. UpdateLink();
  131. if (!m_AutoUpdatePosition)
  132. {
  133. RemoveTracking(this);
  134. }
  135. else if (!s_Tracked.Contains(this))
  136. {
  137. AddTracking(this);
  138. }
  139. }
  140. #endif
  141. }
  142. }