场景转换、加载进度和转换效果

发布于:2024-12-18 ⋅ 阅读:(61) ⋅ 点赞:(0)

游戏中转换场景

从游戏开始到游戏结束需要各种条件的场景转换

实现

建UI/button,这个组件自带事件触发
创建场景转换类

using UnityEngine;
using UnityEngine.SceneManagement;

public class ButtonController : MonoBehaviour
{
    public void LoadLevelString(string levelName)
    {
        
        FadeCanvas.fader.FaderLoadString(levelName);
       
    }
    public void LoadLevelInt(int levelIndex)
    {
        SceneManager.LoadScene(levelIndex);
    }

    public void RestartLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

将脚本及函数设置在buton的事件中。

转换效果和加载进度条

设置新的canva
将UIscalemode
reference resolution设置为1920 1080
在这里插入图片描述
设置子UI/image 设置为黑色 并添加canvas group组件
在这里插入图片描述
设置加载进度条,背景形状填充。
主要控制的是填充的图片
在这里插入图片描述

(最好使用脚本对ui的进行属性控制,以节省性能)
在场景转换的效果运行时,需要屏蔽其他操作

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

public class FadeCanvas : MonoBehaviour
{
    public static FadeCanvas fader;

    [SerializeField] private CanvasGroup canvasGroup;
    [SerializeField] private float changeValue;
    [SerializeField] private GameObject loadingScreen;
    [SerializeField] private Image loadingBar;
    [SerializeField] private float waitTime;
    [SerializeField] private bool fadeStarted = false;
    // Start is called before the first frame update

    private void Awake()
    {
        if (fader == null)
        {
            fader = this;
            DontDestroyOnLoad(gameObject);
        }
        else
            Destroy(gameObject);
    }
    void Start()
    {
        StartCoroutine(FadeIn());
    }

    public void FaderLoadString(string levelName)
    {
        StartCoroutine(FadeOutString(levelName));
    }

    IEnumerator FadeIn()
    {
        loadingScreen.SetActive(false);
        fadeStarted = false;
        while(canvasGroup.alpha > 0)
        {
            if (fadeStarted)
                yield break;
            canvasGroup.alpha -= changeValue;   
            yield return new WaitForSeconds(waitTime);
        }
    }

    IEnumerator FadeOutString(string levelIndex)
    {
        if (canvasGroup.alpha != 0)
            yield break;
        if (fadeStarted)
            yield break;
        fadeStarted = true;
        while (canvasGroup.alpha < 1)
        {
            canvasGroup.alpha += changeValue;
            yield return new WaitForSeconds(waitTime);
        }
        AsyncOperation ao = SceneManager.LoadSceneAsync(levelIndex);
        ao.allowSceneActivation = false;
        loadingScreen.SetActive(true);
        loadingBar.fillAmount = 0;
        while(ao.isDone == false)
        {
            loadingBar.fillAmount = ao.progress / 0.9f;
            if(ao.progress == 0.9f)
            {
                ao.allowSceneActivation = true;
            }
            yield return null;
        }
        StartCoroutine(FadeIn());
    }
}

这个脚本用到了单例设计模式,在场景转换时,避免被销毁
场景加载器中获取加载信息
进度加载由async.progress属性中获取。当属性值为0.9时就表示后台加载成功,但是资源并未显示出来,具体属性就是allowsceneactiviate。当这个属性值为true时,内容才会允许加载。


网站公告

今日签到

点亮在社区的每一天
去签到