Load Level
概要
有三种可能的使用情况来加载一个关卡。
·编辑按钮使用情况:当用户点击加载按钮时,可以在编辑器中加载一个关卡。要在编辑器中加载游戏,您必须注册LE_EventInterface.OnLoad事件,该事件在关卡编辑器中单击加载按钮时触发。注册LE_EventInterface.OnLoad事件。这个事件将在关卡被加载时被调用。请记住,当脚本被销毁时,您也应该注销事件,否则可能会发生内存泄漏。
using LE_LevelEditor.Events;
// Register for the load event, which is called when the level is loaded
LE_EventInterface.OnLoad += OnLoad;
下面的事件处理程序将执行LE_LoadEvent的事件参数的回调以向编辑器提供必须加载的关卡的字节数组。
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
}
·不使用编辑按钮情况:但是,在某些情况下,您可能需要在编辑器中加载一个关卡,而无需用户交互。例如,当用户从编辑器开始一个关卡时,然后现在回到关卡编辑器场景。用户会希望看到他已经开始的关卡,因为他可能想要在游戏测试会话后后更改某些内容。在这种情况下,您必须调用LE_LevelEditorMain.GetLoadEvent方法才能获得在LE_EventInterface.OnLoad事件中相同的事件参数。要将关卡加载到关卡编辑器中,您必须调用当前实例的LE_LevelEditorMain.GetLoadEvent函数。
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();
// 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
});
·游戏使用情况:在前两种使用情况中,该关卡被加载用于编辑,但是您可能也想要打开关卡进行游戏。
在游戏中加载一个关卡更简单。您必须调用LE_SaveLoad.LoadLevelDataFromByteArray方法来加载关卡。如果您必须加载关卡元数据(例如获得当前关卡的金牌得分),那么您应该看看本文(链接)的第3步。
下面的代码将使用LE_SaveLoad.LoadLevelDataFromByteArray方法加载要进行游戏的关卡。它将传递由Unity编辑器监视面板提供的关卡的数据字节数组和定义在地形纹理配置中的一些值。最后,调用LE_SaveLoad.DisableLevelEditing。该函数将销毁LE_Object 脚本的所有实例,以确保该关卡不能被编辑。
地形纹理配置实例是必需的,这样才能正确加载地形。关卡文件不包含任何Asset,因此也没有地形纹理。关卡文件只存储在纹理配置中定义的纹理的id。因此,需要使用相同的纹理配置来进行关卡加载和关卡保存。
在这个例子中,只有关卡数据字节数组被加载。元数据字节数组未加载,因为在默认情况下,它仅包含元数据,例如关卡图标,关卡进行游戏并不需要它。但是,如果您添加了一些进行这个关卡的游戏需要的元数据,您还需要加载并处理它(参见这个链接的第三步)。
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
原文链接:http://www.freebord-game.com/index.php/multiplatform-runtime-level-editor/documentation/load