Пример #1
0
    void InitGame()
    {
        surface = Instantiate(surfacePrefab);
        (levelRooms, connectedRooms) = levelGenerator.GenerateLevel();
        statManager.InitiateStats(levelRooms);
        gearManager.InitiateGears(levelRooms);
        currentRoomIndex = new RoomIndex {
            abs = 0, ord = 0
        };
        currentRoom = levelRooms[currentRoomIndex];
        map         = mapController.CreateMap(levelRooms, connectedRooms);
        map.SetActive(false);

        playerParam = new UnitParam
        {
            id         = 0,
            unitNature = UnitNature.Player,
            stat       = statManager.GenerateStat(UnitNature.Player),
            gear       = new Gear {
            }
        };

        roomGenerator.SetupRoom(currentRoom, playerSpawn);
        surface.BuildNavMesh();
    }
Пример #2
0
        static string GetAndEnsureTargetPath(NavMeshSurface2d surface)
        {
            // Create directory for the asset if it does not exist yet.
            var activeScenePath = surface.gameObject.scene.path;

            var targetPath = "Assets";

            if (!string.IsNullOrEmpty(activeScenePath))
            {
                targetPath = Path.Combine(Path.GetDirectoryName(activeScenePath), Path.GetFileNameWithoutExtension(activeScenePath));
            }
            else
            {
                var prefabStage    = PrefabStageUtility.GetPrefabStage(surface.gameObject);
                var isPartOfPrefab = prefabStage != null && prefabStage.IsPartOfPrefabContents(surface.gameObject);
                if (isPartOfPrefab && !string.IsNullOrEmpty(prefabStage.prefabAssetPath))
                {
                    var prefabDirectoryName = Path.GetDirectoryName(prefabStage.prefabAssetPath);
                    if (!string.IsNullOrEmpty(prefabDirectoryName))
                    {
                        targetPath = prefabDirectoryName;
                    }
                }
            }
            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }
            return(targetPath);
        }
Пример #3
0
        void DeleteStoredPrefabNavMeshDataAsset(NavMeshSurface2d surface)
        {
            for (var i = m_PrefabNavMeshDataAssets.Count - 1; i >= 0; i--)
            {
                var storedAssetInfo = m_PrefabNavMeshDataAssets[i];
                if (storedAssetInfo.surface == surface)
                {
                    var storedNavMeshData = storedAssetInfo.navMeshData;
                    if (storedNavMeshData != null && storedNavMeshData != surface.navMeshData)
                    {
                        var assetPath = AssetDatabase.GetAssetPath(storedNavMeshData);
                        AssetDatabase.DeleteAsset(assetPath);
                    }

                    m_PrefabNavMeshDataAssets.RemoveAt(i);
                    break;
                }
            }

            if (m_PrefabNavMeshDataAssets.Count == 0)
            {
                PrefabStage.prefabSaving       -= DeleteStoredNavMeshDataAssetsForOwnedSurfaces;
                PrefabStage.prefabStageClosing -= ForgetUnsavedNavMeshDataChanges;
            }
        }
Пример #4
0
        static void SetNavMeshData(NavMeshSurface2d navSurface, NavMeshData navMeshData)
        {
            var so = new SerializedObject(navSurface);
            var navMeshDataProperty = so.FindProperty("m_NavMeshData");

            navMeshDataProperty.objectReferenceValue = navMeshData;
            so.ApplyModifiedPropertiesWithoutUndo();
        }
Пример #5
0
        static NavMeshData InitializeBakeData(NavMeshSurface2d surface)
        {
            var emptySources = new List <NavMeshBuildSource>();
            var emptyBounds  = new Bounds();

            return(UnityEngine.AI.NavMeshBuilder.BuildNavMeshData(surface.GetBuildSettings(), emptySources, emptyBounds
                                                                  , surface.transform.position, surface.transform.rotation));
        }
Пример #6
0
        static void CreateNavMeshAsset(NavMeshSurface2d surface)
        {
            var targetPath = GetAndEnsureTargetPath(surface);

            var combinedAssetPath = Path.Combine(targetPath, "NavMesh-" + surface.name + ".asset");

            combinedAssetPath = AssetDatabase.GenerateUniqueAssetPath(combinedAssetPath);
            AssetDatabase.CreateAsset(surface.navMeshData, combinedAssetPath);
        }
