123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
-
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- public class NavMeshSurface: MonoBehaviour
- {
- #if UNITY_5_6_OR_NEWER
-
-
-
- [SerializeField]
- public ZEDManager zedManagerSpatialMapping = null;
-
-
-
-
- [SerializeField]
- public int agentTypeID = 0;
-
-
-
-
- private List<NavMeshBuildSource> sources = new List<NavMeshBuildSource>();
-
-
-
- private NavMeshData navMesh;
-
-
-
- private Bounds bounds;
-
-
-
-
- private Material materialTransparent;
- #endif
-
-
-
-
- public class PositionEventArgs : System.EventArgs
- {
-
-
-
- public Vector3 position;
-
-
-
- public bool valid = false;
-
-
-
- public int agentTypeID = 0;
- }
-
-
-
- public static event System.EventHandler<PositionEventArgs> OnNavMeshReady;
-
-
-
- [HideInInspector]
- [SerializeField]
- public bool hideFlag = true;
-
-
-
- [HideInInspector]
- [SerializeField]
- public bool isOver = false;
-
-
-
- private GameObject navMeshObject;
-
-
-
- private Vector3 navMeshPosition;
-
-
-
- public Vector3 NavMeshPosition { get { return navMeshObject != null ? navMeshPosition : Vector3.zero; } }
-
- void Start()
- {
- if(!zedManagerSpatialMapping)
- {
- zedManagerSpatialMapping = FindObjectOfType<ZEDManager>();
- }
- #if UNITY_5_6_OR_NEWER
- navMesh = new NavMeshData();
- NavMesh.AddNavMeshData(navMesh);
-
- bounds = new Bounds(transform.position, new Vector3(30, 30, 30));
- materialTransparent = Resources.Load("Materials/Mat_ZED_Transparent_NavMesh") as Material;
- #endif
- if (zedManagerSpatialMapping) {
- zedManagerSpatialMapping.GetSpatialMapping.OnMeshReady += MeshIsOver;
- zedManagerSpatialMapping.GetSpatialMapping.OnMeshStarted += NewNavMesh;
- }
- }
-
-
-
-
- void NewNavMesh()
- {
- if(navMeshObject != null)
- {
- Destroy(navMeshObject);
- }
- }
- void OnDisable()
- {
-
- if (zedManagerSpatialMapping) {
- zedManagerSpatialMapping.GetSpatialMapping.OnMeshReady -= MeshIsOver;
- zedManagerSpatialMapping.GetSpatialMapping.OnMeshStarted -= NewNavMesh;
- }
- }
-
-
-
-
- void MeshIsOver()
- {
- #if UNITY_5_6_OR_NEWER
- UpdateNavMesh();
-
- sources.Clear();
-
- bool isDrawn = Draw();
- #endif
- if (isDrawn)
- {
- PositionEventArgs args = new PositionEventArgs();
- args.position = NavMeshPosition;
- args.valid = isDrawn;
- args.agentTypeID = agentTypeID;
- System.EventHandler<PositionEventArgs> handler = OnNavMeshReady;
- if (handler != null)
- {
- handler(this, args);
- }
- }else
- {
- Debug.LogWarning(ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.NAVMESH_NOT_GENERATED));
- }
- isOver = true;
-
- }
-
-
-
-
- bool Draw()
- {
- NavMeshTriangulation triangulation = NavMesh.CalculateTriangulation();
- if (triangulation.areas.Length == 0)
- return false;
-
-
- if (navMeshObject == null)
- {
- navMeshObject = new GameObject("NavMesh");
- }
- navMeshObject.transform.parent = transform;
-
-
- MeshFilter meshFilter = navMeshObject.GetComponent<MeshFilter>();
- if (!meshFilter)
- {
- meshFilter = navMeshObject.AddComponent<MeshFilter>();
- }
- meshFilter.mesh.Clear();
- meshFilter.mesh.vertices = triangulation.vertices;
- meshFilter.mesh.triangles = triangulation.indices;
- meshFilter.mesh.RecalculateNormals();
- MeshRenderer r = navMeshObject.GetComponent<MeshRenderer>();
- if (!r)
- {
- r = navMeshObject.AddComponent<MeshRenderer>();
- }
- r.sharedMaterial = materialTransparent;
- navMeshPosition = r.bounds.center;
- navMeshObject.SetActive(false);
- return true;
- }
-
-
-
-
- void CollectSources()
- {
- #if UNITY_5_6_OR_NEWER
- sources.Clear();
- foreach (var o in zedManagerSpatialMapping.MappingChunkList)
- {
- MeshFilter m = o.o.GetComponent<MeshFilter>();
- if (m != null)
- {
- NavMeshBuildSource s = new NavMeshBuildSource();
- s.shape = NavMeshBuildSourceShape.Mesh;
- s.sourceObject = m.mesh;
- s.transform = o.o.transform.localToWorldMatrix;
- s.area = agentTypeID;
- sources.Add(s);
- }
- }
- #endif
- }
-
-
-
-
-
- void CalculateBounds(List<NavMeshBuildSource> sources)
- {
-
- if (sources.Count != 0) {
- bounds.center = transform.position;
-
-
- foreach (var src in sources) {
- Mesh m = src.sourceObject as Mesh;
- bounds.Encapsulate (m.bounds);
- }
- }
-
- bounds.Expand(0.1f);
- }
-
-
-
- void UpdateNavMesh()
- {
- isOver = false;
-
-
- CollectSources();
- #if UNITY_5_6_OR_NEWER
- if (sources.Count != 0)
- {
-
- CalculateBounds(sources);
-
- var defaultBuildSettings = NavMesh.GetSettingsByID(agentTypeID);
- NavMeshBuilder.UpdateNavMeshData(navMesh, defaultBuildSettings, sources, bounds);
-
- }
- #endif
- }
- #if UNITY_5_6_OR_NEWER
-
-
-
- public void SwitchStateDisplayNavMesh()
- {
- hideFlag = !hideFlag;
- if (navMeshObject != null)
- {
- navMeshObject.SetActive(hideFlag);
- }
- }
- #endif
- private void OnApplicationQuit()
- {
- Destroy(navMeshObject);
- }
- }
- #if UNITY_5_6_OR_NEWER
- #if UNITY_EDITOR
- [CustomEditor(typeof(NavMeshSurface))]
- class ZEDNavMeshEditor : Editor
- {
-
-
-
-
-
- SerializedProperty managerID;
-
-
-
- SerializedProperty agentID;
-
-
-
- SerializedProperty hideFlag;
-
-
-
- SerializedProperty isOver;
- private void OnEnable()
- {
-
- managerID = serializedObject.FindProperty("zedManagerSpatialMapping");
- agentID = serializedObject.FindProperty("agentTypeID");
- hideFlag = serializedObject.FindProperty("hideFlag");
- isOver = serializedObject.FindProperty("isOver");
- }
-
-
-
-
-
-
- public static void AgentTypePopup(string labelName, SerializedProperty agentTypeID)
- {
- var index = -1;
- var count = NavMesh.GetSettingsCount();
-
- GUIContent[] agentTypeNames = new GUIContent[count + 2];
- for (var i = 0; i < count; i++)
- {
- var id = NavMesh.GetSettingsByIndex(i).agentTypeID;
- var name = NavMesh.GetSettingsNameFromID(id);
- agentTypeNames[i] = new GUIContent(name);
- if (id == agentTypeID.intValue)
- index = i;
- }
- agentTypeNames[count] = new GUIContent("");
- agentTypeNames[count + 1] = new GUIContent("Open Agent Settings...");
- bool validAgentType = index != -1;
- if (!validAgentType)
- {
- EditorGUILayout.HelpBox("Agent Type invalid.", MessageType.Warning);
- }
- var rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);
- EditorGUI.BeginProperty(rect, GUIContent.none, agentTypeID);
- EditorGUI.BeginChangeCheck();
- GUIContent text = new GUIContent(labelName, "The ID of the agent type the NavMesh will be built for.");
- index = EditorGUI.Popup(rect, text, index, agentTypeNames);
- if (EditorGUI.EndChangeCheck())
- {
- if (index >= 0 && index < count)
- {
- var id = NavMesh.GetSettingsByIndex(index).agentTypeID;
- agentTypeID.intValue = id;
- }
- else if (index == count + 1)
- {
- UnityEditor.AI.NavMeshEditorHelpers.OpenAgentSettings(-1);
- }
- }
- EditorGUI.EndProperty();
- }
- public override void OnInspectorGUI()
- {
- NavMeshSurface obj = (NavMeshSurface)target;
- serializedObject.Update();
- GUIContent zedManagerLink = new GUIContent("ZED Rig", "ZEDManager (Rig) to link to NavMesh generation");
- obj.zedManagerSpatialMapping = (ZEDManager)EditorGUILayout.ObjectField(zedManagerLink, obj.zedManagerSpatialMapping , typeof(ZEDManager), true);
- AgentTypePopup("Agent Type", agentID);
- GUI.enabled = isOver.boolValue;
- GUIContent text = new GUIContent(hideFlag.boolValue ? "Hide" : "Display", "Toggle the visibility of the NavMesh.");
- if (GUILayout.Button(text))
- {
- obj.SwitchStateDisplayNavMesh();
- }
- serializedObject.ApplyModifiedProperties();
- }
- }
- #endif
- #endif
|