LoadLevel
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
});
·游戏使⽤情况:在前两种使⽤情况中,该关卡被加载⽤于编辑,但是您可能也想要打开关卡进⾏游戏。
register for在游戏中加载⼀个关卡更简单。您必须调⽤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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论