Exemplo n.º 1
0
        private void DrawTerrains(bool helpEnabled)
        {
            bool originalGUIState = GUI.enabled;

#if GAIA_PRO_PRESENT
            EditorGUILayout.BeginHorizontal();
            m_editorUtils.Label("IngestTerrain", GUILayout.Width(100));
            m_ingestTerrain = (Terrain)EditorGUILayout.ObjectField(m_ingestTerrain, typeof(Terrain), true);
            if (m_editorUtils.Button("Ingest"))
            {
                ////Try to find the X and Z coordinate for the scene name
                //double minX = m_terrainLoaderManager.TerrainSceneStorage.m_terrainScenes.Select(x => x.m_pos.x).Min();
                //double maxX = m_terrainLoaderManager.TerrainSceneStorage.m_terrainScenes.Select(x => x.m_pos.x).Max();
                //double minZ = m_terrainLoaderManager.TerrainSceneStorage.m_terrainScenes.Select(x => x.m_pos.z).Min();
                //double maxZ = m_terrainLoaderManager.TerrainSceneStorage.m_terrainScenes.Select(x => x.m_pos.z).Max();
                GaiaSessionManager    sessionManager        = GaiaSessionManager.GetSessionManager();
                WorldCreationSettings worldCreationSettings = new WorldCreationSettings()
                {
                    m_autoUnloadScenes      = false,
                    m_applyFloatingPointFix = TerrainLoaderManager.Instance.TerrainSceneStorage.m_useFloatingPointFix,
                    m_isWorldMap            = false
                };
                TerrainScene newScene = TerrainSceneCreator.CreateTerrainScene(m_ingestTerrain.gameObject.scene, TerrainLoaderManager.Instance.TerrainSceneStorage, sessionManager.m_session, m_ingestTerrain.gameObject, worldCreationSettings);
                TerrainLoaderManager.Instance.TerrainSceneStorage.m_terrainScenes.Add(newScene);
                TerrainLoaderManager.Instance.SaveStorageData();
                GaiaSessionManager.AddTerrainScenesToBuildSettings(new List <TerrainScene>()
                {
                    newScene
                });
            }
            EditorGUILayout.EndHorizontal();
            GUILayout.Space(EditorGUIUtility.singleLineHeight);
#endif

            EditorGUILayout.BeginHorizontal();
            if (m_editorUtils.Button("AddToBuildSettings"))
            {
                if (TerrainLoaderManager.ColliderOnlyLoadingActive)
                {
                    if (EditorUtility.DisplayDialog(m_editorUtils.GetTextValue("AddColliderScenesToBuildSettingsPopupTitle"), m_editorUtils.GetTextValue("AddColliderScenesToBuildSettingsPopupText"), m_editorUtils.GetTextValue("Continue"), m_editorUtils.GetTextValue("Cancel")))
                    {
#if GAIA_PRO_PRESENT
                        GaiaSessionManager.AddOnlyColliderScenesToBuildSettings(m_terrainLoaderManager.TerrainSceneStorage.m_terrainScenes);
#endif
                    }
                }
                else
                {
                    if (EditorUtility.DisplayDialog(m_editorUtils.GetTextValue("AddToBuildSettingsPopupTitle"), m_editorUtils.GetTextValue("AddToBuildSettingsPopupText"), m_editorUtils.GetTextValue("Continue"), m_editorUtils.GetTextValue("Cancel")))
                    {
#if GAIA_PRO_PRESENT
                        GaiaSessionManager.AddTerrainScenesToBuildSettings(m_terrainLoaderManager.TerrainSceneStorage.m_terrainScenes);
#endif
                    }
                }
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            if (m_editorUtils.Button("UnloadAll"))
            {
                m_terrainLoaderManager.SetLoadingRange(0, 0);
                m_terrainLoaderManager.UnloadAll(true);
            }
            if (m_editorUtils.Button("LoadAll"))
            {
                if (EditorUtility.DisplayDialog(m_editorUtils.GetTextValue("LoadAllPopupTitle"), m_editorUtils.GetTextValue("LoadAllPopupText"), m_editorUtils.GetTextValue("Continue"), m_editorUtils.GetTextValue("Cancel")))
                {
                    foreach (TerrainScene terrainScene in m_terrainLoaderManager.TerrainSceneStorage.m_terrainScenes)
                    {
                        m_terrainLoaderManager.SetLoadingRange(100000, m_terrainLoaderManager.GetImpostorLoadingRange());
                        terrainScene.AddRegularReference(m_terrainLoaderManager.gameObject);
                    }
                }
            }

            EditorGUILayout.EndHorizontal();

            if (!GaiaUtils.HasImpostorTerrains())
            {
                GUI.enabled = false;
            }
            EditorGUILayout.BeginHorizontal();
            if (m_editorUtils.Button("UnloadAllImpostors"))
            {
                m_terrainLoaderManager.SetLoadingRange(m_terrainLoaderManager.GetLoadingRange(), 0);
                m_terrainLoaderManager.UnloadAllImpostors(true);
            }
            if (m_editorUtils.Button("LoadAllImpostors"))
            {
                if (EditorUtility.DisplayDialog(m_editorUtils.GetTextValue("LoadAllPopupTitle"), m_editorUtils.GetTextValue("LoadAllImpostorsPopupText"), m_editorUtils.GetTextValue("Continue"), m_editorUtils.GetTextValue("Cancel")))
                {
                    m_terrainLoaderManager.SetLoadingRange(m_terrainLoaderManager.GetLoadingRange(), 100000);
                    foreach (TerrainScene terrainScene in m_terrainLoaderManager.TerrainSceneStorage.m_terrainScenes)
                    {
                        terrainScene.AddImpostorReference(m_terrainLoaderManager.gameObject);
                    }
                }
            }

            EditorGUILayout.EndHorizontal();

            GUI.enabled = originalGUIState;

            GUILayout.Space(EditorGUIUtility.singleLineHeight);

            float buttonWidth1 = 110;
            float buttonWidth2 = 60;

            if (m_terrainBoxStyle == null || m_terrainBoxStyle.normal.background == null)
            {
                m_terrainBoxStyle         = new GUIStyle(EditorStyles.helpBox);
                m_terrainBoxStyle.margin  = new RectOffset(0, 0, 0, 0);
                m_terrainBoxStyle.padding = new RectOffset(3, 3, 3, 3);
            }

            int removeIndex  = -99;
            int currentIndex = 0;

            foreach (TerrainScene terrainScene in m_terrainLoaderManager.TerrainSceneStorage.m_terrainScenes)
            {
                EditorGUILayout.BeginVertical(m_terrainBoxStyle);
                {
                    EditorGUILayout.LabelField(terrainScene.GetTerrainName());
                    EditorGUILayout.BeginHorizontal();
                    bool isLoaded         = terrainScene.m_regularLoadState == LoadState.Loaded && terrainScene.TerrainObj != null && terrainScene.TerrainObj.activeInHierarchy;
                    bool isImpostorLoaded = terrainScene.m_impostorLoadState == LoadState.Loaded || terrainScene.m_impostorLoadState == LoadState.Cached;

                    bool currentGUIState = GUI.enabled;
                    GUI.enabled = isLoaded;
                    if (m_editorUtils.Button("SelectPlaceholder", GUILayout.Width(buttonWidth1)))
                    {
                        Selection.activeGameObject = GameObject.Find(terrainScene.GetTerrainName());
                        EditorGUIUtility.PingObject(Selection.activeObject);
                    }
                    GUI.enabled = currentGUIState;
                    if (isLoaded)
                    {
                        if (m_editorUtils.Button("UnloadPlaceholder", GUILayout.Width(buttonWidth2)))
                        {
                            if (ResetToWorldOriginLoading())
                            {
                                terrainScene.RemoveAllReferences(true);
                            }
                        }
                    }
                    else
                    {
                        if (m_editorUtils.Button("LoadPlaceholder", GUILayout.Width(buttonWidth2)))
                        {
                            if (ResetToWorldOriginLoading())
                            {
                                terrainScene.AddRegularReference(m_terrainLoaderManager.gameObject);
                            }
                        }
                    }

                    if (string.IsNullOrEmpty(terrainScene.m_impostorScenePath))
                    {
                        GUI.enabled = false;
                    }

                    if (isImpostorLoaded)
                    {
                        if (m_editorUtils.Button("UnLoadImpostor", GUILayout.Width(buttonWidth1)))
                        {
                            if (ResetToWorldOriginLoading())
                            {
                                terrainScene.RemoveImpostorReference(m_terrainLoaderManager.gameObject, 0, true);
                            }
                        }
                    }
                    else
                    {
                        if (m_editorUtils.Button("LoadImpostor", GUILayout.Width(buttonWidth1)))
                        {
                            if (ResetToWorldOriginLoading())
                            {
                                terrainScene.AddImpostorReference(m_terrainLoaderManager.gameObject);
                            }
                        }
                    }

                    GUI.enabled = originalGUIState;

                    if (m_editorUtils.Button("RemoveScene"))
                    {
                        if (EditorUtility.DisplayDialog(m_editorUtils.GetTextValue("RemoveSceneTitle"), m_editorUtils.GetTextValue("RemoveSceneText"), m_editorUtils.GetTextValue("Continue"), m_editorUtils.GetTextValue("Cancel")))
                        {
                            removeIndex = currentIndex;
                        }
                    }

                    EditorGUILayout.EndHorizontal();
                    if (terrainScene.RegularReferences.Count > 0)
                    {
                        EditorGUI.indentLevel++;
                        terrainScene.m_isFoldedOut = m_editorUtils.Foldout(terrainScene.m_isFoldedOut, "ShowTerrainReferences");
                        if (terrainScene.m_isFoldedOut)
                        {
                            foreach (GameObject go in terrainScene.RegularReferences)
                            {
                                EditorGUILayout.BeginHorizontal();
                                GUILayout.Space(20);
                                m_editorUtils.Label(new GUIContent(go.name, m_editorUtils.GetTextValue("TerrainReferenceToolTip")));
                                if (m_editorUtils.Button("TerrainReferenceSelect", GUILayout.Width(buttonWidth1)))
                                {
                                    Selection.activeObject = go;
                                    SceneView.lastActiveSceneView.FrameSelected();
                                }
                                if (m_editorUtils.Button("TerrainReferenceRemove", GUILayout.Width(buttonWidth2)))
                                {
                                    terrainScene.RemoveRegularReference(go);
                                }
                                GUILayout.Space(100);
                                EditorGUILayout.EndHorizontal();
                            }
                        }
                        EditorGUI.indentLevel--;
                    }
                    if (terrainScene.ImpostorReferences.Count > 0)
                    {
                        EditorGUI.indentLevel++;
                        terrainScene.m_isFoldedOut = m_editorUtils.Foldout(terrainScene.m_isFoldedOut, "ShowImpostorReferences");
                        if (terrainScene.m_isFoldedOut)
                        {
                            foreach (GameObject go in terrainScene.ImpostorReferences)
                            {
                                EditorGUILayout.BeginHorizontal();
                                GUILayout.Space(20);
                                m_editorUtils.Label(new GUIContent(go.name, m_editorUtils.GetTextValue("TerrainReferenceToolTip")));
                                if (m_editorUtils.Button("TerrainReferenceSelect", GUILayout.Width(buttonWidth1)))
                                {
                                    Selection.activeObject = go;
                                    SceneView.lastActiveSceneView.FrameSelected();
                                }
                                if (m_editorUtils.Button("TerrainReferenceRemove", GUILayout.Width(buttonWidth2)))
                                {
                                    terrainScene.RemoveImpostorReference(go);
                                }
                                GUILayout.Space(100);
                                EditorGUILayout.EndHorizontal();
                            }
                        }
                        EditorGUI.indentLevel--;
                    }
                }
                GUILayout.EndVertical();
                GUILayout.Space(5f);
                currentIndex++;
            }

            if (removeIndex != -99)
            {
                AssetDatabase.DeleteAsset(m_terrainLoaderManager.TerrainSceneStorage.m_terrainScenes[removeIndex].m_scenePath);
                m_terrainLoaderManager.TerrainSceneStorage.m_terrainScenes.RemoveAt(removeIndex);
                m_terrainLoaderManager.SaveStorageData();
            }
        }
Exemplo n.º 2
0
        public void CreateWorldMapTerrain()
        {
            //Remove old world map terrain, if any
            Terrain oldWMTerrain = TerrainHelper.GetWorldMapTerrain();

            if (oldWMTerrain != null)
            {
#if UNITY_EDITOR
                if (EditorUtility.DisplayDialog("Delete old world map?", "You are about to create a new world map in this scene. This will delete the existing world map and the stamp tokens on it. This will only affect the small world map terrain, the actual full scene terrain tiles will not be removed. Continue?", "Continue", "Cancel"))
                {
                    DestroyImmediate(oldWMTerrain.gameObject);
                }
                else
                {
                    return;
                }
#else
                Destroy(oldWMTerrain.gameObject);
#endif
            }

            GaiaSettings       gaiaSettings    = GaiaUtils.GetGaiaSettings();
            GaiaDefaults       currentDefaults = gaiaSettings.m_currentDefaults;
            GaiaSessionManager gsm             = GaiaSessionManager.GetSessionManager();

            //Collect some info from the existing world, if any.

            //We need the average height from the terrains to create the height of the worldmap accordingly in scale
            double averageHeight = currentDefaults.m_terrainHeight;
            //We need the heightmap resolution and terrain size stored in the session
            //To properly scale between world map and local map even when one of the two is not loaded at the moment

            int count = 0;

            foreach (Terrain t in Terrain.activeTerrains)
            {
                averageHeight += t.terrainData.size.y;
                if (!TerrainHelper.IsWorldMapTerrain(t))
                {
                    m_localHeightmapRes = t.terrainData.heightmapResolution;
                    m_localTileSize     = t.terrainData.size.x;
                }

                count++;
            }
            //}
            //only calculate an average if there is at least one placeholder or terrain
            if (count > 0)
            {
                averageHeight /= count;
            }


            BoundsDouble bounds = new BoundsDouble();
            TerrainHelper.GetTerrainBounds(ref bounds);
            //Collect the new world settings for world creation
            WorldCreationSettings worldCreationSettings = ScriptableObject.CreateInstance <WorldCreationSettings>();
            worldCreationSettings.m_xTiles                = 1;
            worldCreationSettings.m_zTiles                = 1;
            worldCreationSettings.m_tileSize              = (count > 0) ? Mathd.RoundToInt(bounds.size.x) : gaiaSettings.m_tilesX * currentDefaults.m_terrainSize;
            worldCreationSettings.m_tileHeight            = currentDefaults.m_terrainHeight;
            worldCreationSettings.m_createInScene         = false;
            worldCreationSettings.m_autoUnloadScenes      = false;
            worldCreationSettings.m_applyFloatingPointFix = false;
            worldCreationSettings.m_applyFloatingPointFix = false;
            worldCreationSettings.m_centerOffset          = Vector3.zero; //(count > 0) ? new Vector2Double(bounds.center.x + bounds.extents.x + worldCreationSettings.m_tileSize, bounds.center.z) : Vector2Double.zero;
            worldCreationSettings.m_isWorldMap            = true;
            //clone defaults so we can inject our custom heightmap resolution for the worldmap
            GaiaDefaults clonedDefaults = Instantiate(currentDefaults);
            clonedDefaults.m_heightmapResolution = (int)m_heightmapResolution;

            worldCreationSettings.m_gaiaDefaults = clonedDefaults;

            m_currentWorldCreationSettings     = worldCreationSettings;
            GaiaSessionManager.OnWorldCreated -= OnWorldMapCreate;
            GaiaSessionManager.OnWorldCreated += OnWorldMapCreate;
            GaiaSessionManager.CreateWorld(worldCreationSettings);
        }