This commit is contained in:
j.mei7
2022-03-01 20:47:08 +01:00
parent 37f2c289bc
commit 7e0f3e6930
188 changed files with 218108 additions and 0 deletions

View File

@@ -0,0 +1,296 @@
using System;
using System.Collections.Generic;
using UnityEngine.Tilemaps;
namespace UnityEngine.AI
{
class NavMeshBuilder2dWrapper
{
public Dictionary<Sprite, Mesh> map;
public Dictionary<uint, Mesh> coliderMap;
public int defaultArea;
public int layerMask;
public int agentID;
public bool overrideByGrid;
public GameObject useMeshPrefab;
public bool compressBounds;
public Vector3 overrideVector;
public NavMeshCollectGeometry CollectGeometry;
public CollectObjects2d CollectObjects;
public GameObject parent;
public bool hideEditorLogs;
public NavMeshBuilder2dWrapper()
{
map = new Dictionary<Sprite, Mesh>();
coliderMap = new Dictionary<uint, Mesh>();
}
public Mesh GetMesh(Sprite sprite)
{
Mesh mesh;
if (map.ContainsKey(sprite))
{
mesh = map[sprite];
}
else
{
mesh = new Mesh();
NavMeshBuilder2d.sprite2mesh(sprite, mesh);
map.Add(sprite, mesh);
}
return mesh;
}
internal Mesh GetMesh(Collider2D collider)
{
#if UNITY_2019_3_OR_NEWER
Mesh mesh;
uint hash = collider.GetShapeHash();
if (coliderMap.ContainsKey(hash))
{
mesh = coliderMap[hash];
}
else
{
mesh = collider.CreateMesh(false, false);
coliderMap.Add(hash, mesh);
}
return mesh;
#else
throw new InvalidOperationException("PhysicsColliders supported in Unity 2019.3 and higher.");
#endif
}
internal IEnumerable<GameObject> GetRoot()
{
switch (CollectObjects)
{
case CollectObjects2d.Children: return new[] { parent };
case CollectObjects2d.Volume:
case CollectObjects2d.All:
default:
return new[] { GameObject.FindObjectOfType<Grid>().gameObject };
}
}
}
class NavMeshBuilder2d
{
internal static void CollectSources(List<NavMeshBuildSource> sources, NavMeshBuilder2dWrapper builder)
{
var root = builder.GetRoot();
foreach (var it in root)
{
CollectSources(it, sources, builder);
}
}
private static void CollectSources(GameObject root, List<NavMeshBuildSource> sources, NavMeshBuilder2dWrapper builder)
{
foreach (var modifier in root.GetComponentsInChildren<NavMeshModifier>())
{
if (((0x1 << modifier.gameObject.layer) & builder.layerMask) == 0)
{
continue;
}
if (!modifier.AffectsAgentType(builder.agentID))
{
continue;
}
int area = builder.defaultArea;
//if it is walkable
if (builder.defaultArea != 1 && !modifier.ignoreFromBuild)
{
var tilemap = modifier.GetComponent<Tilemap>();
if (tilemap != null)
{
if (builder.compressBounds)
{
tilemap.CompressBounds();
}
if (!builder.hideEditorLogs) Debug.Log($"Walkable Bounds [{tilemap.name}]: {tilemap.localBounds}");
var box = BoxBoundSource(NavMeshSurface2d.GetWorldBounds(tilemap.transform.localToWorldMatrix, tilemap.localBounds));
box.area = builder.defaultArea;
sources.Add(box);
}
}
if (modifier.overrideArea)
{
area = modifier.area;
}
if (!modifier.ignoreFromBuild)
{
if (builder.CollectGeometry == NavMeshCollectGeometry.PhysicsColliders)
{
CollectSources(sources, modifier, area, builder);
}
else
{
var tilemap = modifier.GetComponent<Tilemap>();
if (tilemap != null)
{
CollectTileSources(sources, tilemap, area, builder);
}
var sprite = modifier.GetComponent<SpriteRenderer>();
if (sprite != null)
{
CollectSources(sources, sprite, area, builder);
}
}
}
}
if (!builder.hideEditorLogs) Debug.Log("Sources " + sources.Count);
}
private static void CollectSources(List<NavMeshBuildSource> sources, SpriteRenderer sprite, int area, NavMeshBuilder2dWrapper builder)
{
if (sprite == null)
{
return;
}
var src = new NavMeshBuildSource();
src.shape = NavMeshBuildSourceShape.Mesh;
src.area = area;
Mesh mesh;
mesh = builder.GetMesh(sprite.sprite);
if (mesh == null)
{
if (!builder.hideEditorLogs) Debug.Log($"{sprite.name} mesh is null");
return;
}
src.transform = Matrix4x4.TRS(Vector3.Scale(sprite.transform.position, builder.overrideVector), sprite.transform.rotation, sprite.transform.lossyScale);
src.sourceObject = mesh;
sources.Add(src);
}
private static void CollectSources(List<NavMeshBuildSource> sources, NavMeshModifier modifier, int area, NavMeshBuilder2dWrapper builder)
{
var collider = modifier.GetComponent<Collider2D>();
if (collider == null)
{
return;
}
if (collider.usedByComposite)
{
collider = collider.GetComponent<CompositeCollider2D>();
}
var src = new NavMeshBuildSource();
src.shape = NavMeshBuildSourceShape.Mesh;
src.area = area;
Mesh mesh;
mesh = builder.GetMesh(collider);
if (mesh == null)
{
if (!builder.hideEditorLogs) Debug.Log($"{collider.name} mesh is null");
return;
}
if (collider.attachedRigidbody)
{
src.transform = Matrix4x4.TRS(Vector3.Scale(collider.attachedRigidbody.transform.position, builder.overrideVector), collider.attachedRigidbody.transform.rotation, Vector3.one);
}
else
{
src.transform = Matrix4x4.identity;
}
src.sourceObject = mesh;
sources.Add(src);
}
static private void CollectTileSources(List<NavMeshBuildSource> sources, Tilemap tilemap, int area, NavMeshBuilder2dWrapper builder)
{
var bound = tilemap.cellBounds;
var vec3int = new Vector3Int(0, 0, 0);
var size = new Vector3(tilemap.layoutGrid.cellSize.x, tilemap.layoutGrid.cellSize.y, 0);
Mesh sharedMesh = null;
Quaternion rot = default;
var src = new NavMeshBuildSource();
src.shape = NavMeshBuildSourceShape.Mesh;
src.area = area;
Mesh mesh;
if (builder.useMeshPrefab != null)
{
sharedMesh = builder.useMeshPrefab.GetComponent<MeshFilter>().sharedMesh;
size = builder.useMeshPrefab.transform.localScale;
rot = builder.useMeshPrefab.transform.rotation;
}
for (int i = bound.xMin; i < bound.xMax; i++)
{
for (int j = bound.yMin; j < bound.yMax; j++)
{
vec3int.x = i;
vec3int.y = j;
if (!tilemap.HasTile(vec3int))
{
continue;
}
if (!builder.overrideByGrid && tilemap.GetColliderType(vec3int) == Tile.ColliderType.Sprite)
{
var sprite = tilemap.GetSprite(vec3int);
if (sprite != null)
{
mesh = builder.GetMesh(sprite);
src.transform = Matrix4x4.TRS(Vector3.Scale(tilemap.GetCellCenterWorld(vec3int), builder.overrideVector) - tilemap.layoutGrid.cellGap, tilemap.transform.rotation, tilemap.transform.lossyScale) * tilemap.orientationMatrix * tilemap.GetTransformMatrix(vec3int);
src.sourceObject = mesh;
sources.Add(src);
}
}
else if (builder.useMeshPrefab != null || (builder.overrideByGrid && builder.useMeshPrefab != null))
{
src.transform = Matrix4x4.TRS(Vector3.Scale(tilemap.GetCellCenterWorld(vec3int), builder.overrideVector), rot, size);
src.sourceObject = sharedMesh;
sources.Add(src);
}
else //default to box
{
var boxsrc = new NavMeshBuildSource();
boxsrc.transform = Matrix4x4.TRS(Vector3.Scale(tilemap.GetCellCenterWorld(vec3int), builder.overrideVector) - tilemap.layoutGrid.cellGap, tilemap.transform.rotation, tilemap.transform.lossyScale) * tilemap.orientationMatrix * tilemap.GetTransformMatrix(vec3int);
boxsrc.shape = NavMeshBuildSourceShape.Box;
boxsrc.size = size;
boxsrc.area = area;
sources.Add(boxsrc);
}
}
}
}
internal static void sprite2mesh(Sprite sprite, Mesh mesh)
{
Vector3[] vert = new Vector3[sprite.vertices.Length];
for (int i = 0; i < sprite.vertices.Length; i++)
{
vert[i] = new Vector3(sprite.vertices[i].x, sprite.vertices[i].y, 0);
}
mesh.vertices = vert;
mesh.uv = sprite.uv;
int[] tri = new int[sprite.triangles.Length];
for (int i = 0; i < sprite.triangles.Length; i++)
{
tri[i] = sprite.triangles[i];
}
mesh.triangles = tri;
}
static private NavMeshBuildSource BoxBoundSource(Bounds localBounds)
{
var src = new NavMeshBuildSource();
src.transform = Matrix4x4.Translate(localBounds.center);
src.shape = NavMeshBuildSourceShape.Box;
src.size = localBounds.size;
src.area = 0;
return src;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 72f0dd11bca0bfd459699d21003f89ba
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,12 @@
{
"name": "NavMeshComponents",
"references": [],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": []
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 8c4dd21966739024fbd72155091d199e
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,172 @@
using System.Collections.Generic;
namespace UnityEngine.AI
{
[ExecuteInEditMode]
[DefaultExecutionOrder(-101)]
[AddComponentMenu("Navigation/NavMeshLink", 33)]
[HelpURL("https://github.com/Unity-Technologies/NavMeshComponents#documentation-draft")]
public class NavMeshLink : MonoBehaviour
{
[SerializeField]
int m_AgentTypeID;
public int agentTypeID { get { return m_AgentTypeID; } set { m_AgentTypeID = value; UpdateLink(); } }
[SerializeField]
Vector3 m_StartPoint = new Vector3(0.0f, 0.0f, -2.5f);
public Vector3 startPoint { get { return m_StartPoint; } set { m_StartPoint = value; UpdateLink(); } }
[SerializeField]
Vector3 m_EndPoint = new Vector3(0.0f, 0.0f, 2.5f);
public Vector3 endPoint { get { return m_EndPoint; } set { m_EndPoint = value; UpdateLink(); } }
[SerializeField]
float m_Width;
public float width { get { return m_Width; } set { m_Width = value; UpdateLink(); } }
[SerializeField]
int m_CostModifier = -1;
public int costModifier { get { return m_CostModifier; } set { m_CostModifier = value; UpdateLink(); } }
[SerializeField]
bool m_Bidirectional = true;
public bool bidirectional { get { return m_Bidirectional; } set { m_Bidirectional = value; UpdateLink(); } }
[SerializeField]
bool m_AutoUpdatePosition;
public bool autoUpdate { get { return m_AutoUpdatePosition; } set { SetAutoUpdate(value); } }
[SerializeField]
int m_Area;
public int area { get { return m_Area; } set { m_Area = value; UpdateLink(); } }
NavMeshLinkInstance m_LinkInstance = new NavMeshLinkInstance();
Vector3 m_LastPosition = Vector3.zero;
Quaternion m_LastRotation = Quaternion.identity;
static readonly List<NavMeshLink> s_Tracked = new List<NavMeshLink>();
void OnEnable()
{
AddLink();
if (m_AutoUpdatePosition && m_LinkInstance.valid)
AddTracking(this);
}
void OnDisable()
{
RemoveTracking(this);
m_LinkInstance.Remove();
}
public void UpdateLink()
{
m_LinkInstance.Remove();
AddLink();
}
static void AddTracking(NavMeshLink link)
{
#if UNITY_EDITOR
if (s_Tracked.Contains(link))
{
Debug.LogError("Link is already tracked: " + link);
return;
}
#endif
if (s_Tracked.Count == 0)
NavMesh.onPreUpdate += UpdateTrackedInstances;
s_Tracked.Add(link);
}
static void RemoveTracking(NavMeshLink link)
{
s_Tracked.Remove(link);
if (s_Tracked.Count == 0)
NavMesh.onPreUpdate -= UpdateTrackedInstances;
}
void SetAutoUpdate(bool value)
{
if (m_AutoUpdatePosition == value)
return;
m_AutoUpdatePosition = value;
if (value)
AddTracking(this);
else
RemoveTracking(this);
}
void AddLink()
{
#if UNITY_EDITOR
if (m_LinkInstance.valid)
{
Debug.LogError("Link is already added: " + this);
return;
}
#endif
var link = new NavMeshLinkData();
link.startPosition = m_StartPoint;
link.endPosition = m_EndPoint;
link.width = m_Width;
link.costModifier = m_CostModifier;
link.bidirectional = m_Bidirectional;
link.area = m_Area;
link.agentTypeID = m_AgentTypeID;
m_LinkInstance = NavMesh.AddLink(link, transform.position, transform.rotation);
if (m_LinkInstance.valid)
m_LinkInstance.owner = this;
m_LastPosition = transform.position;
m_LastRotation = transform.rotation;
}
bool HasTransformChanged()
{
if (m_LastPosition != transform.position) return true;
if (m_LastRotation != transform.rotation) return true;
return false;
}
void OnDidApplyAnimationProperties()
{
UpdateLink();
}
static void UpdateTrackedInstances()
{
foreach (var instance in s_Tracked)
{
if (instance.HasTransformChanged())
instance.UpdateLink();
}
}
#if UNITY_EDITOR
void OnValidate()
{
m_Width = Mathf.Max(0.0f, m_Width);
if (!m_LinkInstance.valid)
return;
UpdateLink();
if (!m_AutoUpdatePosition)
{
RemoveTracking(this);
}
else if (!s_Tracked.Contains(this))
{
AddTracking(this);
}
}
#endif
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 6eeb5dc026fdf4b488bc7ae0138ab719
timeCreated: 1477924439
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 92f4afa3e25264f5b964937ccea49ff2, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,54 @@
using System.Collections.Generic;
namespace UnityEngine.AI
{
[ExecuteInEditMode]
[AddComponentMenu("Navigation/NavMeshModifier", 32)]
[HelpURL("https://github.com/Unity-Technologies/NavMeshComponents#documentation-draft")]
public class NavMeshModifier : MonoBehaviour
{
[SerializeField]
bool m_OverrideArea;
public bool overrideArea { get { return m_OverrideArea; } set { m_OverrideArea = value; } }
[SerializeField]
int m_Area;
public int area { get { return m_Area; } set { m_Area = value; } }
[SerializeField]
bool m_IgnoreFromBuild;
public bool ignoreFromBuild { get { return m_IgnoreFromBuild; } set { m_IgnoreFromBuild = value; } }
// List of agent types the modifier is applied for.
// Special values: empty == None, m_AffectedAgents[0] =-1 == All.
[SerializeField]
List<int> m_AffectedAgents = new List<int>(new int[] { -1 }); // Default value is All
static readonly List<NavMeshModifier> s_NavMeshModifiers = new List<NavMeshModifier>();
public static List<NavMeshModifier> activeModifiers
{
get { return s_NavMeshModifiers; }
}
void OnEnable()
{
if (!s_NavMeshModifiers.Contains(this))
s_NavMeshModifiers.Add(this);
}
void OnDisable()
{
s_NavMeshModifiers.Remove(this);
}
public bool AffectsAgentType(int agentTypeID)
{
if (m_AffectedAgents.Count == 0)
return false;
if (m_AffectedAgents[0] == -1)
return true;
return m_AffectedAgents.IndexOf(agentTypeID) != -1;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 1e3fdca004f2d45fe8abbed571a8abd5
timeCreated: 1477924411
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: cc7b9475dbddf4f9088d327d6e10ab77, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,54 @@
using System.Collections.Generic;
namespace UnityEngine.AI
{
[ExecuteInEditMode]
[AddComponentMenu("Navigation/NavMeshModifierVolume", 31)]
[HelpURL("https://github.com/Unity-Technologies/NavMeshComponents#documentation-draft")]
public class NavMeshModifierVolume : MonoBehaviour
{
[SerializeField]
Vector3 m_Size = new Vector3(4.0f, 3.0f, 4.0f);
public Vector3 size { get { return m_Size; } set { m_Size = value; } }
[SerializeField]
Vector3 m_Center = new Vector3(0, 1.0f, 0);
public Vector3 center { get { return m_Center; } set { m_Center = value; } }
[SerializeField]
int m_Area;
public int area { get { return m_Area; } set { m_Area = value; } }
// List of agent types the modifier is applied for.
// Special values: empty == None, m_AffectedAgents[0] =-1 == All.
[SerializeField]
List<int> m_AffectedAgents = new List<int>(new int[] { -1 }); // Default value is All
static readonly List<NavMeshModifierVolume> s_NavMeshModifiers = new List<NavMeshModifierVolume>();
public static List<NavMeshModifierVolume> activeModifiers
{
get { return s_NavMeshModifiers; }
}
void OnEnable()
{
if (!s_NavMeshModifiers.Contains(this))
s_NavMeshModifiers.Add(this);
}
void OnDisable()
{
s_NavMeshModifiers.Remove(this);
}
public bool AffectsAgentType(int agentTypeID)
{
if (m_AffectedAgents.Count == 0)
return false;
if (m_AffectedAgents[0] == -1)
return true;
return m_AffectedAgents.IndexOf(agentTypeID) != -1;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 35e95dc5ff2b64380880dd7ac5922847
timeCreated: 1477924430
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: cc7b9475dbddf4f9088d327d6e10ab77, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,486 @@
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
#endif
namespace UnityEngine.AI
{
public enum CollectObjects
{
All = 0,
Volume = 1,
Children = 2,
}
[ExecuteAlways]
[DefaultExecutionOrder(-102)]
[AddComponentMenu("Navigation/NavMeshSurface", 30)]
[HelpURL("https://github.com/Unity-Technologies/NavMeshComponents#documentation-draft")]
public class NavMeshSurface : MonoBehaviour
{
[SerializeField]
int m_AgentTypeID;
public int agentTypeID { get { return m_AgentTypeID; } set { m_AgentTypeID = value; } }
[SerializeField]
CollectObjects m_CollectObjects = CollectObjects.All;
public CollectObjects collectObjects { get { return m_CollectObjects; } set { m_CollectObjects = value; } }
[SerializeField]
Vector3 m_Size = new Vector3(10.0f, 10.0f, 10.0f);
public Vector3 size { get { return m_Size; } set { m_Size = value; } }
[SerializeField]
Vector3 m_Center = new Vector3(0, 2.0f, 0);
public Vector3 center { get { return m_Center; } set { m_Center = value; } }
[SerializeField]
LayerMask m_LayerMask = ~0;
public LayerMask layerMask { get { return m_LayerMask; } set { m_LayerMask = value; } }
[SerializeField]
NavMeshCollectGeometry m_UseGeometry = NavMeshCollectGeometry.RenderMeshes;
public NavMeshCollectGeometry useGeometry { get { return m_UseGeometry; } set { m_UseGeometry = value; } }
[SerializeField]
int m_DefaultArea;
public int defaultArea { get { return m_DefaultArea; } set { m_DefaultArea = value; } }
[SerializeField]
bool m_IgnoreNavMeshAgent = true;
public bool ignoreNavMeshAgent { get { return m_IgnoreNavMeshAgent; } set { m_IgnoreNavMeshAgent = value; } }
[SerializeField]
bool m_IgnoreNavMeshObstacle = true;
public bool ignoreNavMeshObstacle { get { return m_IgnoreNavMeshObstacle; } set { m_IgnoreNavMeshObstacle = value; } }
[SerializeField]
bool m_OverrideTileSize;
public bool overrideTileSize { get { return m_OverrideTileSize; } set { m_OverrideTileSize = value; } }
[SerializeField]
int m_TileSize = 256;
public int tileSize { get { return m_TileSize; } set { m_TileSize = value; } }
[SerializeField]
bool m_OverrideVoxelSize;
public bool overrideVoxelSize { get { return m_OverrideVoxelSize; } set { m_OverrideVoxelSize = value; } }
[SerializeField]
float m_VoxelSize;
public float voxelSize { get { return m_VoxelSize; } set { m_VoxelSize = value; } }
// Currently not supported advanced options
[SerializeField]
bool m_BuildHeightMesh;
public bool buildHeightMesh { get { return m_BuildHeightMesh; } set { m_BuildHeightMesh = value; } }
// Reference to whole scene navmesh data asset.
[UnityEngine.Serialization.FormerlySerializedAs("m_BakedNavMeshData")]
[SerializeField]
NavMeshData m_NavMeshData;
public NavMeshData navMeshData { get { return m_NavMeshData; } set { m_NavMeshData = value; } }
// Do not serialize - runtime only state.
NavMeshDataInstance m_NavMeshDataInstance;
Vector3 m_LastPosition = Vector3.zero;
Quaternion m_LastRotation = Quaternion.identity;
static readonly List<NavMeshSurface> s_NavMeshSurfaces = new List<NavMeshSurface>();
public static List<NavMeshSurface> activeSurfaces
{
get { return s_NavMeshSurfaces; }
}
void OnEnable()
{
Register(this);
AddData();
}
void OnDisable()
{
RemoveData();
Unregister(this);
}
public void AddData()
{
#if UNITY_EDITOR
var isInPreviewScene = EditorSceneManager.IsPreviewSceneObject(this);
var isPrefab = isInPreviewScene || EditorUtility.IsPersistent(this);
if (isPrefab)
{
//Debug.LogFormat("NavMeshData from {0}.{1} will not be added to the NavMesh world because the gameObject is a prefab.",
// gameObject.name, name);
return;
}
#endif
if (m_NavMeshDataInstance.valid)
return;
if (m_NavMeshData != null)
{
m_NavMeshDataInstance = NavMesh.AddNavMeshData(m_NavMeshData, transform.position, transform.rotation);
m_NavMeshDataInstance.owner = this;
}
m_LastPosition = transform.position;
m_LastRotation = transform.rotation;
}
public void RemoveData()
{
m_NavMeshDataInstance.Remove();
m_NavMeshDataInstance = new NavMeshDataInstance();
}
public NavMeshBuildSettings GetBuildSettings()
{
var buildSettings = NavMesh.GetSettingsByID(m_AgentTypeID);
if (buildSettings.agentTypeID == -1)
{
Debug.LogWarning("No build settings for agent type ID " + agentTypeID, this);
buildSettings.agentTypeID = m_AgentTypeID;
}
if (overrideTileSize)
{
buildSettings.overrideTileSize = true;
buildSettings.tileSize = tileSize;
}
if (overrideVoxelSize)
{
buildSettings.overrideVoxelSize = true;
buildSettings.voxelSize = voxelSize;
}
return buildSettings;
}
public void BuildNavMesh()
{
var sources = CollectSources();
// Use unscaled bounds - this differs in behaviour from e.g. collider components.
// But is similar to reflection probe - and since navmesh data has no scaling support - it is the right choice here.
var sourcesBounds = new Bounds(m_Center, Abs(m_Size));
if (m_CollectObjects == CollectObjects.All || m_CollectObjects == CollectObjects.Children)
{
sourcesBounds = CalculateWorldBounds(sources);
}
var data = NavMeshBuilder.BuildNavMeshData(GetBuildSettings(),
sources, sourcesBounds, transform.position, transform.rotation);
if (data != null)
{
data.name = gameObject.name;
RemoveData();
m_NavMeshData = data;
if (isActiveAndEnabled)
AddData();
}
}
public AsyncOperation UpdateNavMesh(NavMeshData data)
{
var sources = CollectSources();
// Use unscaled bounds - this differs in behaviour from e.g. collider components.
// But is similar to reflection probe - and since navmesh data has no scaling support - it is the right choice here.
var sourcesBounds = new Bounds(m_Center, Abs(m_Size));
if (m_CollectObjects == CollectObjects.All || m_CollectObjects == CollectObjects.Children)
sourcesBounds = CalculateWorldBounds(sources);
return NavMeshBuilder.UpdateNavMeshDataAsync(data, GetBuildSettings(), sources, sourcesBounds);
}
static void Register(NavMeshSurface surface)
{
#if UNITY_EDITOR
var isInPreviewScene = EditorSceneManager.IsPreviewSceneObject(surface);
var isPrefab = isInPreviewScene || EditorUtility.IsPersistent(surface);
if (isPrefab)
{
//Debug.LogFormat("NavMeshData from {0}.{1} will not be added to the NavMesh world because the gameObject is a prefab.",
// surface.gameObject.name, surface.name);
return;
}
#endif
if (s_NavMeshSurfaces.Count == 0)
NavMesh.onPreUpdate += UpdateActive;
if (!s_NavMeshSurfaces.Contains(surface))
s_NavMeshSurfaces.Add(surface);
}
static void Unregister(NavMeshSurface surface)
{
s_NavMeshSurfaces.Remove(surface);
if (s_NavMeshSurfaces.Count == 0)
NavMesh.onPreUpdate -= UpdateActive;
}
static void UpdateActive()
{
for (var i = 0; i < s_NavMeshSurfaces.Count; ++i)
s_NavMeshSurfaces[i].UpdateDataIfTransformChanged();
}
void AppendModifierVolumes(ref List<NavMeshBuildSource> sources)
{
#if UNITY_EDITOR
var myStage = StageUtility.GetStageHandle(gameObject);
if (!myStage.IsValid())
return;
#endif
// Modifiers
List<NavMeshModifierVolume> modifiers;
if (m_CollectObjects == CollectObjects.Children)
{
modifiers = new List<NavMeshModifierVolume>(GetComponentsInChildren<NavMeshModifierVolume>());
modifiers.RemoveAll(x => !x.isActiveAndEnabled);
}
else
{
modifiers = NavMeshModifierVolume.activeModifiers;
}
foreach (var m in modifiers)
{
if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
continue;
if (!m.AffectsAgentType(m_AgentTypeID))
continue;
#if UNITY_EDITOR
if (!myStage.Contains(m.gameObject))
continue;
#endif
var mcenter = m.transform.TransformPoint(m.center);
var scale = m.transform.lossyScale;
var msize = new Vector3(m.size.x * Mathf.Abs(scale.x), m.size.y * Mathf.Abs(scale.y), m.size.z * Mathf.Abs(scale.z));
var src = new NavMeshBuildSource();
src.shape = NavMeshBuildSourceShape.ModifierBox;
src.transform = Matrix4x4.TRS(mcenter, m.transform.rotation, Vector3.one);
src.size = msize;
src.area = m.area;
sources.Add(src);
}
}
List<NavMeshBuildSource> CollectSources()
{
var sources = new List<NavMeshBuildSource>();
var markups = new List<NavMeshBuildMarkup>();
List<NavMeshModifier> modifiers;
if (m_CollectObjects == CollectObjects.Children)
{
modifiers = new List<NavMeshModifier>(GetComponentsInChildren<NavMeshModifier>());
modifiers.RemoveAll(x => !x.isActiveAndEnabled);
}
else
{
modifiers = NavMeshModifier.activeModifiers;
}
foreach (var m in modifiers)
{
if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
continue;
if (!m.AffectsAgentType(m_AgentTypeID))
continue;
var markup = new NavMeshBuildMarkup();
markup.root = m.transform;
markup.overrideArea = m.overrideArea;
markup.area = m.area;
markup.ignoreFromBuild = m.ignoreFromBuild;
markups.Add(markup);
}
#if UNITY_EDITOR
if (!EditorApplication.isPlaying)
{
if (m_CollectObjects == CollectObjects.All)
{
UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
}
else if (m_CollectObjects == CollectObjects.Children)
{
UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
}
else if (m_CollectObjects == CollectObjects.Volume)
{
Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
var worldBounds = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
}
}
else
#endif
{
if (m_CollectObjects == CollectObjects.All)
{
NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
}
else if (m_CollectObjects == CollectObjects.Children)
{
NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
}
else if (m_CollectObjects == CollectObjects.Volume)
{
Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
var worldBounds = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
}
}
if (m_IgnoreNavMeshAgent)
sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent<NavMeshAgent>() != null));
if (m_IgnoreNavMeshObstacle)
sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent<NavMeshObstacle>() != null));
AppendModifierVolumes(ref sources);
return sources;
}
static Vector3 Abs(Vector3 v)
{
return new Vector3(Mathf.Abs(v.x), Mathf.Abs(v.y), Mathf.Abs(v.z));
}
static Bounds GetWorldBounds(Matrix4x4 mat, Bounds bounds)
{
var absAxisX = Abs(mat.MultiplyVector(Vector3.right));
var absAxisY = Abs(mat.MultiplyVector(Vector3.up));
var absAxisZ = Abs(mat.MultiplyVector(Vector3.forward));
var worldPosition = mat.MultiplyPoint(bounds.center);
var worldSize = absAxisX * bounds.size.x + absAxisY * bounds.size.y + absAxisZ * bounds.size.z;
return new Bounds(worldPosition, worldSize);
}
Bounds CalculateWorldBounds(List<NavMeshBuildSource> sources)
{
// Use the unscaled matrix for the NavMeshSurface
Matrix4x4 worldToLocal = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
worldToLocal = worldToLocal.inverse;
var result = new Bounds();
foreach (var src in sources)
{
switch (src.shape)
{
case NavMeshBuildSourceShape.Mesh:
{
var m = src.sourceObject as Mesh;
result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, m.bounds));
break;
}
case NavMeshBuildSourceShape.Terrain:
{
// Terrain pivot is lower/left corner - shift bounds accordingly
var t = src.sourceObject as TerrainData;
result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, new Bounds(0.5f * t.size, t.size)));
break;
}
case NavMeshBuildSourceShape.Box:
case NavMeshBuildSourceShape.Sphere:
case NavMeshBuildSourceShape.Capsule:
case NavMeshBuildSourceShape.ModifierBox:
result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, new Bounds(Vector3.zero, src.size)));
break;
}
}
// Inflate the bounds a bit to avoid clipping co-planar sources
result.Expand(0.1f);
return result;
}
bool HasTransformChanged()
{
if (m_LastPosition != transform.position) return true;
if (m_LastRotation != transform.rotation) return true;
return false;
}
void UpdateDataIfTransformChanged()
{
if (HasTransformChanged())
{
RemoveData();
AddData();
}
}
#if UNITY_EDITOR
bool UnshareNavMeshAsset()
{
// Nothing to unshare
if (m_NavMeshData == null)
return false;
// Prefab parent owns the asset reference
var isInPreviewScene = EditorSceneManager.IsPreviewSceneObject(this);
var isPersistentObject = EditorUtility.IsPersistent(this);
if (isInPreviewScene || isPersistentObject)
return false;
// An instance can share asset reference only with its prefab parent
var prefab = UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(this) as NavMeshSurface;
if (prefab != null && prefab.navMeshData == navMeshData)
return false;
// Don't allow referencing an asset that's assigned to another surface
for (var i = 0; i < s_NavMeshSurfaces.Count; ++i)
{
var surface = s_NavMeshSurfaces[i];
if (surface != this && surface.m_NavMeshData == m_NavMeshData)
return true;
}
// Asset is not referenced by known surfaces
return false;
}
void OnValidate()
{
if (UnshareNavMeshAsset())
{
Debug.LogWarning("Duplicating NavMeshSurface does not duplicate the referenced navmesh data", this);
m_NavMeshData = null;
}
var settings = NavMesh.GetSettingsByID(m_AgentTypeID);
if (settings.agentTypeID != -1)
{
// When unchecking the override control, revert to automatic value.
const float kMinVoxelSize = 0.01f;
if (!m_OverrideVoxelSize)
m_VoxelSize = settings.agentRadius / 3.0f;
if (m_VoxelSize < kMinVoxelSize)
m_VoxelSize = kMinVoxelSize;
// When unchecking the override control, revert to default value.
const int kMinTileSize = 16;
const int kMaxTileSize = 1024;
const int kDefaultTileSize = 256;
if (!m_OverrideTileSize)
m_TileSize = kDefaultTileSize;
// Make sure tilesize is in sane range.
if (m_TileSize < kMinTileSize)
m_TileSize = kMinTileSize;
if (m_TileSize > kMaxTileSize)
m_TileSize = kMaxTileSize;
}
}
#endif
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 7a5ac11cc976e418e8d13136b07e1f52
timeCreated: 1477658803
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: e4f97225bcfb64760a1c81f460837f01, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,582 @@
using System;
using System.Collections.Generic;
using UnityEngine.Tilemaps;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
#endif
namespace UnityEngine.AI
{
public enum CollectObjects2d
{
All = 0,
Volume = 1,
Children = 2,
}
[ExecuteAlways]
[DefaultExecutionOrder(-102)]
[AddComponentMenu("Navigation/NavMeshSurface2d", 30)]
[HelpURL("https://github.com/Unity-Technologies/NavMeshComponents#documentation-draft")]
public class NavMeshSurface2d : MonoBehaviour
{
[SerializeField]
int m_AgentTypeID;
public int agentTypeID { get { return m_AgentTypeID; } set { m_AgentTypeID = value; } }
[SerializeField]
CollectObjects2d m_CollectObjects = CollectObjects2d.All;
public CollectObjects2d collectObjects { get { return m_CollectObjects; } set { m_CollectObjects = value; } }
[SerializeField]
Vector3 m_Size = new Vector3(10.0f, 10.0f, 10.0f);
public Vector3 size { get { return m_Size; } set { m_Size = value; } }
[SerializeField]
Vector3 m_Center = new Vector3(0, 2.0f, 0);
public Vector3 center { get { return m_Center; } set { m_Center = value; } }
[SerializeField]
LayerMask m_LayerMask = ~0;
public LayerMask layerMask { get { return m_LayerMask; } set { m_LayerMask = value; } }
[SerializeField]
NavMeshCollectGeometry m_UseGeometry = NavMeshCollectGeometry.RenderMeshes;
public NavMeshCollectGeometry useGeometry { get { return m_UseGeometry; } set { m_UseGeometry = value; } }
[SerializeField]
bool m_OverrideByGrid;
public bool overrideByGrid { get { return m_OverrideByGrid; } set { m_OverrideByGrid = value; } }
[SerializeField]
GameObject m_UseMeshPrefab;
public GameObject useMeshPrefab { get { return m_UseMeshPrefab; } set { m_UseMeshPrefab = value; } }
[SerializeField]
bool m_CompressBounds;
public bool compressBounds { get { return m_CompressBounds; } set { m_CompressBounds = value; } }
[SerializeField]
Vector3 m_OverrideVector = Vector3.one;
public Vector3 overrideVector { get { return m_OverrideVector; } set { m_OverrideVector = value; } }
[SerializeField]
int m_DefaultArea;
public int defaultArea { get { return m_DefaultArea; } set { m_DefaultArea = value; } }
[SerializeField]
bool m_IgnoreNavMeshAgent = true;
public bool ignoreNavMeshAgent { get { return m_IgnoreNavMeshAgent; } set { m_IgnoreNavMeshAgent = value; } }
[SerializeField]
bool m_IgnoreNavMeshObstacle = true;
public bool ignoreNavMeshObstacle { get { return m_IgnoreNavMeshObstacle; } set { m_IgnoreNavMeshObstacle = value; } }
[SerializeField]
bool m_OverrideTileSize;
public bool overrideTileSize { get { return m_OverrideTileSize; } set { m_OverrideTileSize = value; } }
[SerializeField]
int m_TileSize = 256;
public int tileSize { get { return m_TileSize; } set { m_TileSize = value; } }
[SerializeField]
bool m_OverrideVoxelSize;
public bool overrideVoxelSize { get { return m_OverrideVoxelSize; } set { m_OverrideVoxelSize = value; } }
[SerializeField]
float m_VoxelSize;
public float voxelSize { get { return m_VoxelSize; } set { m_VoxelSize = value; } }
// Currently not supported advanced options
[SerializeField]
bool m_BuildHeightMesh;
public bool buildHeightMesh { get { return m_BuildHeightMesh; } set { m_BuildHeightMesh = value; } }
[SerializeField]
bool m_HideEditorLogs;
public bool hideEditorLogs { get { return m_HideEditorLogs; } set { m_HideEditorLogs = value; } }
// Reference to whole scene navmesh data asset.
[UnityEngine.Serialization.FormerlySerializedAs("m_BakedNavMeshData")]
[SerializeField]
NavMeshData m_NavMeshData;
public NavMeshData navMeshData { get { return m_NavMeshData; } set { m_NavMeshData = value; } }
// Do not serialize - runtime only state.
NavMeshDataInstance m_NavMeshDataInstance;
Vector3 m_LastPosition = Vector3.zero;
Quaternion m_LastRotation = Quaternion.identity;
static readonly List<NavMeshSurface2d> s_NavMeshSurfaces = new List<NavMeshSurface2d>();
public static List<NavMeshSurface2d> activeSurfaces
{
get { return s_NavMeshSurfaces; }
}
void OnEnable()
{
Register(this);
AddData();
}
void OnDisable()
{
RemoveData();
Unregister(this);
}
public void AddData()
{
#if UNITY_EDITOR
var isInPreviewScene = EditorSceneManager.IsPreviewSceneObject(this);
var isPrefab = isInPreviewScene || EditorUtility.IsPersistent(this);
if (isPrefab)
{
//Debug.LogFormat("NavMeshData from {0}.{1} will not be added to the NavMesh world because the gameObject is a prefab.",
// gameObject.name, name);
return;
}
#endif
if (m_NavMeshDataInstance.valid)
return;
if (m_NavMeshData != null)
{
m_NavMeshDataInstance = NavMesh.AddNavMeshData(m_NavMeshData, transform.position, transform.rotation);
m_NavMeshDataInstance.owner = this;
}
m_LastPosition = transform.position;
m_LastRotation = transform.rotation;
}
public void RemoveData()
{
m_NavMeshDataInstance.Remove();
m_NavMeshDataInstance = new NavMeshDataInstance();
}
public NavMeshBuildSettings GetBuildSettings()
{
var buildSettings = NavMesh.GetSettingsByID(m_AgentTypeID);
if (buildSettings.agentTypeID == -1)
{
if (!m_HideEditorLogs) Debug.LogWarning("No build settings for agent type ID " + agentTypeID, this);
buildSettings.agentTypeID = m_AgentTypeID;
}
if (overrideTileSize)
{
buildSettings.overrideTileSize = true;
buildSettings.tileSize = tileSize;
}
if (overrideVoxelSize)
{
buildSettings.overrideVoxelSize = true;
buildSettings.voxelSize = voxelSize;
}
return buildSettings;
}
public void BuildNavMesh()
{
var sources = CollectSources();
// Use unscaled bounds - this differs in behaviour from e.g. collider components.
// But is similar to reflection probe - and since navmesh data has no scaling support - it is the right choice here.
var sourcesBounds = new Bounds(m_Center, Abs(m_Size));
if (m_CollectObjects != CollectObjects2d.Volume)
{
sourcesBounds = CalculateWorldBounds(sources);
}
var data = NavMeshBuilder.BuildNavMeshData(GetBuildSettings(),
sources, sourcesBounds, transform.position, transform.rotation);
if (data != null)
{
data.name = gameObject.name;
RemoveData();
m_NavMeshData = data;
if (isActiveAndEnabled)
AddData();
}
}
// Source: https://github.com/Unity-Technologies/NavMeshComponents/issues/97#issuecomment-528692289
public AsyncOperation BuildNavMeshAsync()
{
RemoveData();
m_NavMeshData = new NavMeshData(m_AgentTypeID)
{
name = gameObject.name,
position = transform.position,
rotation = transform.rotation
};
if (isActiveAndEnabled)
{
AddData();
}
return UpdateNavMesh(m_NavMeshData);
}
public AsyncOperation UpdateNavMesh(NavMeshData data)
{
var sources = CollectSources();
// Use unscaled bounds - this differs in behaviour from e.g. collider components.
// But is similar to reflection probe - and since navmesh data has no scaling support - it is the right choice here.
var sourcesBounds = new Bounds(m_Center, Abs(m_Size));
if (m_CollectObjects != CollectObjects2d.Volume)
sourcesBounds = CalculateWorldBounds(sources);
return NavMeshBuilder.UpdateNavMeshDataAsync(data, GetBuildSettings(), sources, sourcesBounds);
}
static void Register(NavMeshSurface2d surface)
{
#if UNITY_EDITOR
var isInPreviewScene = EditorSceneManager.IsPreviewSceneObject(surface);
var isPrefab = isInPreviewScene || EditorUtility.IsPersistent(surface);
if (isPrefab)
{
//Debug.LogFormat("NavMeshData from {0}.{1} will not be added to the NavMesh world because the gameObject is a prefab.",
// surface.gameObject.name, surface.name);
return;
}
#endif
if (s_NavMeshSurfaces.Count == 0)
NavMesh.onPreUpdate += UpdateActive;
if (!s_NavMeshSurfaces.Contains(surface))
s_NavMeshSurfaces.Add(surface);
}
static void Unregister(NavMeshSurface2d surface)
{
s_NavMeshSurfaces.Remove(surface);
if (s_NavMeshSurfaces.Count == 0)
NavMesh.onPreUpdate -= UpdateActive;
}
static void UpdateActive()
{
for (var i = 0; i < s_NavMeshSurfaces.Count; ++i)
s_NavMeshSurfaces[i].UpdateDataIfTransformChanged();
}
void AppendModifierVolumes(ref List<NavMeshBuildSource> sources)
{
#if UNITY_EDITOR
var myStage = StageUtility.GetStageHandle(gameObject);
if (!myStage.IsValid())
return;
#endif
// Modifiers
List<NavMeshModifierVolume> modifiers;
if (m_CollectObjects == CollectObjects2d.Children)
{
modifiers = new List<NavMeshModifierVolume>(GetComponentsInChildren<NavMeshModifierVolume>());
modifiers.RemoveAll(x => !x.isActiveAndEnabled);
}
else
{
modifiers = NavMeshModifierVolume.activeModifiers;
}
foreach (var m in modifiers)
{
if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
continue;
if (!m.AffectsAgentType(m_AgentTypeID))
continue;
#if UNITY_EDITOR
if (!myStage.Contains(m.gameObject))
continue;
#endif
var mcenter = m.transform.TransformPoint(m.center);
var scale = m.transform.lossyScale;
var msize = new Vector3(m.size.x * Mathf.Abs(scale.x), m.size.y * Mathf.Abs(scale.y), m.size.z * Mathf.Abs(scale.z));
var src = new NavMeshBuildSource();
src.shape = NavMeshBuildSourceShape.ModifierBox;
src.transform = Matrix4x4.TRS(mcenter, m.transform.rotation, Vector3.one);
src.size = msize;
src.area = m.area;
sources.Add(src);
}
}
List<NavMeshBuildSource> CollectSources()
{
var sources = new List<NavMeshBuildSource>();
var markups = new List<NavMeshBuildMarkup>();
List<NavMeshModifier> modifiers;
if (m_CollectObjects == CollectObjects2d.Children)
{
modifiers = new List<NavMeshModifier>(GetComponentsInChildren<NavMeshModifier>());
modifiers.RemoveAll(x => !x.isActiveAndEnabled);
}
else
{
modifiers = NavMeshModifier.activeModifiers;
}
foreach (var m in modifiers)
{
if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
continue;
if (!m.AffectsAgentType(m_AgentTypeID))
continue;
var markup = new NavMeshBuildMarkup();
markup.root = m.transform;
markup.overrideArea = m.overrideArea;
markup.area = m.area;
markup.ignoreFromBuild = m.ignoreFromBuild;
markups.Add(markup);
}
#if UNITY_EDITOR
if (!EditorApplication.isPlaying)
{
if (m_CollectObjects == CollectObjects2d.All)
{
UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
}
else if (m_CollectObjects == CollectObjects2d.Children)
{
UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
}
else if (m_CollectObjects == CollectObjects2d.Volume)
{
Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
var worldBounds = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
}
if (!hideEditorLogs && !Mathf.Approximately(transform.eulerAngles.x, 270f))
Debug.LogWarning("NavMeshSurface2d is not rotated respectively to (x-90;y0;z0). Apply rotation unless intended.");
var builder = new NavMeshBuilder2dWrapper();
builder.defaultArea = defaultArea;
builder.layerMask = layerMask;
builder.agentID = agentTypeID;
builder.useMeshPrefab = useMeshPrefab;
builder.overrideByGrid = overrideByGrid;
builder.compressBounds = compressBounds;
builder.overrideVector = overrideVector;
builder.CollectGeometry = useGeometry;
builder.CollectObjects = collectObjects;
builder.parent = gameObject;
builder.hideEditorLogs = hideEditorLogs;
NavMeshBuilder2d.CollectSources(sources, builder);
}
else
#endif
{
if (m_CollectObjects == CollectObjects2d.All)
{
NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
}
else if (m_CollectObjects == CollectObjects2d.Children)
{
NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
}
else if (m_CollectObjects == CollectObjects2d.Volume)
{
Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
var worldBounds = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
}
if (!hideEditorLogs && !Mathf.Approximately(transform.eulerAngles.x, 270f))
Debug.LogWarning("NavMeshSurface2d is not rotated respectively to (x-90;y0;z0). Apply rotation unless intended.");
var builder = new NavMeshBuilder2dWrapper();
builder.defaultArea = defaultArea;
builder.layerMask = layerMask;
builder.agentID = agentTypeID;
builder.useMeshPrefab = useMeshPrefab;
builder.overrideByGrid = overrideByGrid;
builder.compressBounds = compressBounds;
builder.overrideVector = overrideVector;
builder.CollectGeometry = useGeometry;
builder.CollectObjects = collectObjects;
builder.parent = gameObject;
builder.hideEditorLogs = hideEditorLogs;
NavMeshBuilder2d.CollectSources(sources, builder);
}
if (m_IgnoreNavMeshAgent)
sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent<NavMeshAgent>() != null));
if (m_IgnoreNavMeshObstacle)
sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent<NavMeshObstacle>() != null));
AppendModifierVolumes(ref sources);
return sources;
}
static Vector3 Abs(Vector3 v)
{
return new Vector3(Mathf.Abs(v.x), Mathf.Abs(v.y), Mathf.Abs(v.z));
}
public static Bounds GetWorldBounds(Matrix4x4 mat, Bounds bounds)
{
var absAxisX = Abs(mat.MultiplyVector(Vector3.right));
var absAxisY = Abs(mat.MultiplyVector(Vector3.up));
var absAxisZ = Abs(mat.MultiplyVector(Vector3.forward));
var worldPosition = mat.MultiplyPoint(bounds.center);
var worldSize = absAxisX * bounds.size.x + absAxisY * bounds.size.y + absAxisZ * bounds.size.z;
return new Bounds(worldPosition, worldSize);
}
Bounds CalculateWorldBounds(List<NavMeshBuildSource> sources)
{
// Use the unscaled matrix for the NavMeshSurface
Matrix4x4 worldToLocal = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
worldToLocal = worldToLocal.inverse;
var result = new Bounds();
if (collectObjects != CollectObjects2d.Children)
{
result.Encapsulate(CalculateGridWorldBounds(worldToLocal));
}
foreach (var src in sources)
{
switch (src.shape)
{
case NavMeshBuildSourceShape.Mesh:
{
var m = src.sourceObject as Mesh;
result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, m.bounds));
break;
}
case NavMeshBuildSourceShape.Terrain:
{
// Terrain pivot is lower/left corner - shift bounds accordingly
var t = src.sourceObject as TerrainData;
result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, new Bounds(0.5f * t.size, t.size)));
break;
}
case NavMeshBuildSourceShape.Box:
case NavMeshBuildSourceShape.Sphere:
case NavMeshBuildSourceShape.Capsule:
case NavMeshBuildSourceShape.ModifierBox:
result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, new Bounds(Vector3.zero, src.size)));
break;
}
}
// Inflate the bounds a bit to avoid clipping co-planar sources
result.Expand(0.1f);
return result;
}
private static Bounds CalculateGridWorldBounds(Matrix4x4 worldToLocal)
{
var bounds = new Bounds();
var grid = FindObjectOfType<Grid>();
var tilemaps = grid.GetComponentsInChildren<Tilemap>();
if (tilemaps == null || tilemaps.Length < 1)
{
throw new NullReferenceException("Add at least one tilemap");
}
foreach (var tilemap in tilemaps)
{
//Debug.Log($"From Local Bounds [{tilemap.name}]: {tilemap.localBounds}");
var lbounds = GetWorldBounds(worldToLocal * tilemap.transform.localToWorldMatrix, tilemap.localBounds);
bounds.Encapsulate(lbounds);
//Debug.Log($"To World Bounds: {bounds}");
}
bounds.Expand(0.1f);
return bounds;
}
bool HasTransformChanged()
{
if (m_LastPosition != transform.position) return true;
if (m_LastRotation != transform.rotation) return true;
return false;
}
void UpdateDataIfTransformChanged()
{
if (HasTransformChanged())
{
RemoveData();
AddData();
}
}
#if UNITY_EDITOR
bool UnshareNavMeshAsset()
{
// Nothing to unshare
if (m_NavMeshData == null)
return false;
// Prefab parent owns the asset reference
var isInPreviewScene = EditorSceneManager.IsPreviewSceneObject(this);
var isPersistentObject = EditorUtility.IsPersistent(this);
if (isInPreviewScene || isPersistentObject)
return false;
// An instance can share asset reference only with its prefab parent
var prefab = UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(this) as NavMeshSurface2d;
if (prefab != null && prefab.navMeshData == navMeshData)
return false;
// Don't allow referencing an asset that's assigned to another surface
for (var i = 0; i < s_NavMeshSurfaces.Count; ++i)
{
var surface = s_NavMeshSurfaces[i];
if (surface != this && surface.m_NavMeshData == m_NavMeshData)
return true;
}
// Asset is not referenced by known surfaces
return false;
}
void OnValidate()
{
if (UnshareNavMeshAsset())
{
if (!m_HideEditorLogs) Debug.LogWarning("Duplicating NavMeshSurface does not duplicate the referenced navmesh data", this);
m_NavMeshData = null;
}
var settings = NavMesh.GetSettingsByID(m_AgentTypeID);
if (settings.agentTypeID != -1)
{
// When unchecking the override control, revert to automatic value.
const float kMinVoxelSize = 0.01f;
if (!m_OverrideVoxelSize)
m_VoxelSize = settings.agentRadius / 3.0f;
if (m_VoxelSize < kMinVoxelSize)
m_VoxelSize = kMinVoxelSize;
// When unchecking the override control, revert to default value.
const int kMinTileSize = 16;
const int kMaxTileSize = 1024;
const int kDefaultTileSize = 256;
if (!m_OverrideTileSize)
m_TileSize = kDefaultTileSize;
// Make sure tilesize is in sane range.
if (m_TileSize < kMinTileSize)
m_TileSize = kMinTileSize;
if (m_TileSize > kMaxTileSize)
m_TileSize = kMaxTileSize;
}
}
#endif
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 148387606d0ccd84fa326ca211f274ba
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 871b885cd68224f4787ae765e0da9571, type: 3}
userData:
assetBundleName:
assetBundleVariant: