public class LoadSceneManager : MonoSingleton<LoadSceneManager>
{
readonly List<string> buildinScenes = new List<string>()
{
"Login",
"Loading",
};
Dictionary<string, AssetBundle> assetBundles = new Dictionary<string, AssetBundle>();
bool sceneLoading = false;
public void LoadSceneAsync(string name, LoadSceneMode mode, LuaFunction func)
{
if (!sceneLoading)
{
StartCoroutine(LoadSceneCoroutine(name, mode, func));
}
}
IEnumerator LoadSceneCoroutine(string name, LoadSceneMode mode, LuaFunction func)
{
#if UNITY_EDITOR
if (HotUpdateConfig.DEBUG_MODE)
{
bool containsScene = false;
UnityEditor.EditorBuildSettingsScene[] scenes = UnityEditor.EditorBuildSettings.scenes;
for (int i = 0; i < scenes.Length; i++)
{
UnityEditor.EditorBuildSettingsScene scene = scenes[i];
if (System.IO.Path.GetFileNameWithoutExtension(scene.path) == name)
{
containsScene = true;
break;
}
}
if (!containsScene)
{
string guestPath = "Assets/Art/04Scene/Level/" + name + "/" + name + ".unity";
if (File.Exists(guestPath))
{
List<UnityEditor.EditorBuildSettingsScene> newScenes = new List<UnityEditor.EditorBuildSettingsScene>();
newScenes.AddRange(scenes);
newScenes.Add(new UnityEditor.EditorBuildSettingsScene(guestPath, true));
UnityEditor.EditorBuildSettings.scenes = newScenes.ToArray();
}
}
}
#endif
Scene current = SceneManager.GetActiveScene();
AssetBundle assetBundle = null;
if (assetBundles.TryGetValue(current.name, out assetBundle))
{
//卸载当前场景
assetBundle.Unload(true);
assetBundles.Remove(current.name);
}
if (buildinScenes.Contains(name) || HotUpdateConfig.DEBUG_MODE)
{
yield return LoadLocalSceneCoroutine(name, mode, func);
}
else
{
string abName = AssetBundleMapper.Instance.GetABNameByAssetName(name);
if (string.IsNullOrEmpty(abName))
{
yield return LoadLocalSceneCoroutine(name, mode, func);
}
else
{
string[] dependencies = LoadPrefabManager.Instance.GetAssetBundleManifest().GetAllDependencies(abName);
int loadedCount = 0;
for (int i = 0; i < dependencies.Length; i++)
{
yield return LoadPrefabManager.Instance.LoadSingleAssetBundle(dependencies[i],
(ab) =>
{ loadedCount++; });
}
while (loadedCount != dependencies.Length)
{
yield return null;
}
string fullPath = AssetBundleMapper.Instance.GetAssetBundleFullPath(abName);
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(fullPath);
sceneLoading = true;
while (!request.isDone)
{
func?.Call((uint)((request.progress * 100) * 0.98));
yield return null;
}
func?.Call(98);
assetBundles[name] = request.assetBundle;
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(name, mode);
yield return asyncOperation;
func?.Call(100);
}
}
sceneLoading = false;
}
IEnumerator LoadLocalSceneCoroutine(string name, LoadSceneMode mode, LuaFunction func)
{
var mAsync = SceneManager.LoadSceneAsync(name, mode);
sceneLoading = true;
mAsync.allowSceneActivation = false;
while (mAsync.progress < 0.9f)
{
func?.Call((uint)(mAsync.progress * 100));
yield return null;
}
func?.Call(100);
mAsync.allowSceneActivation = true;
}
}
加载场景思路
猜你喜欢
转载自blog.csdn.net/Momo_Da/article/details/105131600
今日推荐
周排行