Exemplo n.º 1
0
        //========================================================================================

        #region private methods

        /// <summary>
        /// Runtime Coroutine that loads a subScene
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        private IEnumerator LoadSubSceneCR(SubSceneJob job)
        {
            isJobRunning = true;
            //Debug.LogFormat("Load Job started: {0} {1} {2}", job.Region.SuperRegion.Type, job.Region.name, job.SceneType);

            string sceneName = WorldUtility.GetSubSceneName(job.Region.Id, job.SubSceneMode, job.SubSceneType);
            var subSceneRoot = job.Region.GetSubSceneRoot(job.SubSceneType);

            //editor subScenes are loaded (no streaming)
            if (editorSubScenesLoaded)
            {
                if (subSceneRoot)
                {
                    subSceneRoot.gameObject.SetActive(true);
                }
            }
            //streaming
            else
            {
                if (subSceneRoot)
                {
                    Debug.LogWarningFormat("Load Job for existing subScene started! region=\"{0}\", subScene=\"{1}\"", job.Region.name, job.SubSceneMode.ToString());
                }
                else if (!Application.CanStreamedLevelBeLoaded(sceneName))
                {
                    Debug.LogWarningFormat("scene {0} cannot be streamed", sceneName);
                    var root = new GameObject("empty").transform;
                    root.SetParent(job.Region.transform);
                    job.SubSceneRoot = root;
                }
                else
                {
                    AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);

                    while (!async.isDone)
                    {
                        yield return null;
                    }

                    Scene scene = SceneManager.GetSceneByName(sceneName);
                    var root = scene.GetRootGameObjects()[0].transform;
                    SceneManager.MoveGameObjectToScene(root.gameObject, gameObject.scene);
                    root.SetParent(job.Region.transform, false);
                    job.SubSceneRoot = root;

                    async = SceneManager.UnloadSceneAsync(sceneName);

                    while (!async.isDone)
                    {
                        yield return null;
                    }
                }
            }

            job.IsJobSuccessful = true;
            job.Callback(job);

            //Debug.Log("Load Job done");
            isJobRunning = false;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Runtime Coroutine that unloads a subScene.
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        private IEnumerator UnloadSubSceneCR(SubSceneJob job)
        {
            isJobRunning = true;
            //Debug.LogFormat("Unload Job started: {0} {1} {2}", job.Region.SuperRegion.Type, job.Region.name, job.SceneType);

            var subSceneRoot = job.Region.GetSubSceneRoot(job.SubSceneType, job.SubSceneMode);

            //editor subScenes are loaded (no streaming)
            if (editorSubScenesLoaded)
            {
                if (subSceneRoot)
                {
                    subSceneRoot.gameObject.SetActive(false);
                }
            }
            //streaming
            else
            {
                if (subSceneRoot)
                {
                    Destroy(subSceneRoot.gameObject);
                }
            }

            //if (job.SubSceneRoot != null)
            //{
            //Destroy(job.SubSceneRoot.gameObject);

            //UnityEngine.SceneManagement.Scene scene = UnityEngine.SceneManagement.SceneManager.CreateScene("destroyer");
            //var root = job.SubSceneRoot;
            //root.SetParent(null, true);
            //UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(root.gameObject, scene);

            //AsyncOperation async = UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync(scene);

            //while (!async.isDone)
            //{
            //    yield return null;
            //}                
            //}

            yield return null;
            //Resources.UnloadUnusedAssets();

            job.IsJobSuccessful = true;
            job.Callback(job);

            //Debug.Log("Unload Job done");
            isJobRunning = false;
        }
Exemplo n.º 3
0
        private void OnSubSceneJobDone(SubSceneJob subSceneJob)
        {
            if (subSceneJob.SubSceneMode != currentSubSceneMode)
            {
                return;
            }

            int index = (int)subSceneJob.SubSceneMode;

            if (subSceneJob.JobType == eSubSceneJobType.Load)
            {
                if (subSceneStates[index] == eSubSceneState.Loading)
                {
                    subSceneStates[index] = eSubSceneState.Loaded;
                }
            }
            else if (subSceneJob.JobType == eSubSceneJobType.Unload)
            {
                if (subSceneStates[index] == eSubSceneState.Unloading)
                {
                    subSceneStates[index] = eSubSceneState.Unloaded;
                }
            }
        }