// STATIC LOGIC ------------------------------------------------------------------------------------------------------------------- /// <summary> /// Create a terrain from its terrain data. If the level has a terrain editor, then an existing terrain will be recycled. /// </summary> public static GameObject CreateOrRecycleTerrain(TerrainData p_terrainData, int p_terrainLayer) { // try to find an existing terrain LE_GUI3dTerrain gui3dTerrain = Object.FindObjectOfType <LE_GUI3dTerrain>(); Terrain terrain = gui3dTerrain != null ? gui3dTerrain.TerrainInstance : null; GameObject terrainGO; if (terrain != null) { // an editable terrain was already existent -> recycle it terrainGO = terrain.gameObject; gui3dTerrain.RecycleTerrain(p_terrainData, true); } else { // there is no editable terrain in this level -> create new terrain GO terrainGO = Terrain.CreateTerrainGameObject(p_terrainData); // assign terrain for further editing if (gui3dTerrain != null) { gui3dTerrain.SetTerrain(terrainGO.GetComponent <Terrain>()); } } terrainGO.layer = p_terrainLayer; terrainGO.transform.position = new Vector3(-p_terrainData.size.x * 0.5f, terrainGO.transform.position.y, -p_terrainData.size.z * 0.5f); return(terrainGO); }
private void InitializeDefaultTerrain(LE_ConfigTerrain p_LEConfTerrain) { // initialize custom default terrain if (p_LEConfTerrain.CustomDefaultTerrain != null && p_LEConfTerrain.CustomDefaultTerrain.terrainData != null) { // save a reference to the default data prefab m_GUI3dTerrain.DefaultTerrainDataPrefab = p_LEConfTerrain.CustomDefaultTerrain.terrainData; // clone the terrain data so that the asset is not broken when testing in Unity Editor p_LEConfTerrain.CustomDefaultTerrain.enabled = false; p_LEConfTerrain.CustomDefaultTerrain.terrainData = m_GUI3dTerrain.GetDefaultTerrainDataDeepCopy(); if (p_LEConfTerrain.CustomDefaultTerrain.GetComponent <TerrainCollider>() != null) { p_LEConfTerrain.CustomDefaultTerrain.GetComponent <TerrainCollider>().terrainData = p_LEConfTerrain.CustomDefaultTerrain.terrainData; } else { Debug.LogError("LE_LevelEditorMain: the CustomDefaultTerrain assigned to LE_ConfigTerrain must have a collider!"); } p_LEConfTerrain.CustomDefaultTerrain.Flush(); p_LEConfTerrain.CustomDefaultTerrain.enabled = true; // access the custom predefined terrain data // and wrap it with a terrain manager, which is then assigned to the GUI3dTerrain instance m_GUI3dTerrain.SetTerrain(p_LEConfTerrain.CustomDefaultTerrain); // your terrain must be in the LE_ConfigTerrain.TerrainLayer layer, you can set this in the game object, // but it is included here so that you cannot forget it p_LEConfTerrain.CustomDefaultTerrain.gameObject.layer = p_LEConfTerrain.TerrainLayer; // just to be on the safe side call this event and notify listeners that the level data was changed if (LE_EventInterface.OnChangeLevelData != null) { LE_EventInterface.OnChangeLevelData(p_LEConfTerrain.CustomDefaultTerrain.gameObject, new LE_LevelDataChangedEvent(LE_ELevelDataChangeType.TERRAIN_LOADED_DEFAULT)); } // a terrain does exist -> activate edit terrain UI if (LE_GUIInterface.Instance.delegates.SetTerrainUIMode != null) { LE_GUIInterface.Instance.delegates.SetTerrainUIMode(LE_GUIInterface.Delegates.ETerrainUIMode.EDIT); } else { Debug.LogWarning("LE_LevelEditorMain: you have not set the LE_GUIInterface.delegates.SetTerrainUIMode delegate. You need to set it for example if you want to disable the create UI if the default Unity terrain is set!"); } } else { // there is no terrain -> activate create terrain UI if (LE_GUIInterface.Instance.delegates.SetTerrainUIMode != null) { LE_GUIInterface.Instance.delegates.SetTerrainUIMode(LE_GUIInterface.Delegates.ETerrainUIMode.CREATE); } else { Debug.LogWarning("LE_LevelEditorMain: you have not set the LE_GUIInterface.delegates.SetTerrainUIMode delegate. You need to set it for example if you want to show the create UI if there is no default Unity terrain set!"); } } }