예제 #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;
        }
예제 #2
0
        //========================================================================================

#if UNITY_EDITOR
        private void Update()
        {
            if (Application.isPlaying)
            {
                return;
            }

            string subSceneName = WorldUtility.GetSubSceneRootName(subSceneMode, subSceneType);
            if (name != subSceneName)
            {
                name = subSceneName;
            }
        }
예제 #3
0
        void IRegionEventHandler.CreateSubScene(eSubSceneMode subSceneMode, eSubSceneType subSceneType)
        {
            string subScenePath = WorldUtility.GetSubScenePath(gameObject.scene.path, Id, subSceneMode, subSceneType);
            string subScenePathFull = WorldUtility.GetFullPath(subScenePath);

            if (GetSubSceneRoot(subSceneType) != null)
            {
                return;
            }
            else if (System.IO.File.Exists(subScenePathFull))
            {
                Debug.LogFormat("SubScene \"{0}\" already exists but is not loaded!", subScenePath);
                return;
            }
            else
            {
                var rootGO = new GameObject(WorldUtility.GetSubSceneRootName(subSceneMode, subSceneType), typeof(SubScene));
                rootGO.GetComponent<SubScene>().Initialize(subSceneMode, subSceneType);

                var root = rootGO.transform;
                root.SetParent(transform, false);
            }
        }
예제 #4
0
        public override void OnInspectorGUI()
        {
            //
            List <SubScene> loadedSubScenes = self.GetAllSubScenes();

            //
            //base.OnInspectorGUI();

            EditorGUILayout.LabelField("----");

            boundsSizeProperty.vector3Value = EditorGUILayout.Vector3Field("Bounds Size", boundsSizeProperty.vector3Value);

            EditorGUILayout.LabelField("Render Distances");

            overrideRenderDistanceFarProperty.boolValue = EditorGUILayout.Toggle("  Override Far", overrideRenderDistanceFarProperty.boolValue);

            if (overrideRenderDistanceFarProperty.boolValue)
            {
                localRenderDistanceFarProperty.floatValue = EditorGUILayout.FloatField("  Far", localRenderDistanceFarProperty.floatValue);
            }

            overrideRenderDistanceInactiveProperty.boolValue = EditorGUILayout.Toggle("  Override Inactive", overrideRenderDistanceInactiveProperty.boolValue);

            if (overrideRenderDistanceInactiveProperty.boolValue)
            {
                localRenderDistanceInactiveProperty.floatValue = EditorGUILayout.FloatField("  Inactive", localRenderDistanceInactiveProperty.floatValue);
            }

            EditorGUILayout.LabelField("----");

            drawBoundsProperty.boolValue = EditorGUILayout.Toggle("Draw Bounds", drawBoundsProperty.boolValue);

            if (!Application.isPlaying && self.transform.parent.GetComponent <World>().EditorSubScenesLoaded)
            {
                GUILayout.Label("");

                foreach (var subSceneMode in self.AvailableSubSceneModes)
                {
                    foreach (var subSceneType in Enum.GetValues(typeof(eSubSceneType)).Cast <eSubSceneType>())
                    {
                        if (!self.GetSubSceneRoot(subSceneType, subSceneMode, loadedSubScenes) && GUILayout.Button("Create " + WorldUtility.GetSubSceneRootName(subSceneMode, subSceneType)))
                        {
                            UnityEngine.EventSystems.ExecuteEvents.Execute <IRegionEventHandler>(self.gameObject, null, (x, y) => x.CreateSubScene(subSceneMode, subSceneType));
                        }
                    }
                }

                GUILayout.Label("");
            }

            serializedObject.ApplyModifiedProperties();
        }
예제 #5
0
        void IWorldEventHandler.ExportSubScenes()
        {
            if (!editorSubScenesLoaded)
            {
                return;
            }

            //clear subScene folder
            ((IWorldEventHandler)this).ClearSubSceneFolder();

            //create folder, in case it does not exist
            string scenePath = gameObject.scene.path;
            string folderPath = scenePath.Remove(scenePath.LastIndexOf('.'));
            string parentFolderPath = folderPath.Remove(folderPath.LastIndexOf('/'));
            if (!UnityEditor.AssetDatabase.IsValidFolder(folderPath))
            {
                UnityEditor.AssetDatabase.CreateFolder(parentFolderPath, gameObject.scene.name);
            }

            //finding all the regions
            var regions = new List<RegionBase>();
            for (int i = 0; i < transform.childCount; i++)
            {
                var region = transform.GetChild(i).GetComponent<RegionBase>();

                if (region != null)
                {
                    regions.Add(region);
                }
            }

            foreach (var region in regions)
            {
                foreach (var subSceneType in Enum.GetValues(typeof(eSubSceneType)).Cast<eSubSceneType>())
                {
                    foreach (var subSceneMode in region.AvailableSubSceneModes)
                    {
                        var root = region.GetSubSceneRoot(subSceneType, subSceneMode);

                        if (!root || root.childCount == 0) //if root is null or empty there is no need to create a subScene
                        {
                            continue;
                        }

                        //paths
                        string subScenePath = WorldUtility.GetSubScenePath(gameObject.scene.path, region.Id, subSceneMode, subSceneType);
                        //string subScenePathFull = WorldUtility.GetFullPath(subScenePath);

                        //moving copy to subScene
                        root.SetParent(null, false);
                        var subScene = UnityEditor.SceneManagement.EditorSceneManager.NewScene(UnityEditor.SceneManagement.NewSceneSetup.EmptyScene, UnityEditor.SceneManagement.NewSceneMode.Additive);
                        UnityEditor.SceneManagement.EditorSceneManager.MoveGameObjectToScene(root.gameObject, subScene);

                        //saving and closing the sub scene
                        UnityEditor.SceneManagement.EditorSceneManager.SaveScene(subScene, subScenePath);
                        UnityEditor.SceneManagement.EditorSceneManager.CloseScene(subScene, true);

                        //add subScene to buildsettings
                        var buildSettingsScenes = UnityEditor.EditorBuildSettings.scenes.ToList();
                        buildSettingsScenes.Add(new UnityEditor.EditorBuildSettingsScene(subScenePath, true));
                        UnityEditor.EditorBuildSettings.scenes = buildSettingsScenes.ToArray();

                        // if (root.childCount != 0)
                        // {
                        //     foreach (var superRegionType in Enum.GetValues(typeof(eSuperRegionType)).Cast<eSuperRegionType>())
                        //     {
                        //         //duplicating
                        //         var rootCopyGO = Instantiate(root.gameObject, root.position, Quaternion.identity);
                        //         rootCopyGO.name = root.name;
                        //         rootCopyGO.transform.Translate(Vector3.Scale(worldSize, SUPERREGION_OFFSETS[superRegionType]));
                        //     }
                        // }
                    }
                }
            }

            UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(gameObject.scene);
            editorSubScenesLoaded = false;
        }
예제 #6
0
        void IWorldEventHandler.ImportSubScenes()
        {
            if (editorSubScenesLoaded)
            {
                return;
            }

            var regions = new List<RegionBase>();

            for (int i = 0; i < transform.childCount; i++)
            {
                var region = transform.GetChild(i).GetComponent<RegionBase>();

                if (region)
                {
                    regions.Add(region);
                }
            }

            foreach (var region in regions)
            {
                foreach (var subSceneType in Enum.GetValues(typeof(eSubSceneType)).Cast<eSubSceneType>())
                {
                    foreach (var subSceneMode in region.AvailableSubSceneModes)
                    {
                        if (region.GetSubSceneRoot(subSceneType, subSceneMode) != null)
                        {
                            Debug.LogErrorFormat("The \"{0}\" of Region \"{1}\" is already loaded!", WorldUtility.GetSubSceneRootName(subSceneMode, subSceneType), region.name);
                            continue;
                        }

                        //paths
                        string subScenePath = WorldUtility.GetSubScenePath(gameObject.scene.path, region.Id, subSceneMode, subSceneType);
                        string subScenePathFull = WorldUtility.GetFullPath(subScenePath);

                        Scene subScene = new Scene();

                        if (System.IO.File.Exists(subScenePathFull))
                        {
                            subScene = UnityEditor.SceneManagement.EditorSceneManager.OpenScene(subScenePath, UnityEditor.SceneManagement.OpenSceneMode.Additive);
                        }

                        //move subScene content to open world scene
                        if (subScene.IsValid())
                        {
                            var rootGO = subScene.GetRootGameObjects()[0];
                            UnityEditor.SceneManagement.EditorSceneManager.MoveGameObjectToScene(rootGO, this.gameObject.scene);

                            var root = rootGO.transform;
                            root.SetParent(region.transform, false);

                            if (!root.gameObject.activeSelf)
                            {
                                root.gameObject.SetActive(true);
                            }
                        }

                        //end: close subScene
                        UnityEditor.SceneManagement.EditorSceneManager.CloseScene(subScene, true);
                    }
                }
            }


            UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(gameObject.scene);
            editorSubScenesLoaded = true;
        }