Add Additional Meta Data

Documentation for Unity Asset Store version v1.20

Summary

The meta data of your level is stored in the 'LevelMetaData' class. It allows you to store key-value pairs of strings using the 'MetaData' property. In order to add meta data your application must register for the 'LE_EventInterface.OnCollectMetaDataBeforeSave' event.

Step 1: Event Registration

Register for the 'LE_EventInterface.OnCollectMetaDataBeforeSave' event. This event will be called when the level's meta data is saved. 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 meta data collection event, which is called when the level is saved

LE_EventInterface.OnCollectMetaDataBeforeSave += OnCollectMetaDataBeforeSave;

Step 2: Event Handling

The event handler below will add the terrain width and length to the meta data. Additionally, a score value for the gold medal is added. private void OnCollectMetaDataBeforeSave(object p_sender, LE_CollectMetaDataEvent p_args)
{

    // Try to get the terrain size. Use fallback values if there is no terrain.

    int width = 0;
    int length = 0;
    if (Terrain.activeTerrain != null && Terrain.activeTerrain.terrainData != null)
    {
       width = (int)Terrain.activeTerrain.terrainData.size.x;
       length = (int)Terrain.activeTerrain.terrainData.size.z;
    }

    // Add collected terrain size values to level meta data

    p_args.LevelMetaData.Add("TerrainWidth", width.ToString());
    p_args.LevelMetaData.Add("TerrainLength", length.ToString());

    // Add a value for the gold medal score to level meta data

    p_args.LevelMetaData.Add("GoldScore", 123456);
}

Step 3: Load Level Meta Data

When you need the meta data of a level you can easily load it. In this example the terrain size might be needed for the level selection or the gold medal score for the level score evaluation. The code below shows how level meta data can be loaded from a byte array (see how to save a level). using LE_LevelEditor.Core;

// You will probably load your level's meta data from a file here

byte[] metaDataAsByteArray = ...;

// Generate a LevelMetaData instance from a byte array. Passing false as last parameter will
// disable level icon loading. You should do it if you do not need the level icon, because
// this will drastically reduce the loading time of the meta data. Pass true if you need the level icon

LE_SaveLoad.LevelMetaData metaData = LE_SaveLoad.LoadLevelMetaFromByteArray(metaDataAsByteArray, false);

// Get the values that you have stored in Step 2.

int width = 0;
if (metaData.MetaData.ContainsKey("TerrainWidth"))
{
    width = int.Parse(metaData.MetaData["TerrainWidth"]);
}
int length = 0;
if (metaData.MetaData.ContainsKey("TerrainLength"))
{
    length = int.Parse(metaData.MetaData["TerrainLength"]);
}
int goldScore = 0;
if (metaData.MetaData.ContainsKey("GoldScore"))
{
    goldScore = int.Parse(metaData.MetaData["GoldScore"]);
}

// Do something with your values

...