private void SaveLevel() { if (LE_EventInterface.OnSave != null) { // collect level meta data, which depends on the game's implementation LE_CollectMetaDataEvent collectMetaData = new LE_CollectMetaDataEvent(); if (LE_EventInterface.OnCollectMetaDataBeforeSave != null) { LE_EventInterface.OnCollectMetaDataBeforeSave(this, collectMetaData); } // save int removedDuplicatesCount = 0; if (m_confL.IsRemoveDuplicatesOnSave) { removedDuplicatesCount = LE_SaveLoad.RemoveDuplicatesInCurrentLevel(); } LE_SaveEvent saveEventArgs = new LE_SaveEvent( LE_SaveLoad.SaveCurrentLevelDataToByteArray(m_configTextures), LE_SaveLoad.SaveCurrentLevelMetaToByteArray(m_levelIcon, collectMetaData.GetCollectedMetaData()), removedDuplicatesCount); LE_EventInterface.OnSave(this, saveEventArgs); } else { Debug.LogError("LE_LogicLevel: OnLevelLoadBtn: you have to provide an event handler for 'LE_EventInterface.OnSave' to save a level!"); } }
public static void SaveLevel(string p_levelDataFilePath, string p_levelIconFileName, byte[] p_levelData, byte[] p_levelMeta, int p_removedDuplicatesCount) { // info popup System.Action <string> infoPopup = (string p_infoText) => { if (uMyGUI_PopupManager.Instance != null) { if (p_removedDuplicatesCount > 0) { p_infoText += "\n'" + p_removedDuplicatesCount + "' duplicate object(s) removed before saving\n(duplicate = same: object, position, rotation, scale)."; } ((uMyGUI_PopupText)uMyGUI_PopupManager.Instance.ShowPopup(POPUP_TEXT)).SetText("Level Saved", p_infoText).ShowButton("ok"); } }; // save level icon as png LE_SaveLoad.LevelMetaData loadedMeta = LE_SaveLoad.LoadLevelMetaFromByteArray(p_levelMeta, true); if (loadedMeta.Icon != null) { Texture2D downScaledIcon = LE_FileSelectionHelpers.DownscaleTexture(loadedMeta.Icon, 255, 128); // save to file string path = Path.Combine(Application.persistentDataPath, p_levelIconFileName); // need to use own implementation of SaveToFile to keep backwards compatibility with MRLE v1.31 (had no UtilityPlatformIO.SaveToFile(..., bytes[])) LE_FileSelectionHelpers.SaveToFile(path, downScaledIcon.EncodeToPNG()); Object.Destroy(loadedMeta.Icon); Object.Destroy(downScaledIcon); } // save level infoPopup(ExampleGame_LoadSave.SaveByFilePath(p_levelDataFilePath, p_levelData, p_levelMeta)); }
// load level delayed by a few frames so that the loading message is rendered first private IEnumerator LateLoad(LE_LoadEvent p_args, byte[][] p_data) { // wait a few frames until the loading diolog is rendered and start loading the level yield return(new WaitForSeconds(0.3f)); // close loading popup (the rest of the loading mechanics will happen in this frame) uMyGUI_PopupManager.Instance.HidePopup(uMyGUI_PopupManager.POPUP_LOADING); // while the level is stored in a different file on all platforms except the webplayer, the webplayer can load a level created with the // full level editor example or the dungeon game example from the clipboard. The code below will identify such loading operations bool isLevelNotSupported = false; #if UNITY_5_3_OR_NEWER if (SceneManager.GetActiveScene().name == "LE_ExampleEditorTerrainOnly") #else if (Application.loadedLevelName == "LE_ExampleEditorTerrainOnly") #endif { LE_TerrainTextureConfig confTerrain = FindObjectOfType <LE_ConfigTerrain>().TerrainTextureConfig; LE_SaveLoadDataPeek peekLevelData = LE_SaveLoad.PeekLevelDataFromByteArray(p_data[0], confTerrain.TERRAIN_TEXTURES, confTerrain.TERRAIN_TEXTURE_SIZES, confTerrain.TERRAIN_TEXTURE_OFFSETS); isLevelNotSupported = // the terrain only level has no create terrain UI, because it has a default terrain, therefore all saved levels must have a terrain peekLevelData.TerrainDataPreview == null || // in the terrain only example the terrain must have the width and length of 500 to fit into the level peekLevelData.TerrainDataPreview.size.x != 500 || peekLevelData.TerrainDataPreview.size.z != 500 || // additionally, crashes are possible if a 9 patch terrain is used with different heightmap resolutions (see 'TT_Terrain9Patch.CrashCheck') peekLevelData.TerrainDataPreview.heightmapResolution != 257 || // besides, there is no object editing supported in the terrain only example peekLevelData.LevelObjectsCount > 0; Destroy(peekLevelData.TerrainDataPreview); if (isLevelNotSupported) { ((uMyGUI_PopupText)uMyGUI_PopupManager.Instance.ShowPopup(uMyGUI_PopupManager.POPUP_TEXT)).SetText( "Loading Failed", "It seems that you have a level saved in your clipboard that comes from the full " + "level editor example or from the dungeon game example. However, this is a terrain only editor example that cannot load such levels...").ShowButton("ok"); } } if (!isLevelNotSupported) { // load level p_args.LoadLevelDataFromBytesCallback(p_data[0]); p_args.LoadLevelMetaFromBytesCallback(p_data[1]); // wait until the old level is really destroyed yield return(new WaitForEndOfFrame()); // look at player GameObject player = GameObject.Find("Objects/PlayerStartPosition"); if (player != null) { Camera.main.transform.LookAt(player.transform.position, Vector3.up); } // just loaded a level -> it has no changes m_lastSaveFrame = Time.frameCount + 1; // plus one because LE_LevelEditorMain.LastChangeFrame is set to this frame } }
private void Start() { ExampleGame_LoadSave.Init(); // load level LE_ExtensionInterface.Load.Delegate(this, (byte[][] p_levelData) => { if (p_levelData != null && p_levelData.Length > 0 && p_levelData[0] != null) { // load level data (we do not need p_levelData[1], since it contains only meta data for example the level icon) // however, you might want to load it as well when you add other information to it for example the level time LE_SaveLoadData level = LE_SaveLoad.LoadLevelDataFromByteArray( p_levelData[0], TERRAIN_LAYER, TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURES, TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURE_SIZES, TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURE_OFFSETS); // call this function to destroy level editing scripts and improve performance LE_SaveLoad.DisableLevelEditing(level); } else { Debug.LogError("ExampleGame_Game: No saved level found!"); } // hide loading popup shown after calling LE_ExtensionInterface.Load uMyGUI_PopupManager.Instance.HidePopup(uMyGUI_PopupManager.POPUP_LOADING); // find player start position /* GameObject goPlayerStart = GameObject.Find("Objects/PlayerStartPosition"); * if (goPlayerStart != null) * { * PLAYER.transform.position = goPlayerStart.transform.position + goPlayerStart.transform.up; * Destroy(goPlayerStart); // not needed any more * } * else * { * Debug.LogError("ExampleGame_Game: could not find a PlayerStartPosition GameObject!"); * } */ }, true); }
// LOGIC -------------------------------------------------------------------------------------------------------------------------- /// <summary> /// Call to load a level into the level editor. Use the callbacks in the returned event args to start loading from byte arrays. /// Use if loading is needed without using the load button. Learn more: /// http://www.freebord-game.com/index.php/multiplatform-runtime-level-editor/documentation/load /// </summary> public LE_LoadEvent GetLoadEvent() { LE_LoadEvent loadEventArgs = new LE_LoadEvent((byte[] p_savedLevelData) => // LoadLevelDataFromBytes callback { Vector3 removedOffset = new Vector3(99999f, -99999f, 99999f); // clean up level (remove all LE_Objects) LE_Object[] objs = Object.FindObjectsOfType <LE_Object>(); for (int i = 0; i < objs.Length; i++) { objs[i].transform.position += removedOffset; Object.Destroy(objs[i].gameObject); } // load level data LE_SaveLoadData level = LE_SaveLoad.LoadLevelDataFromByteArray(p_savedLevelData, m_GUI3dTerrain != null?m_GUI3dTerrain.TERRAIN_LAYER:0, m_configTextures, m_configTextureSizes, m_configTextureOffsets); // process all level objects as if they were new for (int i = 0; i < level.LevelObjects.Length; i++) { LE_SaveLoadData.ObjectData obj = level.LevelObjects[i]; if (obj.Result == LE_SaveLoadData.ObjectData.EResult.INSTANCE) { // add snapping if needed if (m_GUI3dObject != null) { LE_LogicObjects.AddSnappingScripts(m_GUI3dObject, obj.Instance); } } else if (obj.Result == LE_SaveLoadData.ObjectData.EResult.STREAMED) { // add snapping if needed once spawned LS_ManagedObjectBase managedObj = LS_LevelStreamingSceneManager.Instance.GetManagedObject(obj.StreamedLevelObjectID); if (managedObj != null) { // add snapping if needed if (m_GUI3dObject != null) { managedObj.m_onShow += (object p_object, System.EventArgs p_args) => { if (m_GUI3dObject != null && p_object is LS_ManagedObjectInstantiateDestroy) { LE_LogicObjects.AddSnappingScripts(m_GUI3dObject, ((LS_ManagedObjectInstantiateDestroy)p_object).Instance.GetComponent <LE_Object>()); } }; } } } } // inform listeners that the level is now fully loaded if (LE_EventInterface.OnLoadedLevelInEditor != null) { LE_EventInterface.OnLoadedLevelInEditor(this, System.EventArgs.Empty); } }, (byte[] p_savedLevelMeta) => // LoadLevelMetaFromBytes callback { // load level meta LE_SaveLoad.LevelMetaData meta = LE_SaveLoad.LoadLevelMetaFromByteArray(p_savedLevelMeta, true); m_levelIcon = meta.Icon; if (LE_GUIInterface.Instance.delegates.SetLevelIcon != null) { LE_GUIInterface.Instance.delegates.SetLevelIcon(meta.Icon); } else if (meta.Icon != null) { Debug.LogError("LE_LogicLevel: GetLoadEvent: LE_LoadEvent: LoadLevelMetaFromBytes: you level meta seems to contain an icon, but you have not provided the LE_GUIInterface.delegates.SetLevelIcon delegate. Level icon will not be shown!"); } }); return(loadEventArgs); }