示例#1
0
        /// <summary>
        /// 加载场景。
        /// </summary>
        /// <param name="lightmap">场景名称</param>
        /// <param name="loaded">加载结束,参数为是否加载场景</param>
        private void LoadScene(MapData data, Action <Boolean> loaded, string sceneName, Action <int> process = null)
        {
            try
            {
                //场景文件读取完毕后处理
                Action sceneWasLoaded = () =>
                {
                    //if (MogoUIManager.Instance != null)
                    //    MogoUIManager.Instance.ReleaseMogoUI();
                    //场景文件加载完毕后处理
                    Action LevelWasLoaded = () =>
                    {
                        Debug.Log("LevelWasLoaded: " + sceneName);
                        //Debug.Log("LevelWasLoaded: " + sceneName);
                        //if (!String.IsNullOrEmpty(m_lastSceneResourceName))
                        //    AssetCacheMgr.ReleaseResource(m_lastSceneResourceName);
                        if (data.modelName.Count == 0)
                        {
                            if (loaded != null)
                            {
                                loaded(true);
                            }
                        }

                        Debug.Log("modelName Count: " + data.modelName.Count);
                        if (process != null)
                        {
                            process(20);
                        }
                        var models = data.modelName.Keys.ToList();

                        for (int i = 0; i < models.Count; i++)
                        {
                            var currentOrder = i;
                            Debug.Log(i + ":" + models[currentOrder]);
                            //加载场景模型
                            AssetCacheMgr.GetSceneInstance(models[currentOrder],
                                                           (pref, id, go) =>
                            {
                                go.name = Utils.GetFileNameWithoutExtention(models[currentOrder]);
                                if (data.modelName[models[currentOrder]])// && SystemSwitch.ReleaseMode
                                {
                                    StaticBatchingUtility.Combine(go as GameObject);
                                }
                                Debug.Log("sceneLoaded: " + go.name);
                                LoggerHelper.Debug("sceneLoaded: " + go.name);
                                Debug.Log("id=" + id);
                                //(go as GameObject).SetActive(false);
                                //m_sceneObjects.Add(go);
                                if (currentOrder == data.modelName.Count - 1)
                                {
                                    AssetCacheMgr.UnloadAssetbundles(models.ToArray());
                                    SwitchLightMapFog(data, loaded);
                                }
                            }, (progress) =>
                            {
                                float cur = 60 * ((progress + currentOrder) / models.Count) + 20;
                                if (process != null)
                                {
                                    process((int)(cur));
                                }
                            });
                        }
                        //if (process != null)
                        //	process(100);
                        //if (loaded != null)
                        //	loaded(true);
                        Driver.Instance.LevelWasLoaded = null;
                    };
                    if (sceneName != "10002_Login")
                    {
                        Driver.Instance.LevelWasLoaded = () =>
                        {
                            Driver.Instance.StartCoroutine(UnloadUnusedAssets(() =>
                            {
                                //Debug.LogError("UnloadUnusedAssets finish");
                                GC.Collect();
                                LevelWasLoaded();
                            }));
                        };

                        //Driver.Instance.LevelWasLoaded = LevelWasLoaded;
                        Application.LoadLevel(sceneName);
                    }
                    else
                    {
                        LevelWasLoaded();
                    }
                    Debug.Log("LoadLevel: " + sceneName);
                };

                if (SystemSwitch.ReleaseMode)
                {
                    if (process != null)
                    {
                        process(5);
                    }
                    Debug.Log("xx sceneName: " + m_lastSceneResourceName);
                    m_lastSceneResourceName = string.Concat(sceneName, ".unity");
                    AssetCacheMgr.GetResource(m_lastSceneResourceName,//加载场景
                                              (scene) =>
                    {
                        sceneWasLoaded();
                    },
                                              (progress) =>
                    {
                        float cur = 15 * progress + 5;
                        if (process != null)
                        {
                            process((int)(cur));
                        }
                    }
                                              );
                    //if (process != null)
                    //    process(40);
                }
                else
                {
                    sceneWasLoaded();
                }
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }
        }