首页 > 其他分享 >03.基础框架场景和资源加载模块

03.基础框架场景和资源加载模块

时间:2023-01-02 22:04:51浏览次数:78  
标签:03 false allowSceneActivation 模块 using progress operation true 加载


​​​https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html​


03.基础框架场景和资源加载模块_自动跳转

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

public class SceneMgr : BaseManager<SceneMgr>
{

private IEnumerator cEnumerator;
public void LoadSceneAsync(string sceneName,UnityAction fun)
{
MonoMgr.GetInstance.StartCoroutine(LoadScene(sceneName, fun));
}

// AsyncOperation的progress(0-1)属性在isDone为false时,
// 最大加载到0.9就会暂停,直到isDone为true时才会继续加载0.9-1.0的这10%,
// 而isDone为true或fasle的标志为是: 当allowSceneActivation = false,isDone为false ,
// allowSceneActivation = false 的作用是让场景不会在加载完成后自动跳转到下一个场景,
// 当allowSceneActivation = true,isDone才可以为true,直到progress = 1.0时 isDone = true

// 1、AsyncOperation 在你不主动设置AllowSceneActivation = false的情况下,
// isDown会随着progress的增长(从0增长到1)自动变成true,所以会自动跳转场景;

//2、如果你不想让场景进行自动跳转,你就需要在异步加载时设置allowSceneActivation = false,
//这会使场景不进行自动跳转,原因是isDown在allowSceneActivation = false 的情况下 永远不会为true,
//并且此时的aysncOperation.progress也最多可以加载到0.9(90%),
//当你允许你的场景跳转时(设置allowSceneActivation = true),
//progress加载最后的10%,直到progress = 1, isDown此时自动变为true,异步加载完成,实现场景跳转。

//private AsyncOperation operation = null;
IEnumerator LoadScene(string sceneName, UnityAction fun)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneName);
//阻止当加载完成自动切换
operation.allowSceneActivation = false;
while (!operation.isDone)
{
//Debug.Log(operation.progress*1.0f);
if (operation.progress >= 0.9f)
{
operation.allowSceneActivation = true;
}
yield return operation.progress;
}
// Debug.Log(operation.progress); //0.9
fun?.Invoke();
}

}

//改进正确的

IEnumerator LoadScene(string sceneName)
{
yield return null;
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneName);
//阻止当加载完成自动切换
operation.allowSceneActivation = false;
while (!operation.isDone)
{
Debug.Log(operation.progress*100);
if (operation.progress >= 0.9f)
{
operation.allowSceneActivation = true;
}
yield return null;
}
Debug.Log(operation.progress+":完成");
}



03.基础框架场景和资源加载模块_加载_02


03.基础框架场景和资源加载模块_自动跳转_03


03.基础框架场景和资源加载模块_System_04

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class ResMgr :BaseManager<ResMgr>
{

public T Load<T>(string path) where T:Object
{
T t= Resources.Load<T>(path);
if (t is GameObject)
{
return UnityEngine.Object.Instantiate(t);
}
else
{
return t;
}
}

public void LoadAsync<T>(string path ,UnityAction<T> callBack ) where T:Object
{
MonoMgr.GetInstance.StartCoroutine(LoadRes<T>(path,callBack));
}

IEnumerator LoadRes<T>(string path, UnityAction<T> callBack) where T : Object
{
ResourceRequest request= Resources.LoadAsync<T>(path);
yield return request;
if (request.asset is GameObject)
{
callBack?.Invoke(Object.Instantiate(request.asset) as T);
}
else
{
callBack?.Invoke(request.asset as T);
}
}
}


标签:03,false,allowSceneActivation,模块,using,progress,operation,true,加载
From: https://blog.51cto.com/u_12756070/5984210

相关文章