예제 #1
0
    // 异步载入场景
    private static IEnumerator NonsyncLoad(string sceneName, ILoading loading)
    {
        App.log.Trace("SceneM.cs", "开始异步载入场景{0}", sceneName);



        // 取得载入器
        IScene scene = sceneLoadControl[sceneName];

        s_preScene      = s_CurIScene;
        s_loadingIScene = scene;

        // 1 播放开始载入的动画,并等待其播放完毕
        if (loading != null)
        {
            App.log.Trace("SceneM.cs", "播放载入开始动画");
            loading.FadeIn();
            while (loading.IsFadingIn())
            {
                yield return(null);
            }
        }

        // 欲载入的处理
        scene.PrepareLoad();


        // 2 产生事件并清理旧的场景
        if (eventUnload != null)
        {
            eventUnload(s_preScene);
        }
        if (s_preScene != null)
        {
            s_preScene.Clear();
        }



        // 3 载入新的场景
        scene.Load();

        if (sceneLoadingAnim.ContainsKey(sceneName))
        {
            sceneLoadingAnim[sceneName].SetAsyncLoading(scene.AsyncLoading);
        }

        if (sceneLoadControl.ContainsKey(CombatScene.GetSceneName()))
        {
            IScene sceneCombat = sceneLoadControl[CombatScene.GetSceneName()];
            // 清理内存资源
            if (scene != sceneCombat && s_preScene != sceneCombat)
            {
                Resources.UnloadUnusedAssets();
                System.GC.Collect();
            }
        }

        // 1 播放开始载入的动画,并等待其播放完毕
        if (loading != null)
        {
            App.log.Trace("SceneM.cs", "播放载入开始动画");
            loading.Load();
            while (loading.IsLoading())
            {
                yield return(null);
            }
        }
        yield return(new WaitForMSeconds(100f));


        // 等待载入完成
        while (!scene.IsEnd())
        {
            yield return(null);
        }

        // 场景载入完成(一定要在load完毕后才能替换,此时的环境才能称得上载入完成)
        s_CurIScene = scene;


        bLoading = false;

        // 6 发出载入完毕的事件
        if (eventLoaded != null)
        {
            eventLoaded(scene);
        }
        //载入完成。
        scene.BuildScene();
        scene.Start();

        // 4 播放载入完成的动画,并等待其播放完毕
        if (loading != null)
        {
            App.log.Trace("SceneM.cs", "播放载入完成动画");
            loading.FadeOut();
            while (loading.IsFadingOut())
            {
                yield return(null);
            }

            loading.TryDestroy();
        }

        // 载入完成
        App.log.Trace("SceneM.cs", "异步载入场景{0}完成", sceneName);
    }