Unity—Loading制作--异步加载SceneManager.LoadSceneAsync

        功能:场景加载方法
*****************************************************/
public class ResSvc : MonoBehaviour {

//定义一个Action 来持续更新进度 
  private Action prgCB = null;
 
 //异步加载方法  传入两个参数  一个是场景名字, 一个是一个事件,或者方法。
 //Action loaded可以直接填null
    public void AsyncLoadScene(string sceneName, Action loaded) {
        //进度条点的位置计算方法
        GameRoot.Instance.loadingWnd.SetWndState();
        
        AsyncOperation sceneAsync = SceneManager.LoadSceneAsync(sceneName);
        //来穆特表达式
        prgCB = () => {
            //得到当前的进度值
            float val = sceneAsync.progress;
            //跟新进度条方法 
            GameRoot.Instance.loadingWnd.SetProgress(val);
            //进度值Val为1 就代表加载完成了
            if (val == 1) {
                //判断下是否传了参数Action
                if (loaded != null) {
                    loaded();
                }
                //设置为空,否则会一直在Update里执行
                prgCB = null;
                sceneAsync = null;
                //关闭Loading界面  
                GameRoot.Instance.loadingWnd.SetWndState(false);
            }
        };
    }

    private void Update() {
        if (prgCB != null) {
            prgCB();
        }
    }
    
    
    }
    
    
        功能:加载进度界面
*****************************************************/

using UnityEngine;
using UnityEngine.UI;

public class LoadingWnd : WindowRoot {
    public Text txtTips;
    public Image imgFG;
    public Image imgPoint;
    public Text txtPrg;

    private float fgWidth;
    
    //初始化进度条
    protected override void InitWnd() {
        base.InitWnd();

        fgWidth = imgFG.GetComponent<RectTransform>().sizeDelta.x;

        SetText(txtTips, "这是一条游戏Tips");
        SetText(txtPrg, "0%");
        imgFG.fillAmount = 0;
        imgPoint.transform.localPosition = new Vector3(-545f, 0, 0);
    }

//更新进度条 和进度条上面的点
    public void SetProgress(float prg) {
        SetText(txtPrg, (int)(prg * 100) + "%");
        imgFG.fillAmount = prg;

        float posX = prg * fgWidth - 545;
        imgPoint.GetComponent<RectTransform>().anchoredPosition = new Vector2(posX, 0);
    }
}

————————————————————————————————————————————————————————————————————————————————

                 最后直接调用就可以了
                 
 resSvc.AsyncLoadScene("场景名字",Jzzh)
 //或者
 resSvc.AsyncLoadScene("场景名字",null)
 
 //加载完成之后执行的逻辑
 public void Jzzh(float prg) {
       Debug.log("加载完成之后执行的逻辑")
    }
 
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容