public void SetOriginByTargetTile(int tileX = -99, int tileZ = -99) { if (tileX == -99) { tileX = m_originTargetTileX; } if (tileZ == -99) { tileZ = m_originTargetTileZ; } if (GaiaUtils.HasDynamicLoadedTerrains()) { //Get the terrain tile by X / Z tile in the scene path TerrainScene targetScene = TerrainLoaderManager.TerrainScenes.Find(x => x.m_scenePath.Contains("Terrain_" + tileX.ToString() + "_" + tileZ.ToString())); if (targetScene != null) { SetOrigin(new Vector3Double(targetScene.m_pos.x + (m_terrainSceneStorage.m_terrainTilesSize / 2f), 0f, targetScene.m_pos.z + (m_terrainSceneStorage.m_terrainTilesSize / 2f))); string terrainName = targetScene.GetTerrainName(); GameObject go = GameObject.Find(terrainName); if (go != null) { #if UNITY_EDITOR Selection.activeObject = go; #endif } } else { Debug.LogWarning("Could not find a terrain with the tile coordinates " + tileX.ToString() + "-" + tileZ.ToString() + " in the available terrains. Please check if these coordinates are within the available bounds."); } } else { Terrain t = Terrain.activeTerrains.Where(x => x.name.Contains("Terrain_" + tileX.ToString() + "_" + tileZ.ToString())).First(); if (t != null) { SetOrigin(new Vector3Double(t.transform.position.x + (t.terrainData.size.x / 2f), 0f, t.transform.position.z + (t.terrainData.size.z / 2f))); #if UNITY_EDITOR Selection.activeObject = t.gameObject; #endif } else { Debug.LogWarning("Could not find a terrain with the tile coordinates " + tileX.ToString() + "-" + tileZ.ToString() + " in the scene."); } } }
private void AddToTerrainSceneActionQueue(TerrainScene terrainScene, ReferenceChange referenceChange, float distance, bool forced = false) { int index = m_terrainSceneActionQueue.FindIndex(x => x.m_distance > distance); if (index != -1) { //We found an entry with a larger distance -> insert it in before that m_terrainSceneActionQueue.Insert(index, new TerrainSceneActionQueueEntry { m_terrainScene = terrainScene, m_referenceChange = referenceChange, m_forced = forced, m_distance = distance }); } else { //no larger distance found, this needs to go to the end of the queue m_terrainSceneActionQueue.Add(new TerrainSceneActionQueueEntry { m_terrainScene = terrainScene, m_referenceChange = referenceChange, m_forced = forced, m_distance = distance }); } }
public void LoadStorageData() { #if UNITY_EDITOR //Try to get the terrain scene storage file from the last used GUID first if (!String.IsNullOrEmpty(m_lastUsedGUID)) { m_terrainSceneStorage = (TerrainSceneStorage)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(m_lastUsedGUID), typeof(TerrainSceneStorage)); } //No guid / storage object? Then we need to create one in the current session directory if (m_terrainSceneStorage == null) { GaiaSessionManager gsm = GaiaSessionManager.GetSessionManager(); if (gsm != null && gsm.m_session != null) { string path = GaiaDirectories.GetScenePath(gsm.m_session) + "/TerrainScenes.asset"; if (File.Exists(path)) { m_terrainSceneStorage = (TerrainSceneStorage)AssetDatabase.LoadAssetAtPath(path, typeof(TerrainSceneStorage)); } else { m_terrainSceneStorage = ScriptableObject.CreateInstance <TerrainSceneStorage>(); if (TerrainHelper.GetWorldMapTerrain() != null) { m_terrainSceneStorage.m_hasWorldMap = true; } AssetDatabase.CreateAsset(m_terrainSceneStorage, path); AssetDatabase.ImportAsset(path); } } else { m_terrainSceneStorage = ScriptableObject.CreateInstance <TerrainSceneStorage>(); } } //Check if there are scene files existing already and if they are in the storage data - if not, we should pick them up accordingly string directory = GaiaDirectories.GetTerrainScenePathForStorageFile(m_terrainSceneStorage); var dirInfo = new DirectoryInfo(directory); bool madeChanges = false; if (dirInfo != null) { FileInfo[] allFiles = dirInfo.GetFiles(); foreach (FileInfo fileInfo in allFiles) { if (fileInfo.Extension == ".unity") { string path = GaiaDirectories.GetPathStartingAtAssetsFolder(fileInfo.FullName); if (!m_terrainSceneStorage.m_terrainScenes.Exists(x => x.GetTerrainName() == x.GetTerrainName(path))) { string firstSegment = fileInfo.Name.Split('-')[0]; int xCoord = -99; int zCoord = -99; bool successX, successZ; try { successX = Int32.TryParse(firstSegment.Substring(firstSegment.IndexOf('_') + 1, firstSegment.LastIndexOf('_') - (firstSegment.IndexOf('_') + 1)), out xCoord); successZ = Int32.TryParse(firstSegment.Substring(firstSegment.LastIndexOf('_') + 1, firstSegment.Length - 1 - firstSegment.LastIndexOf('_')), out zCoord); } catch (Exception ex) { if (ex.Message == "123") { } successX = false; successZ = false; } if (successX && successZ) { //double centerX = (xCoord - (m_terrainSceneStorage.m_terrainTilesX / 2f)) * m_terrainSceneStorage.m_terrainTilesSize + (m_terrainSceneStorage.m_terrainTilesSize /2f); //double centerZ = (zCoord - (m_terrainSceneStorage.m_terrainTilesZ / 2f)) * m_terrainSceneStorage.m_terrainTilesSize + (m_terrainSceneStorage.m_terrainTilesSize / 2f); Vector2 offset = new Vector2(-m_terrainSceneStorage.m_terrainTilesSize * m_terrainSceneStorage.m_terrainTilesX * 0.5f, -m_terrainSceneStorage.m_terrainTilesSize * m_terrainSceneStorage.m_terrainTilesZ * 0.5f); Vector3Double position = new Vector3(m_terrainSceneStorage.m_terrainTilesSize * xCoord + offset.x, 0, m_terrainSceneStorage.m_terrainTilesSize * zCoord + offset.y); Vector3Double center = new Vector3Double(position + new Vector3Double(m_terrainSceneStorage.m_terrainTilesSize / 2f, 0f, m_terrainSceneStorage.m_terrainTilesSize / 2f)); BoundsDouble bounds = new BoundsDouble(center, new Vector3Double(m_terrainSceneStorage.m_terrainTilesSize, m_terrainSceneStorage.m_terrainTilesSize * 4, m_terrainSceneStorage.m_terrainTilesSize)); //Use forward slashes in the path - The Unity scene management classes expect it that way path = path.Replace("\\", "/"); TerrainScene terrainScene = new TerrainScene() { m_scenePath = path, m_pos = position, m_bounds = bounds, m_useFloatingPointFix = m_terrainSceneStorage.m_useFloatingPointFix }; if (File.Exists(path.Replace("Terrain", GaiaConstants.ImpostorTerrainName))) { terrainScene.m_impostorScenePath = path.Replace("Terrain", GaiaConstants.ImpostorTerrainName); } if (File.Exists(path.Replace("Terrain", "Collider"))) { terrainScene.m_colliderScenePath = path.Replace("Terrain", "Collider"); } if (File.Exists(path.Replace("Terrain", "Backup"))) { terrainScene.m_backupScenePath = path.Replace("Terrain", "Backup"); } m_terrainSceneStorage.m_terrainScenes.Add(terrainScene); madeChanges = true; } } } } if (madeChanges) { EditorUtility.SetDirty(m_terrainSceneStorage); AssetDatabase.SaveAssets(); } } m_lastUsedGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m_terrainSceneStorage)); RefreshTerrainsWithCurrentData(); RefreshSceneViewLoadingRange(); ////Go over the currently open scene and close the ones that do not seem to have a reference on them //for (int i = EditorSceneManager.loadedSceneCount-1; i >= 0; i--) //{ // Scene scene = EditorSceneManager.GetSceneAt(i); // if (EditorSceneManager.GetActiveScene().Equals(scene)) // { // continue; // } // TerrainScene terrainScene = m_terrainSceneStorage.m_terrainScenes.Find(x => x.m_scenePath == scene.path || x.m_impostorScenePath == scene.path || x.m_colliderScenePath == scene.path); // if (terrainScene != null) // { // terrainScene.UpdateWithCurrentData(); // } // else // { // EditorSceneManager.UnloadSceneAsync(scene); // } //} #endif }
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(); } }