Пример #7
0
 void InitRoom()
 {
     surface     = Instantiate(surfacePrefab);
     currentRoom = GetNextRoom();
     mapController.UpdateMap(currentRoom);
     map.SetActive(false);
     roomGenerator.SetupRoom(currentRoom, playerSpawn);
     surface.BuildNavMesh();
 }
Пример #8
0
    private void Start()
    {
        navMeshSurface2d = FindObjectOfType <NavMeshSurface2d>();

        NewRoute(0, 0, amountPossiblesCorridors.Random, RandomDirection(), 0);
        FillWalls();
        SetPlayerPosition();
        PopulateRooms();
        BuildNavMeshSurface();
    }
Пример #9
0
    public void GetNavMeshSurface()
    {
        if (navMeshSurface2d != null)
        {
            Destroy(navMeshSurface2d.gameObject);
        }

        navMeshSurface2d = FindObjectOfType <NavMeshSurface2d>();
        navMeshSurface2d.gameObject.transform.parent = transform;
        navMeshSurface2d.enabled = true;
    }
Пример #10
0
 static void RenderBoxGizmoNotSelected(NavMeshSurface2d navSurface, GizmoType gizmoType)
 {
     if (NavMeshVisualizationSettings.showNavigation > 0)
     {
         RenderBoxGizmo(navSurface, gizmoType, false);
     }
     else
     {
         Gizmos.DrawIcon(navSurface.transform.position, "NavMeshSurface Icon", true);
     }
 }
Пример #11
0
        void ClearSurface(NavMeshSurface2d navSurface)
        {
            var assetToDelete = GetNavMeshAssetToDelete(navSurface);

            navSurface.RemoveData();
            navSurface.navMeshData = null;
            EditorUtility.SetDirty(navSurface);

            if (assetToDelete)
            {
                AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(assetToDelete));
                EditorSceneManager.MarkSceneDirty(navSurface.gameObject.scene);
            }
        }
Пример #12
0
    static public Vector3 CreateRandomValidStartPoint()
    {
        NavMeshSurface2d navMesh = GameObject.FindGameObjectWithTag("NavMeshUnits").GetComponent <NavMeshSurface2d>();

        float randX = Random.Range(minX, maxX);
        float randY = Random.Range(minY, maxY);

        Vector3 randPos = new Vector3(randX, randY, 0);

        Debug.Log(randPos);


        return(randPos);
    }
Пример #13
0
        static NavMeshData GetNavMeshAssetToDelete(NavMeshSurface2d navSurface)
        {
            var prefabType = PrefabUtility.GetPrefabType(navSurface);

            if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance)
            {
                // Don't allow deleting the asset belonging to the prefab parent
                var parentSurface = PrefabUtility.GetCorrespondingObjectFromSource(navSurface) as NavMeshSurface2d;
                if (parentSurface && navSurface.navMeshData == parentSurface.navMeshData)
                {
                    return(null);
                }
            }
            return(navSurface.navMeshData);
        }
Пример #14
0
        bool IsCurrentPrefabNavMeshDataStored(NavMeshSurface2d surface)
        {
            if (surface == null)
            {
                return(false);
            }

            foreach (var storedAssetInfo in m_PrefabNavMeshDataAssets)
            {
                if (storedAssetInfo.surface == surface)
                {
                    return(storedAssetInfo.navMeshData == surface.navMeshData);
                }
            }

            return(false);
        }
Пример #15
0
        static string GetAndEnsureTargetPath(NavMeshSurface2d surface)
        {
            // Create directory for the asset if it does not exist yet.
            var activeScenePath = surface.gameObject.scene.path;

            var targetPath = "Assets";

            if (!string.IsNullOrEmpty(activeScenePath))
            {
                targetPath = Path.Combine(Path.GetDirectoryName(activeScenePath), Path.GetFileNameWithoutExtension(activeScenePath));
            }
            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }
            return(targetPath);
        }
