private static void CreateAssetLibraryForScene(Scene scene, int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs) { TypeMap typeMap = new TypeMap(); AssetDB assetDB = new AssetDB(); RuntimeShaderUtil shaderUtil = new RuntimeShaderUtil(); IOC.Register <ITypeMap>(typeMap); IOC.Register <IAssetDB>(assetDB); IOC.Register <IRuntimeShaderUtil>(shaderUtil); PersistentRuntimeScene rtScene = new PersistentRuntimeScene(); GetDepsFromContext ctx = new GetDepsFromContext(); rtScene.GetDepsFrom(scene, ctx); Queue <UnityObject> depsQueue = new Queue <UnityObject>(ctx.Dependencies.OfType <UnityObject>()); GetDepsFromContext getDepsCtx = new GetDepsFromContext(); while (depsQueue.Count > 0) { UnityObject uo = depsQueue.Dequeue(); if (!uo) { continue; } if (!(uo is GameObject) && !(uo is Component)) { Type persistentType = typeMap.ToPersistentType(uo.GetType()); if (persistentType != null) { getDepsCtx.Clear(); PersistentObject persistentObject = (PersistentObject)Activator.CreateInstance(persistentType); persistentObject.ReadFrom(uo); persistentObject.GetDepsFrom(uo, getDepsCtx); foreach (UnityObject dep in getDepsCtx.Dependencies) { if (!ctx.Dependencies.Contains(dep)) { ctx.Dependencies.Add(dep); depsQueue.Enqueue(dep); } } } } } IOC.Unregister <IRuntimeShaderUtil>(shaderUtil); IOC.Unregister <ITypeMap>(typeMap); IOC.Unregister <IAssetDB>(assetDB); CreateAssetLibrary(ctx.Dependencies.ToArray(), "Scenes/" + scene.name, "SceneAssetLibrary", index, asset, folder, hs); }
protected override void ReadFromImpl(object obj) { Scene scene = (Scene)obj; GameObject[] rootGameObjects = scene.GetRootGameObjects(); List <PersistentObject> data = new List <PersistentObject>(); List <long> identifiers = new List <long>(); List <PersistentDescriptor> descriptors = new List <PersistentDescriptor>(rootGameObjects.Length); GetDepsFromContext getSceneDepsCtx = new GetDepsFromContext(); for (int i = 0; i < rootGameObjects.Length; ++i) { GameObject rootGO = rootGameObjects[i]; PersistentDescriptor descriptor = CreateDescriptorAndData(rootGO, data, identifiers, getSceneDepsCtx); if (descriptor != null) { descriptors.Add(descriptor); } } HashSet <object> allDeps = getSceneDepsCtx.Dependencies; List <UnityObject> externalDeps = new List <UnityObject>(allDeps.OfType <UnityObject>()); Queue <UnityObject> depsQueue = new Queue <UnityObject>(allDeps.OfType <UnityObject>()); List <PersistentObject> assets = new List <PersistentObject>(); List <int> assetIdentifiers = new List <int>(); GetDepsFromContext getDepsCtx = new GetDepsFromContext(); while (depsQueue.Count > 0) { UnityObject uo = depsQueue.Dequeue(); if (!uo) { continue; } if (!m_assetDB.IsMapped(uo)) { if (!(uo is GameObject) && !(uo is Component)) { Type persistentType = m_typeMap.ToPersistentType(uo.GetType()); if (persistentType != null) { getDepsCtx.Clear(); PersistentObject persistentObject = (PersistentObject)Activator.CreateInstance(persistentType); persistentObject.ReadFrom(uo); persistentObject.GetDepsFrom(uo, getDepsCtx); assets.Add(persistentObject); assetIdentifiers.Add(uo.GetInstanceID()); foreach (UnityObject dep in getDepsCtx.Dependencies) { if (!allDeps.Contains(dep)) { allDeps.Add(dep); depsQueue.Enqueue(dep); } } } } externalDeps.Remove(uo); } } Descriptors = descriptors.ToArray(); Identifiers = identifiers.ToArray(); Data = data.ToArray(); Dependencies = externalDeps.Select(uo => m_assetDB.ToID(uo)).ToArray(); Assets = assets.ToArray(); AssetIdentifiers = assetIdentifiers.ToArray(); }
protected PersistentDescriptor CreateDescriptorAndData(GameObject go, List <PersistentObject> persistentData, List <long> persistentIdentifiers, /*HashSet<int> usings,*/ GetDepsFromContext getDepsFromCtx, PersistentDescriptor parentDescriptor = null) { if (go.GetComponent <RTSLIgnore>()) { //Do not save persistent ignore objects return(null); } Type persistentType = m_typeMap.ToPersistentType(go.GetType()); if (persistentType == null) { return(null); } long persistentID = ToID(go); //if(m_assetDB.IsResourceID(persistentID)) //{ // int ordinal = m_assetDB.ToOrdinal(persistentID); // usings.Add(ordinal); //} PersistentDescriptor descriptor = new PersistentDescriptor(m_typeMap.ToGuid(persistentType), persistentID, go.name); descriptor.Parent = parentDescriptor; PersistentObject goData = (PersistentObject)Activator.CreateInstance(persistentType); goData.ReadFrom(go); goData.GetDepsFrom(go, getDepsFromCtx); persistentData.Add(goData); persistentIdentifiers.Add(persistentID); Component[] components = go.GetComponents <Component>().Where(c => c != null).ToArray(); if (components.Length > 0) { List <PersistentDescriptor> componentDescriptors = new List <PersistentDescriptor>(); for (int i = 0; i < components.Length; ++i) { Component component = components[i]; Type persistentComponentType = m_typeMap.ToPersistentType(component.GetType()); if (persistentComponentType == null) { continue; } long componentID = ToID(component); //if (m_assetDB.IsResourceID(componentID)) //{ // int ordinal = m_assetDB.ToOrdinal(componentID); // usings.Add(ordinal); //} PersistentDescriptor componentDescriptor = new PersistentDescriptor(m_typeMap.ToGuid(persistentComponentType), componentID, component.name); componentDescriptor.Parent = descriptor; componentDescriptors.Add(componentDescriptor); PersistentObject componentData = (PersistentObject)Activator.CreateInstance(persistentComponentType); componentData.ReadFrom(component); componentData.GetDepsFrom(component, getDepsFromCtx); persistentData.Add(componentData); persistentIdentifiers.Add(componentID); } if (componentDescriptors.Count > 0) { descriptor.Components = componentDescriptors.ToArray(); } } Transform transform = go.transform; if (transform.childCount > 0) { List <PersistentDescriptor> children = new List <PersistentDescriptor>(); foreach (Transform child in transform) { PersistentDescriptor childDescriptor = CreateDescriptorAndData(child.gameObject, persistentData, persistentIdentifiers, /*usings,*/ getDepsFromCtx, descriptor); if (childDescriptor != null) { children.Add(childDescriptor); } } descriptor.Children = children.ToArray(); } return(descriptor); }