No Description
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.

NavMeshModifier.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.AI;
  4. namespace Unity.AI.Navigation
  5. {
  6. #if UNITY_2022_2_OR_NEWER
  7. /// <summary> Component that modifies the properties of the GameObjects used for building a NavMesh. </summary>
  8. /// <remarks>The properties apply to the current GameObject and can be optionally applied recursively to all its children
  9. /// in the hierarchy. This modifier overrides the properties set to this GameObject by
  10. /// any other NavMeshModifier in the parent hierarchy.</remarks>
  11. #else
  12. /// <summary> Component that modifies the properties of the GameObjects used for building a NavMesh. </summary>
  13. /// <remarks>The properties apply to the current GameObject and recursively to all its children
  14. /// in the hierarchy. This modifier overrides the properties set to this GameObject by
  15. /// any other NavMeshModifier in the parent hierarchy.</remarks>
  16. #endif
  17. [ExecuteAlways]
  18. [AddComponentMenu("Navigation/NavMeshModifier", 32)]
  19. [HelpURL(HelpUrls.Manual + "NavMeshModifier.html")]
  20. public class NavMeshModifier : MonoBehaviour
  21. {
  22. [SerializeField]
  23. bool m_OverrideArea;
  24. [SerializeField]
  25. int m_Area;
  26. #if UNITY_2022_2_OR_NEWER
  27. [SerializeField]
  28. bool m_OverrideGenerateLinks;
  29. [SerializeField]
  30. bool m_GenerateLinks;
  31. #endif
  32. [SerializeField]
  33. bool m_IgnoreFromBuild;
  34. #if UNITY_2022_2_OR_NEWER
  35. [SerializeField]
  36. bool m_ApplyToChildren = true;
  37. #endif
  38. // List of agent types the modifier is applied for.
  39. // Special values: empty == None, m_AffectedAgents[0] =-1 == All.
  40. [SerializeField]
  41. List<int> m_AffectedAgents = new List<int>(new int[] { -1 }); // Default value is All
  42. /// <summary> Gets or sets whether to assign the <see cref="area"/> type to this object instead of the <see cref="NavMeshSurface.defaultArea"/>. </summary>
  43. /// <remarks> The area type information is used when baking the NavMesh. </remarks>
  44. /// <seealso href="https://docs.unity3d.com/Manual/nav-AreasAndCosts.html"/>
  45. public bool overrideArea { get { return m_OverrideArea; } set { m_OverrideArea = value; } }
  46. /// <summary> Gets or sets the area type applied by this GameObject. </summary>
  47. /// <remarks> The range of useful values is from 0 to 31. Higher values always take precedence over lower values in the case when the surfaces of more GameObjects intersect each other to produce a NavMesh in the same region. A value of 1 has the highest priority over all the other types and it means "not walkable". Consequently, the surface of a GameObject with an <c>area</c> of 1 produces a hole in the NavMesh. This property has the same meaning as <see cref="NavMeshBuildSource.area"/>.</remarks>
  48. /// <seealso href="https://docs.unity3d.com/Manual/nav-AreasAndCosts.html"/>
  49. public int area { get { return m_Area; } set { m_Area = value; } }
  50. #if UNITY_2022_2_OR_NEWER
  51. /// <summary> Gets or sets whether the default links generation condition for the GameObject and its children should be overridden. </summary>
  52. public bool overrideGenerateLinks { get { return m_OverrideGenerateLinks; } set { m_OverrideGenerateLinks = value; } }
  53. /// <summary> Gets or sets whether this object is included in the link generation process. </summary>
  54. public bool generateLinks { get { return m_GenerateLinks; } set { m_GenerateLinks = value; } }
  55. #endif
  56. /// <summary> Gets or sets whether the NavMesh building process ignores this GameObject and its children. </summary>
  57. public bool ignoreFromBuild { get { return m_IgnoreFromBuild; } set { m_IgnoreFromBuild = value; } }
  58. #if UNITY_2022_2_OR_NEWER
  59. /// <summary> Gets or sets whether this GameObject's children also use the modifier settings. </summary>
  60. public bool applyToChildren { get { return m_ApplyToChildren; } set { m_ApplyToChildren = value; } }
  61. #endif
  62. static readonly List<NavMeshModifier> s_NavMeshModifiers = new List<NavMeshModifier>();
  63. /// <summary> Gets the list of all the <see cref="NavMeshModifier"/> components that are currently active in the scene. </summary>
  64. public static List<NavMeshModifier> activeModifiers
  65. {
  66. get { return s_NavMeshModifiers; }
  67. }
  68. void OnEnable()
  69. {
  70. if (!s_NavMeshModifiers.Contains(this))
  71. s_NavMeshModifiers.Add(this);
  72. }
  73. void OnDisable()
  74. {
  75. s_NavMeshModifiers.Remove(this);
  76. }
  77. /// <summary> Verifies whether this modifier can affect in any way the generation of a NavMesh for a given agent type. </summary>
  78. /// <param name="agentTypeID"> The identifier of an agent type that originates from <see cref="NavMeshBuildSettings.agentTypeID"/>. </param>
  79. /// <returns> <c>true</c> if this component can affect the NavMesh built for the given agent type; otherwise <c>false</c>. </returns>
  80. public bool AffectsAgentType(int agentTypeID)
  81. {
  82. if (m_AffectedAgents.Count == 0)
  83. return false;
  84. if (m_AffectedAgents[0] == -1)
  85. return true;
  86. return m_AffectedAgents.IndexOf(agentTypeID) != -1;
  87. }
  88. }
  89. }