Documentation for Unity Asset Store version v1.20
Summary
There are three possible use cases for loading a level. A level can be loaded in the editor when the user clicks on the load button (
Editor Button use case). However, in some cases you might want to load a level in the editor without user interaction. For example when the user has started a level from the editor and now comes back to the level editor scene. The user will want to see the level that he has started, because he might want to change something after his play test session (
Editor No Button use case). In these first two use cases the level is loaded for editing, but you might also want to open the level for playing in your game (
Game use case).
Editor Button: to load the game in the editor you have to register for the '
LE_EventInterface.OnLoad' event, which is triggered when the load button is clicked in the level editor.
Editor No Button: in this case you will have to call the '
LE_LevelEditorMain.GetLoadEvent' method in order to get the same EventArgs like you would receive in the '
LE_EventInterface.OnLoad' event.
Game: loading a level in the game is even simpler. You have to call the '
LE_SaveLoad.LoadLevelDataFromByteArray' method to load the level. If you have to load the level meta data (e.g. to get the gold medal score of the current level) then you should take a look at Step 3 of this
article.
Editor Button
Register for the '
LE_EventInterface.OnLoad' event. This event will be called when the level is loaded. Keep in mind that you also should unregister the event handler when your script gets destroyed otherwise a memory leak could occur.
using LE_LevelEditor.Events;
LE_EventInterface.OnLoad += OnLoad;
The event handler below will execute the callback of the '
LE_LoadEvent' EventArgs to provide the editor with the byte arrays of the level that has to be loaded.
private void OnLoad(object p_sender, LE_LoadEvent p_args)
{
byte[] dataAsByteArray = ...;
byte[] metaAsByteArray = ...;
p_args.LoadLevelDataFromBytesCallback(dataAsByteArray);
p_args.LoadLevelMetaFromBytesCallback(metaAsByteArray);
}
Editor No Button
To load a level into the level editor you have to call the '
LE_LevelEditorMain.GetLoadEvent' of the current instance.
using LE_LevelEditor;
byte[] dataAsByteArray = ...;
byte[] metaAsByteArray = ...;
LE_LevelEditorMain lvlEditor = FindObjectOfType<LE_LevelEditorMain>();
lvlEditor.ExecuteWhenReady(()=>
{
lvlEditor.GetLoadEvent().LoadLevelDataFromBytesCallback(dataAsByteArray);
lvlEditor.GetLoadEvent().LoadLevelMetaFromBytesCallback(metaAsByteArray);
});
Game
The code below will load a level for playing using the '
LE_SaveLoad.LoadLevelDataFromByteArray' method. It will pass the data byte array of the level and some values defined in the terrain texture config, which is provided through the Unity editor inspector. Finally, '
LE_SaveLoad.DisableLevelEditing' is called. This function will destroy all instances of the '
LE_Object' script to ensure that the level cannot be edited.
The terrain texture config instance is required in order to load the terrain correctly. The level files do not contain any assets and therfore also no terrain textures. The level file only stores an id of the texture as it is defined in the texture config. Hence, the same texture config needs to be used for level loading and level saving.
In this example only the level data byte array is loaded. The meta data byte array is not loaded, since in the default case it contains only meta data for example the level icon, which is not required to play the level. However, if you have added some meta data that is required to play the game you will also have to load and process it (see Step 3 of
Add Additional Meta Data).
using LE_LevelEditor.Core;
[SerializeField]
private LE_TerrainTextureConfig TERRAIN_TEXTURE_CONFIG = null;
byte[] dataAsByteArray = ...;
LE_SaveLoadData level = LE_SaveLoad.LoadLevelDataFromByteArray(
dataAsByteArray,
0,
TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURES,
TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURE_SIZES,
TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURE_OFFSETS);
LE_SaveLoad.DisableLevelEditing(level);
Documentation for Unity Asset Store version v1.10
Summary
There are three possible use cases for loading a level. A level can be loaded in the editor when the user clicks on the load button (
Editor Button use case). However, in some cases you might want to load a level in the editor without user interaction. For example when the user has started a level from the editor and now comes back to the level editor scene. The user will want to see the level that he has started, because he might want to change something after his play test session (
Editor No Button use case). In these first two use cases the level is loaded for editing, but you might also want to open the level for playing in your game (
Game use case).
Editor Button: to load the game in the editor you have to register for the '
LE_EventInterface.OnLoad' event, which is triggered when the load button is clicked in the level editor.
Editor No Button: in this case you will have to call the '
LE_GUIWindowRight.GetLoadEvent' method in order to get the same EventArgs like you would receive in the '
LE_EventInterface.OnLoad' event.
Game: loading a level in the game is even simpler. You have to call the '
LE_SaveLoad.LoadLevelDataFromByteArray' method to load the level. If you have to load the level meta data (e.g. to get the gold medal score of the current level) then you should take a look at Step 3 of this
article.
Editor Button
Register for the '
LE_EventInterface.OnLoad' event. This event will be called when the level is loaded. Keep in mind that you also should unregister the event handler when your script gets destroyed otherwise a memory leak could occur.
LE_EventInterface.OnLoad += OnLoad;
The event handler below will execute the callback of the '
LE_LoadEvent' EventArgs to provide the editor with the byte arrays of the level that has to be loaded.
private void OnLoad(object p_sender, LE_LoadEvent p_args)
{
byte[] dataAsByteArray = ...;
byte[] metaAsByteArray = ...;
p_args.LoadLevelDataFromBytesCallback(dataAsByteArray);
p_args.LoadLevelMetaFromBytesCallback(metaAsByteArray);
}
Editor No Button
To load a level into the level editor you have to call the '
LE_GUIWindowRight.GetLoadEvent' of the current instatiated '
LE_GUIWindowRight' instance.
byte[] dataAsByteArray = ...;
byte[] metaAsByteArray = ...;
FindObjectOfType<LE_GUILevelEditor>().RightWindow.GetLoadEvent().LoadLevelDataFromBytesCallback(dataAsByteArray);
FindObjectOfType<LE_GUILevelEditor>().RightWindow.GetLoadEvent().LoadLevelMetaFromBytesCallback(metaAsByteArray);
Game
The code below will load a level for playing using the '
LE_SaveLoad.LoadLevelDataFromByteArray' method. It will pass the data byte array of the level and some values defined in the terrain texture config, which is provided through the Unity editor inspector. Finally, '
LE_SaveLoad.DisableLevelEditing' is called. This function will destroy all instances of the '
LE_Object' script to ensure that the level cannot be edited.
The terrain texture config instance is required in order to load the terrain correctly. The level files do not contain any assets and therfore also no terrain textures. The level file only stores an id of the texture as it is defined in the texture config. Hence, the same texture config needs to be used for level loading and level saving.
In this example only the level data byte array is loaded. The meta data byte array is not loaded, since in the default case it contains only meta data for example the level icon, which is not required to play the level. However, if you have added some meta data that is required to play the game you will also have to load and process it (see Step 3 of
Add Additional Meta Data).
[SerializeField]
private LE_TerrainTextureConfig TERRAIN_TEXTURE_CONFIG = null;
byte[] dataAsByteArray = ...;
LE_SaveLoadData level = LE_SaveLoad.LoadLevelDataFromByteArray(
dataAsByteArray,
0,
TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURES,
TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURE_SIZES,
TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURE_OFFSETS);
LE_SaveLoad.DisableLevelEditing(level);
Documentation for Unity Asset Store version v1.01
Summary
There are three possible use cases for loading a level. A level can be loaded in the editor when the user clicks on the load button (
Editor Button use case). However, in some cases you might want to load a level in the editor without user interaction. For example when the user has started a level from the editor and now comes back to the level editor scene. The user will want to see the level that he has started, because he might want to change something after his play test session (
Editor No Button use case). In these first two use cases the level is loaded for editing, but you might also want to open the level for playing in your game (
Game use case).
Editor Button: to load the game in the editor you have to register for the '
LE_EventInterface.OnLoad' event, which is triggered when the load button is clicked in the level editor.
Editor No Button: in this case you will have to call the '
LE_GUIWindowRight.GetLoadEvent' method in order to get the same EventArgs like you would receive in the '
LE_EventInterface.OnLoad' event.
Game: loading a level in the game is even simpler. You have to call the '
LE_SaveLoad.LoadLevelDataFromByteArray' method to load the level. If you have to load the level meta data (e.g. to get the gold medal score of the current level) then you should take a look at Step 3 of this
article.
Editor Button: Step 1: Event Registration
Register for the '
LE_EventInterface.OnLoad' event. This event will be called when the level is loaded. Keep in mind that you also should unregister the event handler when your script gets destroyed otherwise a memory leak could occur.
// Register for the load event, which is called when the level is loaded
LE_EventInterface.OnLoad += OnLoad;
Editor Button: Step 2: Event Handling
The event handler below will execute the callback of the '
LE_LoadEvent' EventArgs to provide the editor with the byte arrays of the level that has to be loaded.
private void OnLoad(object p_sender, LE_LoadEvent p_args)
{
// You will probably load your level's data and meta data from a file here
byte[] dataAsByteArray = ...;
byte[] metaAsByteArray = ...;
// Execute the callbacks of the EventArgs in order to load the level into the editor
p_args.LoadLevelDataFromBytesCallback(dataAsByteArray);
p_args.LoadLevelMetaFromBytesCallback(metaAsByteArray);
// You could make some default operations on the level, since it is fully loaded now
// For example you could make the camera always look at the player
}
Editor No Button: Step 1: Load Level For Editing
To load a level into the level editor you have to call the '
LE_GUIWindowRight.GetLoadEvent' of the current instatiated '
LE_GUIWindowRight' instance.
// You will probably load your level's data and meta data from a file here
byte[] dataAsByteArray = ...;
byte[] metaAsByteArray = ...;
// Search for an instance of the LE_GUILevelEditor and access its right window instance.
// A load event can be acquired from the right window instance.
// Execute the callbacks of the acquired event in order to load the level into the editor
FindObjectOfType<LE_GUILevelEditor>().RightWindow.GetLoadEvent().LoadLevelDataFromBytesCallback(dataAsByteArray);
FindObjectOfType<LE_GUILevelEditor>().RightWindow.GetLoadEvent().LoadLevelMetaFromBytesCallback(metaAsByteArray);
// You could make some default operations on the level, since it is fully loaded now
// For example you could make the camera always look at the player
Game: Step 1: Load Level For Playing
The code below will load a level for playing using the '
LE_SaveLoad.LoadLevelDataFromByteArray' method. It will pass the data byte array of the level and some values defined in the terrain texture config, which is provided through the Unity editor inspector. Finally, all instances of the '
LE_Object' script will be destroyed. This way the level cannot be edited and performance is improved.
The terrain texture config instance is required in order to load the terrain correctly. The level files do not contain any assets and therfore also no terrain textures. The level file only stores an id of the texture as it is defined in the texture config. Hence, the same texture config needs to be used for level loading and level saving.
In this example only the level data byte array is loaded. The meta data byte array is not loaded, since in the default case it contains only meta data for example the level icon, which is not required to play the level. However, if you have added some meta data that is required to play the game you will also have to load and process it (see Step 3 of
Add Additional Meta Data).
// This serialized field should be defined in the top of this class. Its value should be
// assigned through the Unity editor's inspector.
[SerializeField]
private LE_TerrainTextureConfig TERRAIN_TEXTURE_CONFIG = null;
// You will probably load your level's data from a file here
byte[] dataAsByteArray = ...;
// Load the level from the byte array. Since there are no editor scripts in this scene the terrain
// texture configuration is not defined and needs to be passed to the LoadLevelDataFromByteArray method.
// In this example we expect TERRAIN_TEXTURE_CONFIG to be a serialized property of this class.
LE_SaveLoad.LoadedLevel level = LE_SaveLoad.LoadLevelDataFromByteArray(
dataAsByteArray,
TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURES,
TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURE_SIZES,
TERRAIN_TEXTURE_CONFIG.TERRAIN_TEXTURE_OFFSETS);
// destroy all level object scripts for performance optimization
// use the returned LoadedLevel instance to get the level objects
// it also contains the level file version and the terrain GameObject
for (int i = 0; i < level.LevelObjects.Length; i++)
{
Destroy(level.LevelObjects[i]);
}
// You could make some default operations on the level, since it is fully loaded now
// For example you could move the player to the start position and make the camera look at him