目录
简介
一、通过名称加载场景
二、通过指针加载场景
三、事件
四、示例
加载场景的重载函数如下:
/// <summary>
/// 异步加载场景
/// </summary>
/// <param name="sceneName">场景名称</param>
/// <param name="sceneActivationDelay">激活延迟时长</param>
/// <param name="loadSceneMode">场景加载方式</param>
/// <returns></returns>
public static SceneLoader LoadAsync(string sceneName, float sceneActivationDelay = 3f, LoadSceneMode loadSceneMode = LoadSceneMode.Single)
/// <summary>
/// 异步加载场景
/// </summary>
/// <param name="sceneBuildIndex">场景指针</param>
/// <param name="sceneActivationDelay">激活延迟时长</param>
/// <param name="loadSceneMode">场景加载方式</param>
/// <returns></returns>
public static SceneLoader LoadAsync(int sceneBuildIndex, float sceneActivationDelay = 3f, LoadSceneMode loadSceneMode = LoadSceneMode.Single)
其中sceneActivationDelay参数表示当场景在内存中加载完成时,需要延迟该时长才允许场景激活,原理是首先将异步操作AsyncOperation中的allowSceneActivation设为false,在场景加载完成并延迟后再将其设为true,该参数默认值为3。
loadSceneMode表示加载场景的方式,默认值为Single,表示覆盖当前场景,Additive则表示场景叠加到当前场景。
using UnityEngine;
using SK.Framework;public class Example : MonoBehaviour
{private void Start(){//加载名为Example的场景SceneLoader.LoadAsync("Example", 5f);}
}
using UnityEngine;
using SK.Framework;
using UnityEngine.SceneManagement;public class Example : MonoBehaviour
{private void Start(){//加载名为Example的场景SceneLoader.LoadAsync("Example", 0f, LoadSceneMode.Additive);}
}
using UnityEngine;
using SK.Framework;public class Example : MonoBehaviour
{private void Start(){//加载BuildIndex为1的场景SceneLoader.LoadAsync(1);}
}
场景加载事件包含开始事件、加载中事件、完成事件,通过如下方式设置:
using UnityEngine;
using SK.Framework;public class Example : MonoBehaviour
{private void Start(){//加载名为Example的场景SceneLoader.LoadAsync("Example", 5f).OnBegan(() => Debug.Log("开始加载")).OnLoading(progress => Debug.Log(string.Format("加载进度 {0}", progress))).OnCompleted(() => Debug.Log("加载完成"));}
}
using UnityEngine;
using SK.Framework;
using UnityEngine.UI;
using UnityEngine.SceneManagement;public class Example : MonoBehaviour
{//场景加载过渡界面[SerializeField] GameObject loadingView;//加载进度条[SerializeField] Image progress;//加载进度文本[SerializeField] Text progressText;private void Start(){//加载名为Example的场景SceneLoader.LoadAsync("Example", 5f, LoadSceneMode.Additive).OnBegan(() => loadingView.SetActive(true)).OnLoading(s => {progress.fillAmount = = string.Format("{0}%", Mathf.Round(s * 100));}).OnCompleted(() => loadingView.SetActive(false));}
}
效果:
本文发布于:2024-01-29 07:44:52,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170648549713770.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |