public IEnumerator SetUpNewSceneFromSelf()

in IndoorSceneSynthesis/ConstraintStochasticIndoorSceneGeneration/Custom/CustomScripts/CSceneBuilderTool.cs [279:470]


    public IEnumerator SetUpNewSceneFromSelf(bool setRandomSeed = false, int objectRandomSeed = -1, bool deleteUnsuccessful = true)
    {
        //lock
        CSceneBuilderTool.isSampling = true;

        // var newScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
        var originalScene = EditorSceneManager.GetActiveScene();
        string originalScenePath = originalScene.path;
        string saveSceneFolder = "Assets/Custom/BuildScenes/";

        int sceneIndex = globalRandomSeed; //(int)UnityEngine.Random.Range(0f, 100000f);
        if (setRandomSeed)
        {
            sceneIndex = (int)UnityEngine.Random.Range(0f, 100000f);
        }

        string saveScenePath = saveSceneFolder + jsonSceneTitle + "_" + sceneIndex.ToString() + ".unity";

        if (objectRandomSeed > 0) {
            saveScenePath = saveSceneFolder + objectRandomSeed.ToString() + ".unity";
        }

        EditorSceneManager.SaveScene(originalScene, saveScenePath, true);
        EditorSceneManager.OpenScene(saveScenePath);

        //GameObject dalibao = Instantiate((GameObject)AssetDatabase.LoadAssetAtPath("Assets/Custom/SceneRandomizer/Dalibao.prefab", typeof(GameObject)));

        //set furniture
        CFurniturePlacerTool furniturePlacerTool = GameObject.Find("FurniturePlacer").GetComponent<CFurniturePlacerTool>();
        //furniturePlacerTool.randomSeed = (int)UnityEngine.Random.Range(0f, 10000f);
        CFurniturePool furnturePool = furniturePlacerTool.gameObject.GetComponent<CFurniturePool>();
        furnturePool.randomSeed = (int)UnityEngine.Random.Range(0f, 10000f);
        furnturePool.roomType = CSceneBuilderTool.samplingRoomType;

        furniturePlacerTool.SetFloorAndScaleDoor();
        if (CSceneBuilderTool.samplingRoomType == CRoomType.Kitchen || CSceneBuilderTool.samplingRoomType == CRoomType.Bathroom) {
            //additional process for Kitchen
            CKitchenPlacerTool kitchenPlacerTool = GameObject.Find("KitchenObjSet").GetComponent<CKitchenPlacerTool>();
            kitchenPlacerTool.ReNameKitchenSceneObjs();
        }
        furniturePlacerTool.LoadFurniturePrefab();

        furniturePlacerTool.CustomPlaceFurniture();
        while (!CSceneBuilderTool.furniturePlacingFinish) {
            yield return new EditorWaitForSeconds(0.2f);
        }

        Debug.Log("CSceneBuilderTool.furniturePlacingFinish" + CSceneBuilderTool.furniturePlacingSuccessful);

        if (CSceneBuilderTool.furniturePlacingSuccessful)
        {
            Debug.Log("Place Furniture Successful!");

            //rename cabinet,drawer,shelf
            CKitchenPlacerTool kitchenPlacerTool = GameObject.Find("KitchenObjSet").GetComponent<CKitchenPlacerTool>();
            kitchenPlacerTool.ReNameKitchenSceneObjs();


            CObjectPlacerTool objectPlacerTool = GameObject.Find("ObjectPlacer").GetComponent<CObjectPlacerTool>();
            objectPlacerTool.randomSeed = (int)UnityEngine.Random.Range(0f, 1000f);

            CObjectPool objectPool = objectPlacerTool.gameObject.GetComponent<CObjectPool>();
            objectPool.randomSeed = (int)UnityEngine.Random.Range(0f, 1000f);
            if (objectRandomSeed >= 0) {
                objectPool.randomSeed = objectRandomSeed;
            }
            objectPool.roomType = CSceneBuilderTool.samplingRoomType;

            objectPlacerTool.LoadObjectPrefab();

            objectPlacerTool.CustomPlaceObject();
            while (!CSceneBuilderTool.objectPlacingFinish) {
                yield return new EditorWaitForSeconds(0.2f);
            }

            //yield return new EditorWaitForSeconds(10);

            if (CSceneBuilderTool.objectPlacingSuccessful)
            {
                Debug.Log("Place Objects Successful!");

                Debug.Log("Debuging scene tools");
                //return false;

                //Set decoration
                //CDecorationPlacerTool decorationPlacertool = GameObject.Find("DecorationPlacer").GetComponent<CDecorationPlacerTool>();
                //decorationPlacertool.GetComponent<CFurniturePool>().randomSeed = (int)UnityEngine.Random.Range(0f, 1000f);

                //decorationPlacertool.SetFloor();
                //decorationPlacertool.LoadRule();
                //decorationPlacertool.CustomPlaceDecoration();
                

                //Debug.Log("Place Decoration Successful!");

                //Restore door size
                furniturePlacerTool.RestoreDoorSize();

                //Set celling!!!
                StructureObject[] structObjs = Resources.FindObjectsOfTypeAll<StructureObject>();
                foreach (StructureObject structObj in structObjs) {
                    if (structObj.WhatIsMyStructureObjectTag == StructureObjectTag.Ceiling) {
                        structObj.gameObject.SetActive(false);
                    }
                }
                //

                //start game
                //EditorApplication.ExecuteMenuItem("Edit/Play");

                //EditorApplication.ExecuteMenuItem("Edit/Play");

                //add streaming tool
                //AddFMEStreamToCurrentScene();

                //set nav
                SetNavMeshNotWalkable();

                var agentController = FindObjectOfType<PhysicsRemoteFPSAgentController>();
                //var capsuleCollider = agentController.GetComponent<CapsuleCollider>();
                var navmeshAgent = agentController.GetComponent<NavMeshAgent>();
                navmeshAgent.enabled = true;
                // The Editor bake interface does not take with parameters and could not be modified as of 2018.3
                //var buildSettings = 
                new NavMeshBuildSettings()
                {
                    agentTypeID = navmeshAgent.agentTypeID,
                    agentRadius = 0.2f,
                    agentHeight = 1.8f,
                    agentSlope = 10,
                    agentClimb = 0.5f,
                    minRegionArea = 0.05f,
                    overrideVoxelSize = false,
                    overrideTileSize = false
                };

                UnityEditor.AI.NavMeshBuilder.BuildNavMesh();

                //save
                EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());

                //save and register
                //var currentScene = EditorSceneManager.GetActiveScene();
                //EditorSceneManager.SaveScene(currentScene, saveScenePath);

                List<EditorBuildSettingsScene> currentBuildingScenes = new List<EditorBuildSettingsScene>();
                foreach (EditorBuildSettingsScene buildScene in EditorBuildSettings.scenes)
                {
                    currentBuildingScenes.Add(buildScene);
                }
                currentBuildingScenes.Add(new EditorBuildSettingsScene(saveScenePath, true));
                EditorBuildSettings.scenes = currentBuildingScenes.ToArray();

                //successful = true;

            }
        }

        //destrop scene generating tool
        //DestroyImmediate(dalibao);

        if (!deleteUnsuccessful) {
            //save
            EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());
        }

        Debug.Log("go back to original scene: " + originalScenePath);
        EditorSceneManager.OpenScene(originalScenePath);

        CSceneBuilderTool.last_scene_successful = objectPlacingSuccessful && furniturePlacingSuccessful;

        if (deleteUnsuccessful) {
            if ((!CSceneBuilderTool.objectPlacingSuccessful) || (!CSceneBuilderTool.furniturePlacingSuccessful)) {
                Debug.Log("now deleting scene: " + Application.dataPath + saveScenePath.Substring(6));
                //File.Delete(Application.dataPath + saveScenePath.Substring(6));
                AssetDatabase.DeleteAsset(saveScenePath);
            }
        }
        Debug.Log("Successful one scene: " + objectPlacingSuccessful + " " + furniturePlacingSuccessful);


        //reset
        CSceneBuilderTool.objectPlacingSuccessful = false;
        CSceneBuilderTool.objectPlacingFinish = false;
        CSceneBuilderTool.furniturePlacingSuccessful = false;
        CSceneBuilderTool.furniturePlacingFinish = false;
        CSceneBuilderTool.isSampling = false; 



        //return successful;
    }