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