public void DeserializeOverride(BinaryFormatter formatter, MemoryStream stream) { // 1: scene count int sceneCount = (int)formatter.Deserialize(stream); Setup(sceneCount); if (sceneCount == 0) { return; } var defaultScene = default(Scene); SceneHandlesCache.Add(GetHandle(defaultScene), defaultScene); for (int n = SceneManager.sceneCount; n >= 0; n--) { var scene = SceneManager.GetSceneAt(n); SceneHandlesCache.Add(GetHandle(scene), scene); } for (int n = 0; n < sceneCount; n++) { // 2: scene handle int sceneHandle = (int)formatter.Deserialize(stream); Scene scene; bool sceneIsLoaded = SceneHandlesCache.TryGetValue(sceneHandle, out scene); // 3: InstanceID count for scene int idCount = (int)formatter.Deserialize(stream); if (sceneIsLoaded) { var objs = new BiDictionary <int, Object>(idCount); // 4: Instance IDs for scene for (int i = idCount - 1; i >= 0; i--) { var id = (int)formatter.Deserialize(stream); objs.Add(id, GetTargetInternal(id)); } instanceIdsByScene.Add(scene, objs); } // if scene is not loaded, discard the data else { for (int i = idCount - 1; i >= 0; i--) { formatter.Deserialize(stream); } } } #if DEV_MODE UnityEngine.Debug.Log(StringUtils.ToStringSansNamespace(GetType()) + ".DeserializeOverride InstanceIdsByScene now has " + instanceIdsByScene.Count + " items:\n" + StringUtils.ToString(instanceIdsByScene, "\n")); #endif }
private int GetIdInternal(Object target) { if (target == null) { return(0); } BiDictionary <int, Object> ids; Scene scene; var go = target.GameObject(); if (go != null) { scene = go.scene; if (!scene.IsValid()) { scene = default(Scene); } } else { scene = default(Scene); } if (!instanceIdsByScene.TryGetValue(scene, out ids)) { ids = new BiDictionary <int, Object>(); } int id = target.GetInstanceID(); ids[id] = target; return(id); }