protected override object WriteToImpl(object obj) { if (Descriptors == null && Data == null) { return(obj); } if (Descriptors == null && Data != null || Data != null && Descriptors == null) { throw new ArgumentException("data is corrupted", "scene"); } if (Descriptors.Length == 0) { return(obj); } if (Identifiers == null || Identifiers.Length != Data.Length) { throw new ArgumentException("data is corrupted", "scene"); } Scene scene = (Scene)obj; GameObject[] rootGameObjects = scene.GetRootGameObjects(); for (int i = 0; i < rootGameObjects.Length; ++i) { GameObject rootGO = rootGameObjects[i]; if (rootGO.GetComponent <RTSLIgnore>()) { continue; } UnityObject.DestroyImmediate(rootGO); } Dictionary <int, UnityObject> idToUnityObj = new Dictionary <int, UnityObject>(); for (int i = 0; i < Descriptors.Length; ++i) { PersistentDescriptor descriptor = Descriptors[i]; if (descriptor != null) { CreateGameObjectWithComponents(m_typeMap, descriptor, idToUnityObj, null); } } UnityObject[] assetInstances = null; if (AssetIdentifiers != null) { IUnityObjectFactory factory = IOC.Resolve <IUnityObjectFactory>(); assetInstances = new UnityObject[AssetIdentifiers.Length]; for (int i = 0; i < AssetIdentifiers.Length; ++i) { PersistentObject asset = Assets[i]; Type uoType = m_typeMap.ToUnityType(asset.GetType()); if (uoType != null) { if (factory.CanCreateInstance(uoType, asset)) { UnityObject assetInstance = factory.CreateInstance(uoType, asset); if (assetInstance != null) { assetInstances[i] = assetInstance; idToUnityObj.Add(AssetIdentifiers[i], assetInstance); } } else { Debug.LogWarning("Unable to create object of type " + uoType.ToString()); } } else { Debug.LogWarning("Unable to resolve unity type for " + asset.GetType().FullName); } } } m_assetDB.RegisterSceneObjects(idToUnityObj); if (assetInstances != null) { for (int i = 0; i < AssetIdentifiers.Length; ++i) { UnityObject assetInstance = assetInstances[i]; if (assetInstance != null) { PersistentObject asset = Assets[i]; asset.WriteTo(assetInstance); } } } RestoreDataAndResolveDependencies(); m_assetDB.UnregisterSceneObjects(); return(scene); }
protected void RestoreDataAndResolveDependencies() { List <GameObject> goList = new List <GameObject>(); List <bool> goActivationList = new List <bool>(); for (int i = 0; i < Data.Length; ++i) { PersistentObject data = Data[i]; long id = Identifiers[i]; UnityObject obj = FromID <UnityObject>(id); if (obj == null) { Debug.LogWarningFormat("objects does not have object with instance id {0} however PersistentData of type {1} is present", id, data.GetType()); continue; } data.WriteTo(obj); if (obj is GameObject) { goList.Add((GameObject)obj); PersistentGameObject goData = (PersistentGameObject)data; goActivationList.Add(goData.ActiveSelf); } } for (int i = 0; i < goList.Count; ++i) { bool activeSelf = goActivationList[i]; GameObject go = goList[i]; if (go != null) { go.SetActive(activeSelf); } } }