//Save all Rooms void SaveRoomLayouts() { //Find Prefab in Asset Menu for Creating in scene later GameObject lvManagerPrefab = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Objects/Level Manager.prefab", typeof(GameObject)); if (lvManagerPrefab == null) { //Error Finding Asset for level manager Debug.LogError("FAILED SAVING: Did not find levelManager prefab. Rename to \"Level Manager.prefab\" and move to \"Assets/Objects/\""); return; } //All the assets found in the Asset database with levelObj label List <string> roomsThatExist = new List <string>(AssetDatabase.FindAssets("l:LevelObj", new[] { "Assets/Levels/Level Objects" })); //Create an empty List for the Scriptable object that was found List <LevelObject> levelObjects = new List <LevelObject>(); //All the roomNames as a list List <string> roomNames = new List <string>(); //For every asset path found fill the empty list of scriptable objects with the found path foreach (string assetPathGUID in roomsThatExist) { //Asset path as string string assetPath = AssetDatabase.GUIDToAssetPath(assetPathGUID); //Load assetpath into a levelObject LevelObject newLevel = (LevelObject)AssetDatabase.LoadAssetAtPath(assetPath, typeof(LevelObject)); //if this is null it failed finding it and continue looking through asset paths if (newLevel == null) { continue; } //Add to list levelObjects.Add(newLevel); //add the name to the name list roomNames.Add(newLevel.name); } List <LevelObject> toDeleteList = new List <LevelObject>(); //For every room that is in the scene Save to editor foreach (Room room in rooms.Values) { //If doesn't exist in the already loaded assets create new asset and scriptable object instance if (!roomNames.Contains(room.name)) { //Create instance of a new Level LevelObject level = (LevelObject)ScriptableObject.CreateInstance(typeof(LevelObject)); //Set name level.thisScene = room.name; //Set Editor position level.worldEditorX = room.pos.x; level.worldEditorY = room.pos.y; //Create empty list of connections level.doorways = new List <SceneConnector>(); //For every Door in the room Add to empty connection list for (int d = 0; d < room.doors.Count; d++) { //If the room doesnt have a connection continue if (room.doors[d].connection != null) { //New Connection SceneConnector newConnction = new SceneConnector(); //Set room connection to the connected door name newConnction.connection = rooms[room.doors[d].connection.connection.roomNum].name; //Set side of connection newConnction.side = room.doors[d].doorSide; //Set other side of the connection newConnction.otherConnectionSide = room.doors[d].connection.connection.doorSide; //Add to list level.doorways.Add(newConnction); } } //Create Asset in the Asset database with room name AssetDatabase.CreateAsset(level, "Assets/Levels/Level Objects/" + room.name + ".asset"); //Label it so it can be found when saving again AssetDatabase.SetLabels(level, new[] { "LevelObj" }); //Create scene and load it behind the editor Scene newScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); //set Scene Name to room name newScene.name = room.name; //Instantiate previously found prefab GameObject levelManager = Instantiate(lvManagerPrefab); //Set the level to the currently saved level levelManager.GetComponent <LevelManagerScript>().thisLevel = level; //Initiate values for room scene levelManager.GetComponent <LevelManagerScript>().CreateRoomSpecs(); //Save scene to editor EditorSceneManager.SaveScene(newScene, "Assets/Levels/Level Scenes/" + room.name + ".unity"); //Save open scene EditorSceneManager.SaveOpenScenes(); } bool roomNotFound = true; //For every level that is already saved foreach (LevelObject level in levelObjects) { // If level doesn't exist continue if (level == null) { roomNotFound = true; continue; } //if this room is the same as the level save updated features if (room.name == level.name) { //Updated Positon level.worldEditorX = room.pos.x; level.worldEditorY = room.pos.y; Scene newScene = EditorSceneManager.OpenScene("Assets/Levels/Level Scenes/" + room.name + ".unity");; LevelManagerScript lvManager = GameObject.Find("Level Manager(Clone)").GetComponent <LevelManagerScript>(); lvManager.CreateRoomSpecs(); EditorSceneManager.SaveScene(newScene, "Assets/Levels/Level Scenes/" + room.name + ".unity"); //Create boolean for deleted bool didntFindDoorInLevel = false; //For every connection in the asset for (int r = 0; r < level.doorways.Count; r++) { //for every door in the room for (int d = 0; d < room.doors.Count; d++) { if (room.doors[d].connection == null) { // didntFindDoorInRoom = true; continue; } if (rooms[room.doors[d].connection.connection.roomNum].name == level.doorways[r].connection) { didntFindDoorInLevel = false; break; } if (rooms[room.doors[d].connection.connection.roomNum].name != level.doorways[r].connection) { didntFindDoorInLevel = true; continue; } didntFindDoorInLevel = false; } //Remove doorway in asset if the connection is gone if (didntFindDoorInLevel) { level.doorways.RemoveAt(r); } } //Add door to scriptable object that doesnt exists yet if (level.doorways.Count < rooms.Count) { //For every door that is in the room foreach (Door oldDoor in room.doors) { //Create bool bool notFound = false; //for every connection in the level foreach (SceneConnector levelObjectConnection in level.doorways) { //if connection already exists break else the room connection wasnt found if (levelObjectConnection.connection == rooms[oldDoor.connection.connection.roomNum].name) { notFound = false; break; } else { notFound = true; } } //not found add connection to asset if (notFound) { //Create new connection SceneConnector newConnction = new SceneConnector(); //Set connection to room name newConnction.connection = rooms[oldDoor.connection.connection.roomNum].name; //Set side newConnction.side = oldDoor.doorSide; //set other side newConnction.otherConnectionSide = oldDoor.connection.connection.doorSide; //Add connection to asset level.doorways.Add(newConnction); } } } //Did find the room roomNotFound = false; } else { //Didnt find room add to list roomNotFound = true; toDeleteList.Add(level); } } } //Check the rooms to see if they were accidently added to the should remove list foreach (Room room in rooms.Values) { if (toDeleteList.Count >= 1) { foreach (LevelObject deletable in toDeleteList.ToArray()) { if (deletable != null && room != null) { //If it exists remove from list if (deletable.name == room.name) { if (!toDeleteList.Remove(deletable)) { Debug.Log("Didn't Delete"); } } } else { Debug.Log("Didn't Delete"); } } } } //Delete every room asset and scene in the Asset Database foreach (LevelObject delete in toDeleteList) { if (delete == null) { continue; } string deleteName = delete.name; AssetDatabase.DeleteAsset("Assets/Levels/Level Objects/" + deleteName + ".asset"); AssetDatabase.DeleteAsset("Assets/Levels/Level Scenes/" + deleteName + ".unity"); } }
private void Awake() { instance = this; }