Exemple #1
0
        private void GetDependenciesFrom(GameObject go, GetDepsFromContext context)
        {
            if (go.GetComponent <RTSL2Ignore>())
            {
                //Do not save persistent ignore objects
                return;
            }
            Type persistentType = m_typeMap.ToPersistentType(go.GetType());

            if (persistentType == null)
            {
                return;
            }

            PersistentObject goData = (PersistentObject)Activator.CreateInstance(persistentType);

            goData.GetDepsFrom(go, context);

            Component[] components = go.GetComponents <Component>().Where(c => c != null).ToArray();
            if (components.Length > 0)
            {
                for (int i = 0; i < components.Length; ++i)
                {
                    Component component = components[i];
                    Type      persistentComponentType = m_typeMap.ToPersistentType(component.GetType());
                    if (persistentComponentType == null)
                    {
                        continue;
                    }

                    PersistentObject componentData = (PersistentObject)Activator.CreateInstance(persistentComponentType);
                    componentData.GetDepsFrom(component, context);
                }
            }

            Transform transform = go.transform;

            if (transform.childCount > 0)
            {
                foreach (Transform child in transform)
                {
                    GetDependenciesFrom(child.gameObject, context);
                }
            }
        }
        protected PersistentDescriptor CreateDescriptorAndData(GameObject go, List <PersistentObject> persistentData, List <long> persistentIdentifiers, /*HashSet<int> usings,*/ GetDepsFromContext getDepsFromCtx, PersistentDescriptor parentDescriptor = null)
        {
            if (go.GetComponent <RTSL2Ignore>())
            {
                //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);
        }
Exemple #3
0
        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();
        }