117 lines
2.9 KiB
C#
117 lines
2.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Thousandto.Core.Asset;
|
|
using Thousandto.Core.Base;
|
|
using Thousandto.Update.Manager;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 场景预加载
|
|
/// </summary>
|
|
public class CacheSceneSystem
|
|
{
|
|
#region//私有变量
|
|
private bool _isLock = false;
|
|
private string _lockSceneName = string.Empty;
|
|
//已经缓存了的场景名字
|
|
private List<string> _cacheSceneNames = new List<string>();
|
|
#endregion
|
|
|
|
#region//常量
|
|
private const string CONST_STR_PREFS_KEY = "CacheSceneData";
|
|
#endregion
|
|
|
|
#region//公共函数
|
|
public void Initialize()
|
|
{
|
|
_isLock = false;
|
|
string value = string.Empty;
|
|
_cacheSceneNames.Clear();
|
|
value = PlayerPrefs.GetString(CONST_STR_PREFS_KEY, string.Empty);
|
|
if (string.IsNullOrEmpty(value))
|
|
return;
|
|
string[] array = value.Split('_');
|
|
if (array != null)
|
|
{
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
string name = array[i];
|
|
_cacheSceneNames.Add(name);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UnInitialize()
|
|
{
|
|
_isLock = false;
|
|
_lockSceneName = string.Empty;
|
|
_cacheSceneNames.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 预先缓存场景
|
|
/// </summary>
|
|
/// <param name="sceneName">场景名字</param>
|
|
/// <param name="action">缓存结束回调</param>
|
|
/// <param name="isUser">是否加载后使用该场景</param>
|
|
public void PreLoadSceneForCache(string sceneName, MyAction<bool> action = null)
|
|
{
|
|
if (_isLock)
|
|
return;
|
|
|
|
bool isLoading = false;
|
|
//判断是否有场景正在加载
|
|
if (isLoading)
|
|
{
|
|
action(false);
|
|
}
|
|
|
|
_isLock = true;
|
|
_lockSceneName = sceneName;
|
|
|
|
if (_cacheSceneNames.Contains(sceneName))
|
|
{
|
|
//该场景已经缓存过了 不用在load
|
|
CacheFinish();
|
|
}
|
|
else
|
|
{
|
|
SceneRootAssetsLoader.PreLoadSceneAssets(sceneName, CacheFinish);
|
|
}
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region//私有函数
|
|
//缓存完成
|
|
private void CacheFinish()
|
|
{
|
|
string value = string.Empty;
|
|
if(!_cacheSceneNames.Contains(_lockSceneName))
|
|
_cacheSceneNames.Add(_lockSceneName);
|
|
|
|
//保存设置
|
|
StringBuilder sb = new StringBuilder();
|
|
for (int i = 0; i < _cacheSceneNames.Count; i++)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
sb.Append(_cacheSceneNames[i]);
|
|
}
|
|
else
|
|
{
|
|
string str = string.Format("_{0}", _cacheSceneNames[i]);
|
|
sb.Append(str);
|
|
}
|
|
}
|
|
value = sb.ToString();
|
|
PlayerPrefs.SetString(CONST_STR_PREFS_KEY, value);
|
|
|
|
_lockSceneName = string.Empty;
|
|
_isLock = false;
|
|
}
|
|
#endregion
|
|
}
|