/// <summary> /// sets the saved values to the created scripts /// </summary> public void RestoreSavedComponentValues() { foreach (IRestorableGameObject restorableGameObject in RestoreableObjectTree.getValues()) { restorableGameObject.restoreComponentValues(); } }
/// <summary> /// creates the game objects with all its saved data and creates all saved scripts for /// each gameobject without saved values /// </summary> private void recreateObjectsAndScripts() { ///as the parent of the nonPrefabs is going to change, the process must be reversed so that all nonPrefabs ///can find their inGameObject without their hirachy sibling index path changed IEnumerable <ITreeNode <IRestorableGameObject> > treeNodes = RestoreableObjectTree.getReversedNodesWithValue(); ///when instantiating the objects, it is important that the process is in normal order, so the ///Enumerable is reversed again and saved in this list. List <ITreeNode <IRestorableGameObject> > nodes = new List <ITreeNode <IRestorableGameObject> >(); ///save all Transforms from nonSaveablePrefabs IList <Transform> allSavedNonPrefabs = new List <Transform>(); foreach (ITreeNode <IRestorableGameObject> treeNode in treeNodes) { ///get all objects the possibility to find themselve in the default scene state ///before the sibling indicies are restored (only the NonPrefabObjects uses it) treeNode.Value.prepareGameObjectReconstruction(allSavedNonPrefabs); nodes.Insert(0, treeNode); } ///set all parent to null so they wont be a child of a saved prefab, since all prefabs will ///be destroyed and so would the nonPrefabs as the childs of destroyed objects are ///destroyed aswell foreach (Transform t in allSavedNonPrefabs) { t.SetParent(null); ///the order of the objects is one of the most important things in the process of restoring the ///saved scene. all prefabs must be set as last prefabs so the parent hirachy of the root elements ///is not adulterated t.SetAsLastSibling(); } ///delete all obsolete objects from the default scene so the saved hirachy path adds up with the ///scene cleanGarbageHeap(); foreach (ITreeNode <IRestorableGameObject> treeNode in nodes) { ///create the saved objects and also hands its parent over, determined by the objects ///hirachy path treeNode.Value.createObject( getTransformFromPath( treeNode.getParentTreePath() ) , treeNode.Key); } }
/// <summary> /// will iterate the list of registered inGameISaveableGameObject and have them create a /// SaveableObject which are saved within this list or referenced by other SaveableObjects. /// However this function just stores the data temporary and does not write it into the scene file! /// "DirtyData" is set to true, so this scene will be saved to fíle upon next game save. /// </summary> /// <param name="saveState"></param> /// <param name="ignoreForSave">hand any gameobject over that shall not be saved. /// instead their theirs trees of restoreableGameobejcts are returned /// usefull to take gameobject to the next scene</param> /// <returns>this functions returns the unsaved objects, since its very /// likely that they shall be transfered to another scene</returns> public List <IRestorableGameObject> saveScene(PersistentGameDataController.SaveType saveState, List <ISaveableGameObject> ignoreForSave) { List <IRestorableGameObject> result = new List <IRestorableGameObject>(); ///clear current list, and fill it with current objects RestoreableObjectTree.clear(); HirachyTree <ISaveableGameObject> saveableObjectTree = new HirachyTree <ISaveableGameObject>(); Stack <int> currentHirachy; ///reset the hirachy tree before saving again foreach (ISaveableGameObject gameObject in AllInGameObjects) { gameObject.ResetChildNodes(); } ///set parent and children of all objects ///also adds all objects with no saveable parent to root list foreach (ISaveableGameObject gameObject in AllInGameObjects) { ///if the objects should not be saved in the current scene they ///are just ignored for now. since the objects dont use this tree to find ///their parent, the hirachy of the non saved objects are not lost if (!ignoreForSave.Contains(gameObject)) { if (!gameObject.findAndSetParent(out currentHirachy)) { saveableObjectTree.add(gameObject, currentHirachy); } } } ///all the gameObjects with no saveableParent are returning their saveable object, ///which are added to the list. childrens are recursivly added aswell foreach (ITreeNode <ISaveableGameObject> gameObject in saveableObjectTree.getAllNodesWithValues()) { RestoreableObjectTree.add (gameObject.Value.saveObjectAndPrepareScripts(), gameObject.getFullTreePath()); } ///even though the object is not saved in this scene the restoreable objects are ///still created and returned as result so they can be used in other scenes foreach (ISaveableGameObject gameObject in ignoreForSave) { result.Add(gameObject.saveObjectAndPrepareScripts()); } IList <ISaveableGameObject> savedObjects = saveableObjectTree.getValues(); ///after all object with their scripts were initiated the scripts values are saved foreach (ISaveableGameObject gameObject in savedObjects) { gameObject.saveAllBehaviours(saveState); } ///all not saved objects are saved aswell. however their restoreable references ///will not be associated with the current scene. Instead they are returned ///so they can be used in other scenes foreach (ISaveableGameObject gameObject in ignoreForSave) { gameObject.saveAllBehaviours(saveState); } ///deleting all children for next save saveableObjectTree.clear(); ///since the scene is saved, it needs to be stored to file on the ///next game save DirtyData = true; return(result); }