Load Level

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;

// Register for the load event, which is called when the level is loaded

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)
{

    // 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

To load a level into the level editor you have to call the 'LE_LevelEditorMain.GetLoadEvent' of the current instance. using LE_LevelEditor;

// 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_LevelEditorMain.

LE_LevelEditorMain lvlEditor = FindObjectOfType<LE_LevelEditorMain>();

// You can either check with 'lvlEditor.IsReady' if the level editor is initialized (currently after the first update loop)
// or simply add a callback like below.

lvlEditor.ExecuteWhenReady(()=>
{

    // Now that we know that the editor is initialized a load event can be acquired from it.
    // Execute the callbacks of the acquired event in order to load the level into the editor

    lvlEditor.GetLoadEvent().LoadLevelDataFromBytesCallback(dataAsByteArray);
    lvlEditor.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

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;

// 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_SaveLoadData level = LE_SaveLoad.LoadLevelDataFromByteArray(
    dataAsByteArray,

    // pass '0' to put the terrain in the 'Default' layer. Something like LayerMask.NameToLayer("Terrain") is also possible

    0,
    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);


// 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