异步缓存加载场景

/***

  • 异步加载场景
  • 本脚本挂载在空对象Manager上,把要加载的场景名称写入参数面板参数框中
  • 应用:在其他脚本中需要跳转场景的时候,代码:SceneManager.LoadScene();替换为:CacheSceneManager._instance.startLoad();

*/
using System;
using UnityEngine;
using UnityEngine.SceneManagement;

///


/// 此类提供异步缓存加载场景(目前仅支持缓存加载一个场景),注意:在unity编辑器里调试时,异步加载会阻塞主线程,打包后,异步加载不会阻塞主线程,
///

public class CacheSceneManager : MonoBehaviour
{

public static CacheSceneManager _instance;

/// 
/// 预加载场景名称
/// 
public string CacheSceneName;
private AsyncOperation async;
private bool cacheFinish = false;
private bool cacheStart = true;
private bool startLoadScene = false;
private long startLoadTime = 0;
private long cacheAfterSeconds = 2;//2后开始加载下一个场景,视当前场景大小可以更改几秒后加载下一个场景;

void Awake()
{
    //设置后台加载线程的优
    Application.backgroundLoadingPriority = ThreadPriority.BelowNormal;
    cacheFinish = false;
    _instance = this;
    if (CacheSceneName != null)
    {
        CacheSceneManager._instance.cacheScene(CacheSceneName);
    }
}

private void cacheScene(string name)
{
    CacheSceneName = name;
    cacheFinish = false;
    cacheStart = false;
    startLoadScene = false;
    startLoadTime = 0;
    async = null;
}

void Update()
{
    //在脚本加载完成后延迟2秒钟开始缓存,避免和主场景抢占资源,具体时间可根据主场景的大小以及实际情况来设置
    if (!cacheStart)
    {
        if (startLoadTime == 0)
        {
            startLoadTime = getCurrentTimeSeconds();
        }
        if ((getCurrentTimeSeconds() - startLoadTime) > cacheAfterSeconds)
        {
            cacheStart = true;
            Debug.Log(CacheSceneName + " start async cache---------------------->" + getCurrentTimeSeconds());
            async = SceneManager.LoadSceneAsync(CacheSceneName);
            async.allowSceneActivation = false;
        }
    }

    if (async != null)
    {
        //如果加载完成则更新标记
        if (async.progress == 0.9f && !cacheFinish)
        {
            cacheFinish = true;
            Debug.Log(CacheSceneName + " end async cache--------------------------> use Time:" + (getCurrentTimeSeconds() - startLoadTime - cacheAfterSeconds));
        }
        //加载完成后,检测到切换场景时,加载缓存场景
        if (async.progress == 0.9f && startLoadScene)
        {
            async.allowSceneActivation = true;
        }

    }
}
//需要加载缓存场景时调用 CacheSceneManager._instance.startLoad(); 
public void startLoad()
{
    startLoadScene = true;
}

public static long getCurrentTimeSeconds()
{
    return ConvertDateTimeToInt(System.DateTime.Now) / 1000;

}

public static long ConvertDateTimeToInt(System.DateTime time)
{
    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
    long t = (time.Ticks - startTime.Ticks) / 10000;
    return t;
}

}

你可能感兴趣的:(异步缓存加载场景)