// loads content from file public void LoadContent() { // loads content SetManagerFile(file); // if the file is not available, do not save content. if (!FileAvailable()) { Debug.LogError("File Not Available. Load Failed."); return; } // loads the data records if (LoadDataRecords()) { Debug.Log("Content Loaded"); } else { Debug.LogError("Error. Content Not Loaded"); return; } // gets the amount of data int dataCount = GetDataRecordAmount(); // loads all objects for (int i = 0; i < dataCount; i++) { // gets the data, unpacks it, then generates a game object. DataRecord dr = GetDataRecordFromManager(i); object unpacked = DeserializeObject(dr.data); SerializablePrefabObject so = (SerializablePrefabObject)unpacked; GameObject newObject = UnpackPrefabGameObject(so); // if the game object should be loaded as a child of this game object. if (loadAsChildren) { newObject.transform.parent = gameObject.transform; } } // refreshes the collision manager CollisionManager cm = FindObjectOfType <CollisionManager>(); // refreshes the cube list if (cm != null) { cm.RefreshCubeList(true); } // clears ut the record data ClearAllDataRecordsFromManager(); }
// deserializes a game object public GameObject UnpackPrefabGameObject(SerializablePrefabObject so) { // loads the new object object prefab = Resources.Load("Prefabs/" + so.prefab); GameObject newObject; if (prefab != null) { newObject = Instantiate((GameObject)prefab); // instantiate game object } else { newObject = new GameObject(); // empty game object otherwise } newObject.SetActive(so.active); // transformation information // position newObject.transform.position = new Vector3(so.position.x, so.position.y, so.position.z); newObject.transform.rotation = new Quaternion(so.rotation.x, so.rotation.y, so.rotation.z, so.rotation.w); newObject.transform.localScale = new Vector3(so.scale.x, so.scale.y, so.scale.z); return(newObject); }
// saves content public void SaveContent() { // sets the manager file SetManagerFile(file); // the file will be generated if it doesn't exist, so this is unneeded. // if the file is not available, do not save content. // if (!FileAvailable()) // { // Debug.LogError("File Not Accessible. Save Failed."); // return; // } // if 'true', it finds all active blicks if (findBlocks) { // gets all cubes in the scene (active and inactive) CubeBehaviour[] cubes = FindObjectsOfType <CubeBehaviour>(true); // adds blocks for (int i = 0; i < cubes.Length; i++) { // the player shouldn't be added. if (!blocks.Contains(cubes[i]) && cubes[i].tag != "Player") { blocks.Add(cubes[i]); } } } // adds children of the parent object. // if all blocks with the component were added, then this doesn't need to be called if (!findBlocks && addBlockChildren) { // gets cubes that are children of the parent object. CubeBehaviour[] cubes = GetComponentsInChildren <CubeBehaviour>(true); // adds cubes for (int i = 0; i < cubes.Length; i++) { if (!blocks.Contains(cubes[i])) { blocks.Add(cubes[i]); } } } // SAVING DATA // // for every block foreach (CubeBehaviour cube in blocks) { // packs all objects for the data record SerializablePrefabObject spo = PackPrefabGameObject(cube.prefab, cube.gameObject); DataRecord dr; // data record // gets the data, and gives it to the manager dr.data = SerializeObject(spo); AddDataRecordToManager(dr); } string getfile = GetManagerFile(); int recordAmnt = GetDataRecordAmount(); Debug.Log("File: " + getfile); Debug.Log("Record Count: " + recordAmnt); // saves the data if (SaveDataRecords()) { Debug.Log("Content Saved"); } else { Debug.LogError("Error. Content Not Saved"); } // deletes all records - calling this function causes the game to crash, and I don't know why. // either way, this function shouldn't be used. // DeleteAllDataRecordsFromManager(); ClearAllDataRecordsFromManager(); }