// load objects public void LoadFromFile() { // file stream object. DataManager fileStream = new DataManager(); int count = 0; string fullFile = ""; // loads content fileStream.ClearAllDataRecordsFromManager(); // clear existing content fullFile = GetFileWithPath(); // gets data fileStream.SetDataFile(fullFile); // if the record file is not available if (!fileStream.FileAvailable()) { Debug.LogError(fullFile + " not found."); return; } // loads records and gets count fileStream.LoadDataRecords(); count = fileStream.GetDataRecordAmount(); // loads children bool childLoad = (loadAsChildren && parent != null); // grabs all items, with each record representing one item for (int i = 0; i < count; i++) { GameObject newObject = null; byte[] byteData = fileStream.GetDataFromManager(i); object objectData = DataManager.DeserializeObject(byteData); SerializedObject serialData = (SerializedObject)(objectData); // gets to this line, then stops. newObject = SerializableObject.Unpack(serialData); // if the objects should be loaded as children of the current object. if (childLoad) { newObject.transform.parent = parent.transform; } // if a new object was generated, add it to the list. if (newObject != null) { objects.Add(newObject); } } fileStream.ClearAllDataRecordsFromManager(); Destroy(fileStream); Debug.Log("Load Successful!"); }
// for some reason Unity doesn't like these functions // packs data and returns it as byte array // public static byte[] PackToBytes(GameObject entity, string prefabPath = "") // { // SerializedObject obj = Pack(entity, prefabPath); // byte[] data = FileStream.SerializeObject(entity); // return data; // } // // // packs data and returns it as byte array // public byte[] PackToBytes(string prefabPath = "") // { // return PackToBytes(gameObject, prefabPath); // } // the unpack functions are all static since these are meant to load in game objects // unpacks the serialized object // there's no local Unpack since a new object would need to be created, then its values would need to be instaniated. public static GameObject Unpack(SerializedObject serializedObject) { // if the serialized object is empty, return null. if (serializedObject == null) { return(null); } // game object GameObject gameObject; // PARENT // // there is a prefab - load prefab if (serializedObject.prefabPath != "") { Object newObject = Resources.Load(serializedObject.prefabPath); gameObject = Instantiate((GameObject)newObject); gameObject.name = serializedObject.name; } else // there is no prefab - load from type { gameObject = new GameObject(serializedObject.name); } // sets values for transformation gameObject.transform.position = SerializedObject.Vec3ToUnityVector3(serializedObject.position); gameObject.transform.rotation = SerializedObject.QuatToQuaternion(serializedObject.rotation); gameObject.transform.localScale = SerializedObject.Vec3ToUnityVector3(serializedObject.localScale); // COMPONENTS // // gets and adds components, giving them the overwritten data foreach (SerializedComponent comp in serializedObject.components) { // generates a serialized object from the component. // object objectComp = System.Activator.CreateInstance(comp.type); Component attachedComp = null; // component // if there is no type, move on to the next one. if (comp.type == null) { continue; } // checks to see if the component is already attached // (Name returns class name, .FullName returns class namespace with class name) attachedComp = gameObject.GetComponent(comp.type.FullName); // if the attached component is null, it means it hasn't actually been attached at. if (attachedComp == null) { attachedComp = gameObject.AddComponent(comp.type); // attaches component } // if the component is a serializable object, call its function. if (attachedComp is SerializableObject) { SerializableObject serialObject = (SerializableObject)(attachedComp); serialObject.ImportSerializedComponent(serializedObject, comp); // passes content so it can be downcasted. } } // CHILDREN // // adds the children foreach (SerializedObject child in serializedObject.children) { GameObject importedChild = SerializableObject.Unpack(child); importedChild.transform.parent = gameObject.transform; } return(gameObject); }