Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NavMeshLink.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.AI;
  4. #pragma warning disable IDE1006 // Unity-specific lower case public property names
  5. namespace Unity.AI.Navigation
  6. {
  7. /// <summary> Component used to create a navigable link between two NavMesh locations. </summary>
  8. [ExecuteAlways]
  9. [DefaultExecutionOrder(-101)]
  10. [AddComponentMenu("Navigation/NavMeshLink", 33)]
  11. [HelpURL(HelpUrls.Manual + "NavMeshLink.html")]
  12. public partial class NavMeshLink : MonoBehaviour
  13. {
  14. [SerializeField]
  15. int m_AgentTypeID;
  16. [SerializeField]
  17. Vector3 m_StartPoint = new(0.0f, 0.0f, -2.5f);
  18. [SerializeField]
  19. Vector3 m_EndPoint = new(0.0f, 0.0f, 2.5f);
  20. #if ENABLE_NAVIGATION_OFFMESHLINK_TO_NAVMESHLINK
  21. [SerializeField]
  22. Transform m_StartTransform;
  23. [SerializeField]
  24. Transform m_EndTransform;
  25. [SerializeField]
  26. bool m_Activated = true;
  27. #endif
  28. [SerializeField]
  29. float m_Width;
  30. [SerializeField]
  31. float m_CostModifier = -1;
  32. [SerializeField]
  33. bool m_Bidirectional = true;
  34. [SerializeField]
  35. bool m_AutoUpdatePosition;
  36. [SerializeField]
  37. int m_Area;
  38. /// <summary> Gets or sets the type of agent that can use the link. </summary>
  39. public int agentTypeID
  40. {
  41. get => m_AgentTypeID;
  42. set
  43. {
  44. if (value == m_AgentTypeID)
  45. return;
  46. m_AgentTypeID = value;
  47. UpdateLink();
  48. }
  49. }
  50. /// <summary> Gets or sets the local position at the middle of the link's start edge, relative to the GameObject origin. </summary>
  51. /// <remarks> The position is translated and rotated by <see cref="startTransform"/> when `startTransform` is `null` or equal to the GameObject transform. Otherwise, the link is only translated by `startTransform`. The scale of the specified transform is never used.</remarks>
  52. public Vector3 startPoint
  53. {
  54. get => m_StartPoint;
  55. set
  56. {
  57. if (value == m_StartPoint)
  58. return;
  59. m_StartPoint = value;
  60. UpdateLink();
  61. }
  62. }
  63. /// <summary> Gets or sets the local position at the middle of the link's end edge, relative to the GameObject origin. </summary>
  64. /// <remarks> The position is translated and rotated by <see cref="endTransform"/> when `endTransform` is `null` or equal to the GameObject transform. Otherwise, the link is only translated by `endTransform`. The scale of the specified transform is never used.</remarks>
  65. public Vector3 endPoint
  66. {
  67. get => m_EndPoint;
  68. set
  69. {
  70. if (value == m_EndPoint)
  71. return;
  72. m_EndPoint = value;
  73. UpdateLink();
  74. }
  75. }
  76. #if ENABLE_NAVIGATION_OFFMESHLINK_TO_NAVMESHLINK
  77. /// <summary> Gets or sets the <see cref="Transform"/> tracked by the middle of the link's start edge. </summary>
  78. /// <remarks> When this property is `null` or equal to the GameObject transform, it applies the GameObject's translation and rotation as a transform to <see cref="startPoint"/> in order to establish the world position of the link's start edge. When this property takes any other value, it applies only its translation to `startPoint`.</remarks>
  79. public Transform startTransform
  80. {
  81. get => m_StartTransform;
  82. set
  83. {
  84. if (value == m_StartTransform)
  85. return;
  86. m_StartTransform = value;
  87. if (m_StartTransform != null)
  88. m_StartPoint = Vector3.zero;
  89. UpdateLink();
  90. }
  91. }
  92. /// <summary> Gets or sets the Transform tracked by the middle of the link's end edge. </summary>
  93. /// <remarks> When this property is `null` or equal to the GameObject transform, it applies the GameObject's translation and rotation as a transform to <see cref="endPoint"/> in order to establish the world position of the link's end edge. When this property takes any other value, it applies only its translation to the `endPoint`.</remarks>
  94. public Transform endTransform
  95. {
  96. get => m_EndTransform;
  97. set
  98. {
  99. if (value == m_EndTransform)
  100. return;
  101. m_EndTransform = value;
  102. if (m_EndTransform != null)
  103. m_EndPoint = Vector3.zero;
  104. UpdateLink();
  105. }
  106. }
  107. internal bool startRelativeToThisGameObject => m_StartTransform == null || m_StartTransform == transform;
  108. internal bool endRelativeToThisGameObject => m_EndTransform == null || m_EndTransform == transform;
  109. #endif
  110. // Start position relative to the game object position
  111. internal Vector3 localStartPosition
  112. {
  113. get
  114. {
  115. #if ENABLE_NAVIGATION_OFFMESHLINK_TO_NAVMESHLINK
  116. if (!startRelativeToThisGameObject)
  117. return transform.InverseTransformPoint(m_StartTransform.position + m_StartPoint);
  118. return m_StartPoint;
  119. #else
  120. return m_StartPoint;
  121. #endif
  122. }
  123. }
  124. // End position relative to the game object position
  125. internal Vector3 localEndPosition
  126. {
  127. get
  128. {
  129. #if ENABLE_NAVIGATION_OFFMESHLINK_TO_NAVMESHLINK
  130. if (!endRelativeToThisGameObject)
  131. return transform.InverseTransformPoint(m_EndTransform.position + m_EndPoint);
  132. return m_EndPoint;
  133. #else
  134. return m_EndPoint;
  135. #endif
  136. }
  137. }
  138. /// <summary> The width of the segments making up the ends of the link. </summary>
  139. /// <remarks> The segments are created perpendicular to the line from start to end, in the XZ plane of the GameObject. </remarks>
  140. public float width
  141. {
  142. get => m_Width;
  143. set
  144. {
  145. if (value.Equals(m_Width))
  146. return;
  147. m_Width = value;
  148. UpdateLink();
  149. }
  150. }
  151. /// <summary> Gets or sets a value that determines the cost of traversing the link.</summary>
  152. /// <remarks> A negative value implies that the traversal cost is obtained based on the area type.
  153. /// A positive or zero value applies immediately, overriding the cost associated with the area type.</remarks>
  154. public float costModifier
  155. {
  156. get => m_CostModifier;
  157. set
  158. {
  159. if (value.Equals(m_CostModifier))
  160. return;
  161. m_CostModifier = value;
  162. UpdateLink();
  163. }
  164. }
  165. /// <summary> Gets or sets whether the link can be traversed in both directions. </summary>
  166. /// <remarks> A link that connects to NavMeshes at both ends can always be traversed from the start position to the end position. When this property is set to `true` it allows the agents to traverse the link also in the direction from end to start. When the value is `false` the agents will never move over the link from the end position to the start position.</remarks>
  167. public bool bidirectional
  168. {
  169. get => m_Bidirectional;
  170. set
  171. {
  172. if (value == m_Bidirectional)
  173. return;
  174. m_Bidirectional = value;
  175. UpdateLink();
  176. }
  177. }
  178. /// <summary> Gets or sets whether the world positions of the link's edges update whenever
  179. /// the GameObject transform changes at runtime. </summary>
  180. public bool autoUpdate
  181. {
  182. get => m_AutoUpdatePosition;
  183. set
  184. {
  185. if (value == m_AutoUpdatePosition)
  186. return;
  187. m_AutoUpdatePosition = value;
  188. if (m_AutoUpdatePosition)
  189. AddTracking(this);
  190. else
  191. RemoveTracking(this);
  192. }
  193. }
  194. /// <summary> The area type of the link. </summary>
  195. public int area
  196. {
  197. get => m_Area;
  198. set
  199. {
  200. if (value == m_Area)
  201. return;
  202. m_Area = value;
  203. UpdateLink();
  204. }
  205. }
  206. #if ENABLE_NAVIGATION_OFFMESHLINK_TO_NAVMESHLINK
  207. /// <summary> Gets or sets whether the link can be traversed by agents. </summary>
  208. /// <remarks> When this property is set to `true` it allows the agents to traverse the link. When the value is `false` no paths pass through this link and no agent can traverse it as part of their autonomous movement. </remarks>
  209. public bool activated
  210. {
  211. get => m_Activated;
  212. set
  213. {
  214. m_Activated = value;
  215. NavMesh.SetLinkActive(m_LinkInstance, m_Activated);
  216. }
  217. }
  218. /// <summary> Checks whether any agent occupies the link at this moment in time. </summary>
  219. /// <remarks> This property evaluates the internal state of the link every time it is used. </remarks>
  220. public bool occupied => NavMesh.IsLinkOccupied(m_LinkInstance);
  221. #endif
  222. NavMeshLinkInstance m_LinkInstance;
  223. #if ENABLE_NAVIGATION_OFFMESHLINK_TO_NAVMESHLINK
  224. bool m_StartTransformWasEmpty = true;
  225. bool m_EndTransformWasEmpty = true;
  226. Vector3 m_LastStartWorldPosition = Vector3.positiveInfinity;
  227. Vector3 m_LastEndWorldPosition = Vector3.positiveInfinity;
  228. #endif
  229. Vector3 m_LastPosition = Vector3.positiveInfinity;
  230. Quaternion m_LastRotation = Quaternion.identity;
  231. static readonly List<NavMeshLink> s_Tracked = new();
  232. void OnEnable()
  233. {
  234. AddLink();
  235. if (m_AutoUpdatePosition && NavMesh.IsLinkValid(m_LinkInstance))
  236. AddTracking(this);
  237. }
  238. void OnDisable()
  239. {
  240. RemoveTracking(this);
  241. NavMesh.RemoveLink(m_LinkInstance);
  242. }
  243. /// <summary> Replaces the link with a new one using the current settings. </summary>
  244. public void UpdateLink()
  245. {
  246. if (!isActiveAndEnabled)
  247. return;
  248. NavMesh.RemoveLink(m_LinkInstance);
  249. AddLink();
  250. }
  251. static void AddTracking(NavMeshLink link)
  252. {
  253. #if UNITY_EDITOR
  254. if (s_Tracked.Contains(link))
  255. {
  256. Debug.LogError("Link is already tracked: " + link);
  257. return;
  258. }
  259. #endif
  260. if (s_Tracked.Count == 0)
  261. NavMesh.onPreUpdate += UpdateTrackedInstances;
  262. s_Tracked.Add(link);
  263. }
  264. static void RemoveTracking(NavMeshLink link)
  265. {
  266. s_Tracked.Remove(link);
  267. if (s_Tracked.Count == 0)
  268. NavMesh.onPreUpdate -= UpdateTrackedInstances;
  269. }
  270. void AddLink()
  271. {
  272. #if UNITY_EDITOR
  273. if (NavMesh.IsLinkValid(m_LinkInstance))
  274. {
  275. Debug.LogError("Link is already added: " + this);
  276. return;
  277. }
  278. #endif
  279. var link = new NavMeshLinkData
  280. {
  281. startPosition = localStartPosition,
  282. endPosition = localEndPosition,
  283. width = m_Width,
  284. costModifier = m_CostModifier,
  285. bidirectional = m_Bidirectional,
  286. area = m_Area,
  287. agentTypeID = m_AgentTypeID,
  288. };
  289. m_LinkInstance = NavMesh.AddLink(link, transform.position, transform.rotation);
  290. if (NavMesh.IsLinkValid(m_LinkInstance))
  291. {
  292. NavMesh.SetLinkOwner(m_LinkInstance, this);
  293. #if ENABLE_NAVIGATION_OFFMESHLINK_TO_NAVMESHLINK
  294. NavMesh.SetLinkActive(m_LinkInstance, m_Activated);
  295. #endif
  296. }
  297. m_LastPosition = transform.position;
  298. m_LastRotation = transform.rotation;
  299. #if ENABLE_NAVIGATION_OFFMESHLINK_TO_NAVMESHLINK
  300. RecordEndpointTransforms();
  301. m_LastStartWorldPosition = transform.TransformPoint(localStartPosition);
  302. m_LastEndWorldPosition = transform.TransformPoint(localEndPosition);
  303. #endif
  304. }
  305. internal void RecordEndpointTransforms()
  306. {
  307. #if ENABLE_NAVIGATION_OFFMESHLINK_TO_NAVMESHLINK
  308. m_StartTransformWasEmpty = m_StartTransform == null;
  309. m_EndTransformWasEmpty = m_EndTransform == null;
  310. #endif
  311. }
  312. internal bool HaveTransformsChanged()
  313. {
  314. #if ENABLE_NAVIGATION_OFFMESHLINK_TO_NAVMESHLINK
  315. if (m_StartTransform == null && m_EndTransform == null &&
  316. m_StartTransformWasEmpty && m_EndTransformWasEmpty &&
  317. transform.position == m_LastPosition && transform.rotation == m_LastRotation)
  318. return false;
  319. var startWorldPos = startRelativeToThisGameObject ? transform.TransformPoint(m_StartPoint) : m_StartTransform!.position + m_StartPoint;
  320. if (startWorldPos != m_LastStartWorldPosition)
  321. return true;
  322. var endWorldPos = endRelativeToThisGameObject ? transform.TransformPoint(m_EndPoint) : m_EndTransform!.position + m_EndPoint;
  323. return endWorldPos != m_LastEndWorldPosition;
  324. #else
  325. if (m_LastPosition != transform.position)
  326. return true;
  327. if (m_LastRotation != transform.rotation)
  328. return true;
  329. return false;
  330. #endif
  331. }
  332. void OnDidApplyAnimationProperties()
  333. {
  334. UpdateLink();
  335. }
  336. static void UpdateTrackedInstances()
  337. {
  338. foreach (var instance in s_Tracked)
  339. {
  340. if (instance.HaveTransformsChanged())
  341. instance.UpdateLink();
  342. instance.RecordEndpointTransforms();
  343. }
  344. }
  345. #if UNITY_EDITOR
  346. void OnValidate()
  347. {
  348. m_Width = Mathf.Max(0.0f, m_Width);
  349. if (!NavMesh.IsLinkValid(m_LinkInstance))
  350. return;
  351. #if ENABLE_NAVIGATION_OFFMESHLINK_TO_NAVMESHLINK
  352. if (m_StartTransform == null)
  353. m_StartTransform = transform;
  354. if (m_EndTransform == null)
  355. m_EndTransform = transform;
  356. #endif
  357. if (!UnityEditor.EditorApplication.isPlaying)
  358. UpdateLink();
  359. if (!m_AutoUpdatePosition)
  360. {
  361. RemoveTracking(this);
  362. }
  363. else if (!s_Tracked.Contains(this))
  364. {
  365. AddTracking(this);
  366. }
  367. }
  368. #endif
  369. }
  370. }