Пример #16
0
        static void RenderBoxGizmo(NavMeshSurface2d navSurface, GizmoType gizmoType, bool selected)
        {
            var color = selected ? s_HandleColorSelected : s_HandleColor;

            if (!navSurface.enabled)
            {
                color = s_HandleColorDisabled;
            }

            var oldColor  = Gizmos.color;
            var oldMatrix = Gizmos.matrix;

            // Use the unscaled matrix for the NavMeshSurface
            var localToWorld = Matrix4x4.TRS(navSurface.transform.position, navSurface.transform.rotation, Vector3.one);

            Gizmos.matrix = localToWorld;

            if (navSurface.collectObjects == CollectObjects2d.Volume)
            {
                Gizmos.color = color;
                Gizmos.DrawWireCube(navSurface.center, navSurface.size);

                if (selected && navSurface.enabled)
                {
                    var colorTrans = new Color(color.r * 0.75f, color.g * 0.75f, color.b * 0.75f, color.a * 0.15f);
                    Gizmos.color = colorTrans;
                    Gizmos.DrawCube(navSurface.center, navSurface.size);
                }
            }
            else
            {
                if (navSurface.navMeshData != null)
                {
                    var bounds = navSurface.navMeshData.sourceBounds;
                    Gizmos.color = Color.grey;
                    Gizmos.DrawWireCube(bounds.center, bounds.size);
                }
            }

            Gizmos.matrix = oldMatrix;
            Gizmos.color  = oldColor;

            Gizmos.DrawIcon(navSurface.transform.position, "NavMeshSurface Icon", true);
        }
Пример #17
0
        void ClearSurface(NavMeshSurface2d navSurface)
        {
            var hasNavMeshData = navSurface.navMeshData != null;

            StoreNavMeshDataIfInPrefab(navSurface);

            var assetToDelete = GetNavMeshAssetToDelete(navSurface);

            navSurface.RemoveData();

            if (hasNavMeshData)
            {
                SetNavMeshData(navSurface, null);
                EditorSceneManager.MarkSceneDirty(navSurface.gameObject.scene);
            }

            if (assetToDelete)
            {
                AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(assetToDelete));
            }
        }
