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.

NavMeshModifierVolume.cs 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.AI;
  4. namespace Unity.AI.Navigation
  5. {
  6. /// <summary> Component used by the NavMesh building process to assign a different area type to the region inside the specified volume.</summary>
  7. [ExecuteAlways]
  8. [AddComponentMenu("Navigation/NavMeshModifierVolume", 31)]
  9. [HelpURL(HelpUrls.Manual + "NavMeshModifierVolume.html")]
  10. public class NavMeshModifierVolume : MonoBehaviour
  11. {
  12. [SerializeField]
  13. Vector3 m_Size = new Vector3(4.0f, 3.0f, 4.0f);
  14. [SerializeField]
  15. Vector3 m_Center = new Vector3(0, 1.0f, 0);
  16. [SerializeField]
  17. int m_Area;
  18. /// <summary> Gets or sets the dimensions of the cuboid modifier volume. </summary>
  19. /// <remarks> The dimensions apply in the local space of the GameObject. </remarks>
  20. public Vector3 size { get { return m_Size; } set { m_Size = value; } }
  21. /// <summary> Gets or sets the center position of the modifier volume. </summary>
  22. /// <remarks> The position is relative to the GameObject transform. </remarks>
  23. public Vector3 center { get { return m_Center; } set { m_Center = value; } }
  24. /// <summary> Gets or sets the area type that will be enforced by the volume during the generation of the NavMesh. </summary>
  25. /// <remarks> The range of useful values is from 0 to 31. Higher values always take precedence over lower values in the case when more volumes intersect each other. A value of 1 has the highest priority over all the other types and it means "not walkable". Consequently, a volume 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>
  26. /// <seealso href="https://docs.unity3d.com/Manual/nav-AreasAndCosts.html"/>
  27. public int area { get { return m_Area; } set { m_Area = value; } }
  28. // List of agent types the modifier is applied for.
  29. // Special values: empty == None, m_AffectedAgents[0] =-1 == All.
  30. [SerializeField]
  31. List<int> m_AffectedAgents = new List<int>(new int[] { -1 }); // Default value is All
  32. static readonly List<NavMeshModifierVolume> s_NavMeshModifiers = new List<NavMeshModifierVolume>();
  33. /// <summary> Gets the list of all the <see cref="NavMeshModifierVolume"/> components that are currently active in the scene. </summary>
  34. public static List<NavMeshModifierVolume> activeModifiers
  35. {
  36. get { return s_NavMeshModifiers; }
  37. }
  38. void OnEnable()
  39. {
  40. if (!s_NavMeshModifiers.Contains(this))
  41. s_NavMeshModifiers.Add(this);
  42. }
  43. void OnDisable()
  44. {
  45. s_NavMeshModifiers.Remove(this);
  46. }
  47. /// <summary> Verifies whether this modifier volume can affect in any way the generation of a NavMesh for a given agent type. </summary>
  48. /// <param name="agentTypeID"> The identifier of an agent type that originates from <see cref="NavMeshBuildSettings.agentTypeID"/>. </param>
  49. /// <returns> <c>true</c> if this component can affect the NavMesh built for the given agent type; otherwise <c>false</c>. </returns>
  50. public bool AffectsAgentType(int agentTypeID)
  51. {
  52. if (m_AffectedAgents.Count == 0)
  53. return false;
  54. if (m_AffectedAgents[0] == -1)
  55. return true;
  56. return m_AffectedAgents.IndexOf(agentTypeID) != -1;
  57. }
  58. }
  59. }