/* * Loads an Object * 'i' is the number of the object we are loading. */ private void LoadObject(int i, string file) { int uniqueID = ES2.Load <int>(file + "?tag=uniqueID" + i); string prefabName = ES2.Load <string>(file + "?tag=prefabName" + i); // Create or get an object based on our loaded id and prefabName GameObject loadObject; // If our prefab name is blank, we're loading a scene object. if (prefabName == "") { loadObject = ES2UniqueID.FindTransform(uniqueID).gameObject; } else { loadObject = UniqueObjectManager.InstantiatePrefab(prefabName); } // Load whether this GameObject is active or not. #if UNITY_3_5 loadObject.active = ES2.Load <bool>(file + "?tag=active" + i); #else loadObject.SetActive(ES2.Load <bool>(file + "?tag=active" + i)); #endif Transform t = loadObject.GetComponent <Transform>(); if (t != null) { // Auto-assigning Load is the best way to load Components. ES2.Load <Transform>(file + "?tag=transform" + i, t); // LocalNow we'll get the parent object, if any. int parentuID = ES2.Load <int>(file + "?tag=parentID" + i); Transform parent = ES2UniqueID.FindTransform(parentuID); if (parent != null) { t.parent = parent; } } }
/* * Saves an Object * 'i' is the number of the object we are saving. */ private void SaveObject(GameObject obj, int i, string file) { // Let's get the UniqueID object, as we'll need this. ES2UniqueID uID = obj.GetComponent <ES2UniqueID>(); //Note that we're appending the 'i' to the end of the path so that //we know which object each piece of data belongs to. ES2.Save(uID.id, file + "?tag=uniqueID" + i); ES2.Save(uID.prefabName, file + "?tag=prefabName" + i); // Save whether the GameObject this UniqueID is attached to is active or not. #if UNITY_3_5 ES2.Save(uID.gameObject.active, file + "?tag=active" + i); #else ES2.Save(uID.gameObject.activeSelf, file + "?tag=active" + i); #endif // You could add many more components here, inlcuding custom components. // For simplicity, we're only going to save the Transform component. Transform t = obj.GetComponent <Transform>(); if (t != null) { ES2.Save(t, file + "?tag=transform" + i); // We'll also save the UniqueID of the parent object here, or -1 // string if it doesn't have a parent. ES2UniqueID parentuID = ES2UniqueID.FindUniqueID(t.parent); if (parentuID == null) { ES2.Save(-1, file + "?tag=parentID" + i); } else { ES2.Save(parentuID.id, file + "?tag=parentID" + i); } } }