二十七.加载关卡

加载关卡

1.LevelSelectPlayer

添加参数

private bool levelLoading;  //是否正加载场景

Update()中的判断,加入条件,加载场景不能移动

//防止胡乱移动  加载场景时不能移动
if (Vector3.Distance(transform.position, currentPoint.transform.position) < 0.1f && !levelLoading)

判断之后加入跳转关卡信息

//跳转关卡
if (currentPoint.isLevel)
{
    if(Input.GetButtonDown("Jump"))
    {
        levelLoading = true;
        //...
    }
}

2.MapPoint

加入参数,表示待加载的关卡

public string levelToLoad;  //待加载的关卡

在关卡节点,分别设置levelToLoad的值,指定对应的加载的场景名

请添加图片描述

3.新建空节点LSManager

节点用来管理关卡选择

再新建c#脚本LSManager.cs,绑定至节点上

引入头文件

using UnityEngine.SceneManagement;

添加参数,获取当前Player

public LevelSelectPlayer thePlayer;

添加跳转场景代码

public void LoadLevel()
{
    StartCoroutine(LoadLevelCo());
}

public IEnumerator LoadLevelCo()
{
    yield return new WaitForSeconds(1f);

    SceneManager.LoadScene(thePlayer.currentPoint.levelToLoad);
}

将Player拖到对应的thePlayer

请添加图片描述

4.完善LevelSelectPlayer跳转场景

添加参数,指向LSManager

public LSManager theManager;    //关卡选择管理

将LSManager节点拖到对应的theManager上

请添加图片描述

完善加载场景代码

//跳转关卡
if (currentPoint.isLevel && currentPoint.levelToLoad != "")
{
    if(Input.GetButtonDown("Jump"))
    {
        levelLoading = true;
        //加载场景
        theManager.LoadLevel();
    }
}

5.加入淡入淡出效果

添加UI->Canvas组件到场景

在Canvas下,新建Panel,设置属性

请添加图片描述

新建关卡选择UI管理脚本LSUIController,添加组件到Canvas下绑定

添加头文件UI的引用

using UnityEngine.UI;

添加淡入淡出代码(与UIController相似)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LSUIController : MonoBehaviour
{
    public static LSUIController sInstance;

    //淡入淡出
    public Image fadeScreen;
    public float fadeSpeed;
    private bool shouldFadeToBlack;
    private bool shouldFadeFromBlack;

    private void Awake()
    {
        sInstance = this;
    }
    // Start is called before the first frame update
    void Start()
    {
        //开始时添加淡入效果
        FadeFromBlack();
    }

    // Update is called once per frame
    void Update()
    {
        if (shouldFadeToBlack)
        {
            fadeScreen.color = new Color(fadeScreen.color.r, fadeScreen.color.g, fadeScreen.color.b, Mathf.MoveTowards(fadeScreen.color.a, 1f, fadeSpeed * Time.deltaTime));
            if (fadeScreen.color.a == 1f)
            {
                shouldFadeToBlack = false;
            }
        }
        if (shouldFadeFromBlack)
        {
            fadeScreen.color = new Color(fadeScreen.color.r, fadeScreen.color.g, fadeScreen.color.b, Mathf.MoveTowards(fadeScreen.color.a, 0f, fadeSpeed * Time.deltaTime));
            if (fadeScreen.color.a == 0f)
            {
                shouldFadeFromBlack = false;
            }
        }
    }

    public void FadeToBlack()
    {
        shouldFadeToBlack = true;
        shouldFadeFromBlack = false;
    }
    public void FadeFromBlack()
    {
        shouldFadeFromBlack = true;
        shouldFadeToBlack = false;
    }
}

修改Panel为FadeScreen,拖入到对应的FadeScreen,并设置FadeSpeed值

请添加图片描述

最后,修改LSManager中加载场景,加入淡入

public IEnumerator LoadLevelCo()
{
    LSUIController.sInstance.FadeToBlack();

    yield return new WaitForSeconds(1f / LSUIController.sInstance.fadeSpeed + 0.25f);

    SceneManager.LoadScene(thePlayer.currentPoint.levelToLoad);
}
### 关于Unreal Engine中的异步加载关卡 在Unreal Engine中,异步加载关卡是一个常见的需求,尤其是在开发大型开放世界游戏时。通过异步加载技术,可以显著减少玩家等待时间并优化资源管理。 #### 使用`UGameplayStatics::LoadStreamLevel`实现异步加载 可以通过调用 `UGameplayStatics::LoadStreamLevel` 函数来实现异步加载关卡的功能[^1]。此函数允许开发者指定要加载关卡名称以及是否以同步或异步方式执行操作。以下是其基本语法: ```cpp bool UGameplayStatics::LoadStreamLevel( UObject* WorldContextObject, FName LevelName, bool bMakeVisibleAfterLoad, bool bShouldBlockOnLoad, float StreamLevelTimeSeconds); ``` 参数说明如下: - **WorldContextObject**: 提供上下文的世界对象。 - **LevelName**: 要加载关卡名称。 - **bMakeVisibleAfterLoad**: 加载完成后是否立即显示该关卡。 - **bShouldBlockOnLoad**: 是否阻塞主线程直到完成加载(设置为false即可启用异步模式)。 - **StreamLevelTimeSeconds**: 延迟加载的时间间隔(可选,默认为0秒)。 当 `bShouldBlockOnLoad` 设置为 false 时,将触发后台线程上的异步加载过程。 #### 利用蓝图节点简化流程 对于不熟悉C++编程的游戏设计师来说,也可以利用虚幻编辑器内的蓝图系统轻松达成相同效果。具体而言,在事件图表(Event Graph)里拖拽出“Load Stream Level”节点,并按照提示填写必要字段即可启动目标地图区域的数据读取工作流[^2]。 另外值得注意的是,为了提高用户体验度与流畅感,在实际项目应用过程中往往还需要考虑诸如进度条展示、预加载提示音效播放之类的辅助功能设计。 ```cpp // 示例代码片段:开启一个新的子级联场景 if (!UGameplayStatics::LoadStreamLevel(this, TEXT("MySublevel"), true, false)) { UE_LOG(LogTemp, Warning, TEXT("Failed to load sub-level asynchronously.")); } ``` 以上就是关于如何运用Unreal Engine来进行高效能且无延迟感知体验下的动态内容引入的一些方法论探讨。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值