/// <summary> /// Creates and setup the feature on obj. /// </summary> /// <param name="obj">The object to spawn and configure the feature.</param> public void SetupFeature(GameObject obj) { // Add component obj.AddComponent <ArcTeleportManager>(); // Secondary scripts GameObject navmesh = new GameObject(); navmesh.name = "Navmesh"; navmesh.transform.parent = obj.transform; navmesh.AddComponent <BorderRenderer>(); navmesh.AddComponent <NavMeshRenderer>(); navmesh.transform.position = Vector3.zero; GameObject pointer = new GameObject(); pointer.name = "Pointer"; pointer.transform.parent = obj.transform; pointer.AddComponent <ParabolicPointer>(); pointer.transform.position = Vector3.zero; // Setup Component ArcTeleportManager teleportManager = obj.GetComponent <ArcTeleportManager>(); ParabolicPointer pointerComponent = pointer.GetComponent <ParabolicPointer>(); teleportManager.Pointer = pointerComponent; NavMeshRenderer navMeshHandler = navmesh.GetComponent <NavMeshRenderer>(); BorderRenderer border = navmesh.GetComponent <BorderRenderer>(); NavMeshHelper.ClearNavMesh(navMeshHandler, new SerializedObject(navMeshHandler), TeleportSettings); NavMeshHelper.UpdateNavMesh(navMeshHandler, new SerializedObject(navMeshHandler), TeleportSettings); }
/// <summary> /// Execute when Update Navmesh button is clicked. /// </summary> private void OnNavmeshUpdateClick() { GameObject featureObject = FindFeatureOfType(typeof(ArcTeleportManager)); if (featureObject == null) { Debug.LogError("Add Arc Teleport before clicking Update Navmesh."); } else { ArcTeleportManager arcTeleportManager = featureObject.GetComponent <ArcTeleportManager>(); NavMeshRenderer mesh = featureObject.transform.Find("Navmesh").GetComponent <NavMeshRenderer>(); // Area Mask // string[] areas = GameObjectUtility.GetNavMeshAreaNames(); int[] area_index = new int[areas.Length]; int temp_mask = 0; for (int x = 0; x < areas.Length; x++) { area_index[x] = GameObjectUtility.GetNavMeshAreaFromName(areas[x]); temp_mask |= ((TeleportSettings.NavAreaMask >> area_index[x]) & 1) << x; } EditorGUI.BeginChangeCheck(); temp_mask = EditorGUILayout.MaskField("Area Mask", temp_mask, areas); if (EditorGUI.EndChangeCheck()) { TeleportSettings.NavAreaMask = 0; for (int x = 0; x < areas.Length; x++) { TeleportSettings.NavAreaMask |= (((temp_mask >> x) & 1) == 1 ? 0 : 1) << area_index[x]; } TeleportSettings.NavAreaMask = ~TeleportSettings.NavAreaMask; } // Sanity check for Null properties // bool HasMesh = (mesh.SelectableMesh != null && mesh.SelectableMesh.vertexCount != 0) || (mesh.SelectableMeshBorder != null && mesh.SelectableMeshBorder.Length != 0); bool MeshNull = mesh.SelectableMesh == null; bool BorderNull = mesh.SelectableMeshBorder == null; if (MeshNull || BorderNull) { string str = "Internal Error: "; if (MeshNull) { str += "Selectable Mesh == null. "; } if (BorderNull) { str += "Border point array == null. "; } str += "This may lead to strange behavior or serialization. Try updating the mesh or delete and recreate the Navmesh object. "; str += "If you are able to consistently get a Vive Nav Mesh object into this state, please submit a bug report."; EditorGUILayout.HelpBox(str, MessageType.Error); } UnityEngine.AI.NavMeshTriangulation tri = UnityEngine.AI.NavMesh.CalculateTriangulation(); int vert_size, tri_size; NavMeshHelper.CullNavmeshTriangulation(ref tri, TeleportSettings.NavAreaMask, out vert_size, out tri_size); Mesh m = NavMeshHelper.ConvertNavmeshToMesh(tri, vert_size, tri_size); // Can't use SerializedProperties here because BorderPointSet doesn't derive from UnityEngine.Object mesh.SelectableMeshBorder = NavMeshHelper.FindBorderEdges(m); TeleportSettings.SelectableMesh = m; mesh.SelectableMesh = mesh.SelectableMesh; TeleportSettings.Save(); } }