Пример #18
0
        public bool IsSurfaceBaking(NavMeshSurface2d surface)
        {
            if (surface == null)
            {
                return(false);
            }

            foreach (var oper in m_BakeOperations)
            {
                if (oper.surface == null || oper.bakeOperation == null)
                {
                    continue;
                }

                if (oper.surface == surface)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #19
0
        void StoreNavMeshDataIfInPrefab(NavMeshSurface2d surfaceToStore)
        {
            var prefabStage    = PrefabStageUtility.GetPrefabStage(surfaceToStore.gameObject);
            var isPartOfPrefab = prefabStage != null && prefabStage.IsPartOfPrefabContents(surfaceToStore.gameObject);

            if (!isPartOfPrefab)
            {
                return;
            }

            // check if data has already been stored for this surface
            foreach (var storedAssetInfo in m_PrefabNavMeshDataAssets)
            {
                if (storedAssetInfo.surface == surfaceToStore)
                {
                    return;
                }
            }

            if (m_PrefabNavMeshDataAssets.Count == 0)
            {
                PrefabStage.prefabSaving -= DeleteStoredNavMeshDataAssetsForOwnedSurfaces;
                PrefabStage.prefabSaving += DeleteStoredNavMeshDataAssetsForOwnedSurfaces;

                PrefabStage.prefabStageClosing -= ForgetUnsavedNavMeshDataChanges;
                PrefabStage.prefabStageClosing += ForgetUnsavedNavMeshDataChanges;
            }

            var isDataOwner = true;

            if (PrefabUtility.IsPartOfPrefabInstance(surfaceToStore) && !PrefabUtility.IsPartOfModelPrefab(surfaceToStore))
            {
                var basePrefabSurface = PrefabUtility.GetCorrespondingObjectFromSource(surfaceToStore) as NavMeshSurface2d;
                isDataOwner = basePrefabSurface == null || surfaceToStore.navMeshData != basePrefabSurface.navMeshData;
            }
            m_PrefabNavMeshDataAssets.Add(new SavedPrefabNavMeshData {
                surface = surfaceToStore, navMeshData = isDataOwner ? surfaceToStore.navMeshData : null
            });
        }
Пример #20
0
        NavMeshData GetNavMeshAssetToDelete(NavMeshSurface2d navSurface)
        {
            if (PrefabUtility.IsPartOfPrefabInstance(navSurface) && !PrefabUtility.IsPartOfModelPrefab(navSurface))
            {
                // Don't allow deleting the asset belonging to the prefab parent
                var parentSurface = PrefabUtility.GetCorrespondingObjectFromSource(navSurface) as NavMeshSurface2d;
                if (parentSurface && navSurface.navMeshData == parentSurface.navMeshData)
                {
                    return(null);
                }
            }

            // Do not delete the NavMeshData asset referenced from a prefab until the prefab is saved
            var prefabStage    = PrefabStageUtility.GetPrefabStage(navSurface.gameObject);
            var isPartOfPrefab = prefabStage != null && prefabStage.IsPartOfPrefabContents(navSurface.gameObject);

            if (isPartOfPrefab && IsCurrentPrefabNavMeshDataStored(navSurface))
            {
                return(null);
            }

            return(navSurface.navMeshData);
        }
Пример #21
0
 public void LoadNav()
 {
     nav = GetComponent <NavMeshSurface2d>();
     nav.BuildNavMesh();
 }
Пример #22
0
 static void RenderBoxGizmoSelected(NavMeshSurface2d navSurface, GizmoType gizmoType)
 {
     RenderBoxGizmo(navSurface, gizmoType, true);
 }
Пример #23
0
 void Start()
 {
     navMeshSurface = FindObjectOfType <NavMeshSurface2d>();
     StartCoroutine(BakeNavMesh());
 }
Пример #24
0
        void ForgetUnsavedNavMeshDataChanges(PrefabStage prefabStage)
        {
            // Debug.Log("On prefab closing - forget about this object's surfaces and stop caring about prefab saving");

            if (prefabStage == null)
            {
                return;
            }

            var allSurfacesInPrefab          = prefabStage.prefabContentsRoot.GetComponentsInChildren <NavMeshSurface2d>(true);
            NavMeshSurface2d surfaceInPrefab = null;
            var index = 0;

            do
            {
                if (allSurfacesInPrefab.Length > 0)
                {
                    surfaceInPrefab = allSurfacesInPrefab[index];
                }

                for (var i = m_PrefabNavMeshDataAssets.Count - 1; i >= 0; i--)
                {
                    var storedPrefabInfo = m_PrefabNavMeshDataAssets[i];
                    if (storedPrefabInfo.surface == null)
                    {
                        // Debug.LogFormat("A surface from the prefab got deleted after it has baked a new NavMesh but it hasn't saved it. Now the unsaved asset gets deleted. ({0})", storedPrefabInfo.navMeshData);

                        // surface got deleted, thus delete its initial NavMeshData asset
                        if (storedPrefabInfo.navMeshData != null)
                        {
                            var assetPath = AssetDatabase.GetAssetPath(storedPrefabInfo.navMeshData);
                            AssetDatabase.DeleteAsset(assetPath);
                        }

                        m_PrefabNavMeshDataAssets.RemoveAt(i);
                    }
                    else if (surfaceInPrefab != null && storedPrefabInfo.surface == surfaceInPrefab)
                    {
                        //Debug.LogFormat("The surface {0} from the prefab was storing the original navmesh data and now will be forgotten", surfaceInPrefab);

                        var baseSurface = PrefabUtility.GetCorrespondingObjectFromSource(surfaceInPrefab) as NavMeshSurface2d;
                        if (baseSurface == null || surfaceInPrefab.navMeshData != baseSurface.navMeshData)
                        {
                            var assetPath = AssetDatabase.GetAssetPath(surfaceInPrefab.navMeshData);
                            AssetDatabase.DeleteAsset(assetPath);

                            //Debug.LogFormat("The surface {0} from the prefab has baked new NavMeshData but did not save this change so the asset has been now deleted. ({1})",
                            //    surfaceInPrefab, assetPath);
                        }

                        m_PrefabNavMeshDataAssets.RemoveAt(i);
                    }
                }
            } while (++index < allSurfacesInPrefab.Length);

            if (m_PrefabNavMeshDataAssets.Count == 0)
            {
                PrefabStage.prefabSaving       -= DeleteStoredNavMeshDataAssetsForOwnedSurfaces;
                PrefabStage.prefabStageClosing -= ForgetUnsavedNavMeshDataChanges;
            }
        }
Пример #25
0
 // Start is called before the first frame update
 void Start()
 {
     nav = GetComponent <NavMeshSurface2d>();
 }