找回密码
 立即注册
查看: 306|回复: 0

unity 预加载场景

[复制链接]
发表于 2022-6-2 12:42 | 显示全部楼层 |阅读模式
unity 预加载场景


思路:

1.创建 加载场景异步函数字典用于存放加载场景的异步变量

2.当加载场景或者预加载的时候字典里记录异步函数

3.当加载场景时候如果异步变量有的话,自动切换场景切换为true.如果已经异步已经为true,就直接切换场景,如果没有的话需要异步加载,并且自动切换场景为true

4.异步加载场景时候只需要将异步变量自动切换场景为false即可
<hr>
代码:

[quote]
using System;


using System.Collections.Generic;

using DJ;

using Sirenix.Utilities;

using UnityEngine;

using UnityEngine.SceneManagement;

namespace ET

{

public class LoadSceneComponentUpdateSystem:UpdateSystem

{

public override void Update(LoadSceneComponent self)

{

}

}

public class LoadSceneComponentAwakeSystem:AwakeSystem

{

public override void Awake(LoadSceneComponent self)

{

self.preLoadSceneDict =new Dictionary();

}

}

public class LoadSceneComponentDestroySystem:DestroySystem

{

public override void Destroy(LoadSceneComponent self)

{

}

}

public static class LoadSceneComponentSystem

  {

///

/// 加载场景

///

///

///

    public static void LoadScene(this LoadSceneComponent self,string sceneName)

{

#if UNITY_EDITOR

UnityEditor.SceneManagement.EditorSceneManager.LoadSceneInPlayMode($"Assets/Bundles/Map/{sceneName}/{sceneName}.unity",

new LoadSceneParameters(LoadSceneMode.Single,LocalPhysicsMode.None));

#else

    SceneManager.LoadScene(sceneName);

#endif

    }

///

/// 异步加载场景

///

///

///

///

///

    public static AsyncOperation LoadSceneAsync(this LoadSceneComponent self,string sceneName,

LoadSceneMode loadSceneMode =LoadSceneMode.Single)

{

self.preLoadSceneDict.TryGetValue(sceneName,out var loadMapOperation);

if (loadMapOperation !=null)

{

loadMapOperation.allowSceneActivation =true;

return loadMapOperation;

}

#if UNITY_EDITOR

loadMapOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(

$"Assets/Bundles/Map/{sceneName}/{sceneName}.unity",

new LoadSceneParameters(loadSceneMode,LocalPhysicsMode.None));

#else

       loadMapOperation = SceneManager.LoadSceneAsync(sceneName, new LoadSceneParameters(loadSceneMode, LocalPhysicsMode.None));

#endif

      self.preLoadSceneDict.Add(sceneName, loadMapOperation);

return loadMapOperation;

}

///

/// 预加载场景

///

///

///

///

///

    public static AsyncOperation PreloadSceneAsync(this LoadSceneComponent self,string sceneName,

LoadSceneMode loadSceneMode =LoadSceneMode.Single)

{

self.preLoadSceneDict.TryGetValue(sceneName,out var loadMapOperation);

if (loadMapOperation !=null)

{

return loadMapOperation;

}

#if UNITY_EDITOR

loadMapOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(

$"Assets/Bundles/Map/{sceneName}/{sceneName}.unity",

new LoadSceneParameters(loadSceneMode,LocalPhysicsMode.None));

#else

    loadMapOperation = SceneManager.LoadSceneAsync(sceneName, new LoadSceneParameters(loadSceneMode, LocalPhysicsMode.None));

#endif

      loadMapOperation.allowSceneActivation =false;

self.preLoadSceneDict.Add(sceneName, loadMapOperation);

return loadMapOperation;

}

///

/// 获取场景异步操作

///

///

///
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-16 04:41 , Processed in 0.088999 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表