/// <summary> /// Create PersistentScene object for active scene /// </summary> /// <returns>PersistentScene object</returns> public static PersistentScene CreatePersistentScene(params Type[] ignoreTypes) { if (IdentifiersMap.Instance == null) { Debug.LogError("Create Runtime Resource Map"); return(null); } GameObject[] gameObjects = SceneManager.GetActiveScene().GetRootGameObjects().OrderBy(g => g.transform.GetSiblingIndex()).ToArray(); PersistentScene persistentScene = new PersistentScene(); PersistentData.CreatePersistentDescriptorsAndData(gameObjects, out persistentScene.Descriptors, out persistentScene.Data /*, out persistentScene.ActiveSelf*/); return(persistentScene); }
protected T Write <T>(T dst, PersistentData src, Dictionary <long, UnityObject> objects) { if (src == null) { return(default(T)); } if (dst == null) { try { dst = Activator.CreateInstance <T>(); } catch (MissingMethodException) { Debug.LogWarningFormat("Unable to instantiate object. {0} default constructor missing", typeof(T).FullName); } } return((T)src.WriteTo(dst, objects)); }
public static void RestoreDataAndResolveDependencies(PersistentData[] dataObjects, Dictionary <long, UnityObject> objects) { //3. Create persistent data dictionary (new object id -> Persistent Data) Dictionary <UnityObject, PersistentData> persistentData = new Dictionary <UnityObject, PersistentData>(); for (int i = 0; i < dataObjects.Length; ++i) { PersistentData data = dataObjects[i]; UnityObject obj; if (objects.TryGetValue(data.InstanceId, out obj)) { persistentData.Add(obj, data); } } //4. Create replacement prefabs and replace PersistentIgnoreObjects foreach (KeyValuePair <UnityObject, PersistentData> kvp in persistentData) { PersistentIgnore persistentIgnore = kvp.Key as PersistentIgnore; if (persistentIgnore == null) { continue; } GameObject go = persistentIgnore.gameObject; PersistentData goData = persistentData[go]; PersistentData scriptData = kvp.Value; PersistentData transformData = persistentData[go.transform]; //Recover go data goData.WriteTo(go, objects); //Recover script data scriptData.WriteTo(persistentIgnore, objects); //Recover transform data transformData.WriteTo(go.transform, objects); /* * if (persistentIgnore.ReplacementPrefab != null) * { * //make sure that Replacement prefab is not active, * //this will prevent Awake and other methods from running too early * persistentIgnore.ReplacementPrefab.gameObject.SetActive(false); * * PersistentIgnore replacementScript = UnityObject.Instantiate(persistentIgnore.ReplacementPrefab); * List<GameObject> destroy = new List<GameObject>(); * //Destroy prefab children according to PersistentIgnore component settings * foreach (Transform childTransform in replacementScript.transform) * { * if (!replacementScript.IsChildOfReplacementPrefab(childTransform)) * { * destroy.Add(childTransform.gameObject); * } * } * for (int i = 0; i < destroy.Count; ++i) * { * UnityObject.DestroyImmediate(destroy[i]); * } * * //replace gameObject with repacementPrefab in objects dictionary * //Thereby all other objects will reference replacementPrefab instad of gameObject * objects[goData.InstanceId] = replacementScript.gameObject; * objects[scriptData.InstanceId] = replacementScript; * objects[transformData.InstanceId] = replacementScript.transform; * * //insert replacementPrefab to hierarchy * replacementScript.transform.SetParent(go.transform.parent); * foreach (Transform childTransform in go.transform) * { * childTransform.SetParent(replacementScript.transform); * } * * UnityObject.Destroy(go); //Destroy gameObject which was replaced by replacement Prefab * } */ } List <GameObject> goList = new List <GameObject>(); List <bool> goActivationList = new List <bool>(); //5. Recover data using scene.Data for (int i = 0; i < dataObjects.Length; ++i) { PersistentData data = dataObjects[i]; if (!objects.ContainsKey(data.InstanceId)) { Debug.LogWarningFormat("objects does not have object with instance id {0} however PersistentData of type {1} is present", data.InstanceId, data.GetType()); continue; } UnityObject obj = objects[data.InstanceId]; data.WriteTo(obj, objects); if (obj is GameObject) { goList.Add((GameObject)obj); goActivationList.Add(data.ActiveSelf); } } for (int i = 0; i < goList.Count; ++i) { bool activeSelf = goActivationList[i]; GameObject go = goList[i]; go.SetActive(activeSelf); } }
public PersistentScript(PersistentData baseObjData) { baseObjectData = baseObjData; }
public DataContract(PersistentData data) { Data = data; }