Add Additional Meta Data

Add Additional Meta Data

概要

您的关卡的元数据存储在' LevelMetaData '类中。它允许你使用' MetaData '属性来存储键值对的字符串。为了添加元数据,您的应用程序必须注册LE_EventInterface.OnCollectMetaDataBeforeSave事件。

第一步:事件注册

注册LE_EventInterface.OnCollectMetaDataBeforeSave事件。当关卡的元数据被保存时,这个事件将被调用。请记住,当脚本被销毁时,您也应该注销事件,否则可能会发生内存泄漏。

using LE_LevelEditor.Events;

// Register for the meta data collection event, which is called when the level is saved

LE_EventInterface.OnCollectMetaDataBeforeSave += OnCollectMetaDataBeforeSave;

第二步:事件执行

下面的事件处理程序会将地形宽度和长度添加到元数据中。此外,还添加了金币值。

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

}

第三步:加载关卡元数据

当你需要一个关卡的元数据时,你可以很容易地加载它。例如,地形大小可能需要用于关卡选择或者金币值用于关卡评分。下面的代码显示了如何从字节数组加载元数据(参见此链接)。

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

...

原文链接:http://www.freebord-game.com/index.php/multiplatform-runtime-level-editor/documentation/add-meta-data

你可能感兴趣的:(Add Additional Meta Data)