场景切换
泰课指路牌:https://www.taikr.com/course/1062/task/31006/show.
在研发一款游戏的时候,会涉及到很多个场景的跳转,且在场景跳转的时候很多东西是通过读取配置表去动态生成的相应的玩家场景,下一个游戏场景的相关信息就由上一个场景转换的时候传递过来。场景切换模块提供给外部一个场景切换的接口。
1 相关函数
LoadLevel |
Loads the level by its name or index. 加载场景,加载之前你需要把场景在Build Settings添加,场景加载后,场景中的激活物体会调用MonoBehavior:OnLevelWasLoaded().使用该方法的时候,之前场景中的物体将会被直接删除。 |
LoadLevelAdditive |
Loads a level additively.该方法不销毁当前场景中的物体,新场景中的物体将会被添加进来,这个方法在一些连续加载的场景中非常有用哈。 |
LoadLevelAdditiveAsync |
Loads the level additively and asynchronously in the background.在后台异步记载场景unity会在后台线程中加载所有的场景资源,这就允许你在加载新场景过程中播放一个进度条或者通过异步加载为玩家创造一个流程的世界该方法会返回AsyncOperation结构,结构中isDone表示是否完成,progress给出当前的播放进度。注意的是在编辑器中后台线程的性能要低于游戏中。 |
LoadLevelAsync |
Loads the level asynchronously in the background.在后台异步记载场景unity会在后台线程中加载所有的场景资源,这就允许你在加载新场景过程中播放一个进度条或者通过异步加载为玩家创造一个流程的世界该方法会返回AsyncOperation结构,结构中isDone表示是否完成,progress给出当前的播放进度。注意的是在编辑器中后台线程的性能要低于游戏中。 |
相关API
CreateScene (创建场景) |
Create an empty new Scene at runtime with the given name. |
GetActiveScene(得到当前激活的场景) |
Gets the currently active Scene. |
GetSceneAt(根据index得到一个场景) |
Get the Scene at index in the SceneManager’s list of loaded Scenes. |
GetSceneByBuildIndex(通过BuildIndex得到一个场景) |
Get a Scene struct from a build index. |
GetSceneByName(通过名字得到一个场景) |
Searches through the Scenes loaded for a Scene with the given name. |
GetSceneByPath(通过路径得到一个场景) |
Searches all Scenes loaded for a Scene that has the given asset path. |
LoadScene(加载场景) |
Loads the Scene by its name or index in Build Settings. |
LoadSceneAsync(异步加载场景) |
Loads the Scene asynchronously in the background. |
MergeScenes( 合并两个场景) |
This will merge the source Scene into the destinationScene. |
MoveGameObjectToScene(把某个游戏物体移动到场景当中) |
Move a GameObject from its current Scene to a new Scene. |
SetActiveScene (激活某个场景) |
Set the Scene to be active. |
UnloadSceneAsync( 异步卸载某个场景) |
Destroys all GameObjects associated with the given Scene and removes the Scene from the SceneManager. |
sceneCount |
当前加载的场景总数。 |
sceneCountInBuildSettings |
Build Settings 中的场景数量。 |
activeSceneChanged |
订阅此事件可在活动场景发生变化时收到通知。 |
sceneLoaded |
向此事件添加委托,以在加载场景时收到通知。 |
sceneUnloaded |
向此事件添加委托以在卸载场景时收到通知。 |
是否销毁:
LoadLevel和LoadLevelAsync,在加载完成后之后将会立刻销毁原先场景中的物体,而LoadLevelAdditive和LoadLevelAdditiveAsync加载后将会保留原先的场景中的物体,这种方式可以实现无缝融合的场景,只需要你在适当的位置加载后面的场景,不过你还是要考虑资源的释放问题。
同步还是异步
异步加载能够获得加载过程的进度和是否加载完成,通过这种方式你可以在切换中增减进度条或者其他表现,这种方式一般通过协程完成
代码如下(示例):
//异步加载
public void LoadSceneAsync(string sceneName,Action Func)
{
MonoMgr.getInstance().StartCoroutine(LoadingSceneAsync(sceneName, Func));
}
public IEnumerator LoadingSceneAsync(string sceneName,Action Func)
{
AsyncOperation AO = SceneManager.LoadSceneAsync(sceneName);
//判定场景是否加载完成,通过协程实现加载进度的更新,可用于其他事件,外部也可以监听
while (!AO.isDone)
{
EventManager.getInstance().EventTrigger("LoadSceneProgress", AO);
yield return AO.progress;
}
yield return AO;
Func();
}
同步加载:
public void LoadScene(string sceneName,Action Func)
{
SceneManager.LoadScene(sceneName);
Func();
}
完整代码:
public class SceneLoadManager : SignleBaseManager<SceneLoadManager>
{
//同步加载
public void LoadScene(string sceneName,Action Func)
{
SceneManager.LoadScene(sceneName);
Func();
}
//异步加载
public void LoadSceneAsync(string sceneName,Action Func)
{
MonoMgr.getInstance().StartCoroutine(LoadingSceneAsync(sceneName, Func));
}
public IEnumerator LoadingSceneAsync(string sceneName,Action Func)
{
AsyncOperation AO = SceneManager.LoadSceneAsync(sceneName);
//判定场景是否加载完成,通过协程实现加载进度的更新,可用于其他事件,外部也可以监听
while (!AO.isDone)
{
EventManager.getInstance().EventTrigger("LoadSceneProgress", AO);
yield return AO.progress;
}
yield return AO;
Func();
}
}
模块的使用:
public class TestSceneLoad : MonoBehaviour
{
private bool IsLoadScene = false;
private void Start()
{
EventManager.getInstance().AddEventListener("LoadSceneProgress", LoadProgress);
}
private void LoadProgress(object loadProgressInfo)
{
Debug.Log($"场景加载进度-------{(loadProgressInfo as AsyncOperation).progress}");
}
void Update()
{
if (Input.GetMouseButtonDown(0) && !IsLoadScene)
{
SceneLoadManager.getInstance().LoadSceneAsync("TestLoadScene", Func);
IsLoadScene = true;
}
}
private void Func()
{
Debug.Log("场景加载完成-------");
}
}
相关链接
Unity程序基础框架__单例基类模块
Unity程序基础框架__缓存池模块
Unity程序基础框架__事件中心模块
Unity程序基础框架__公共Mono模块
Unity程序基础框架__场景切换模块
Unity程序基础框架__资源加载模块
Unity程序基础框架__输入控制模块
Unity程序基础框架__事件中心模块基类优化
Unity程序基础框架__音效管理模块
Unity程序基础框架__UI管理模块