Ejemplo n.º 1
0
        public void Save(AssetItem assetItem, object obj, ProjectEventHandler callback)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            Type persistentType = m_typeMap.ToPersistentType(obj.GetType());

            if (persistentType == null)
            {
                throw new ArgumentException(string.Format("PersistentClass for {0} does not exist", obj.GetType()), "obj");
            }

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

            persistentObject.ReadFrom(obj);

            if (persistentObject is PersistentPrefab)
            {
                PersistentPrefab persistentPrefab = (PersistentPrefab)persistentObject;
                if (persistentPrefab.Descriptors != null)
                {
                    List <PrefabPart> prefabParts = new List <PrefabPart>();
                    PersistentDescriptorsToPrefabPartItems(persistentPrefab.Descriptors, prefabParts);
                    assetItem.Parts = prefabParts.ToArray();
                }
            }

            GetDepsContext getDepsCtx = new GetDepsContext();

            persistentObject.GetDeps(getDepsCtx);

            assetItem.Dependencies = getDepsCtx.Dependencies.ToArray();

            m_storage.Save(m_projectPath, assetItem.Parent.ToString(), assetItem, persistentObject, m_projectInfo, error =>
            {
                callback(error);
            });
        }
Ejemplo n.º 2
0
        private void OnLoadCompleted(AssetItem rootItem, AssetItem[] assetItems, PersistentObject[] persistentObjects, ProjectEventHandler <UnityObject> callback)
        {
            for (int i = 0; i < assetItems.Length; ++i)
            {
                AssetItem assetItem = assetItems[i];
                if (!m_assetDB.IsMapped(assetItem.ItemID))
                {
                    if (m_assetDB.IsStaticResourceID(assetItem.ItemID))
                    {
                        int ordinal = m_assetDB.ToOrdinal(assetItem.ItemID);
                        if (m_assetDB.IsLibraryRefLoaded(ordinal))
                        {
                            m_assetDB.RemoveLibrary(ordinal);
                        }

                        if (!m_assetDB.IsLibraryLoaded(ordinal))
                        {
                            AssetLibraryReferenceInfo reference = m_projectInfo.References.FirstOrDefault(r => r.Ordinal == ordinal);
                            if (reference != null)
                            {
                                m_assetDB.LoadLibrary(reference.AssetLibrary, reference.Ordinal);
                            }
                        }
                    }
                    else if (m_assetDB.IsDynamicResourceID(assetItem.ItemID))
                    {
                        PersistentObject persistentObject = persistentObjects[i];
                        if (persistentObject != null)
                        {
                            if (persistentObject is PersistentPrefab)
                            {
                                PersistentPrefab persistentPrefab                = (PersistentPrefab)persistentObject;
                                Dictionary <int, UnityObject> idToObj            = new Dictionary <int, UnityObject>();
                                List <GameObject>             createdGameObjects = new List <GameObject>();
                                persistentPrefab.CreateGameObjectWithComponents(m_typeMap, persistentPrefab.Descriptors[0], idToObj, createdGameObjects);
                                m_assetDB.RegisterDynamicResources(idToObj);
                                for (int j = 0; j < createdGameObjects.Count; ++j)
                                {
                                    GameObject createdGO = createdGameObjects[i];
                                    createdGO.transform.SetParent(createdGO.transform, false);
                                    m_dynamicResources.Add(unchecked ((int)m_assetDB.ToID(createdGO)), createdGO);
                                }
                            }
                            else
                            {
                                Type        type     = m_typeMap.ToType(assetItem.TypeGuid);
                                UnityObject instance = m_factory.CreateInstance(type);
                                m_assetDB.RegisterDynamicResource(unchecked ((int)assetItem.ItemID), instance);
                                m_dynamicResources.Add(unchecked ((int)assetItem.ItemID), instance);
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < persistentObjects.Length; ++i)
            {
                PersistentObject persistentObject = persistentObjects[i];
                if (persistentObject != null)
                {
                    if (m_assetDB.IsSceneID(assetItems[i].ItemID))
                    {
                        persistentObject.WriteTo(SceneManager.GetActiveScene());
                    }
                    else
                    {
                        UnityObject obj = m_assetDB.FromID <UnityObject>(assetItems[i].ItemID);
                        Debug.Assert(obj != null);
                        if (obj != null)
                        {
                            persistentObject.WriteTo(obj);
                        }
                    }
                }
            }

            UnityObject result = m_assetDB.FromID <UnityObject>(rootItem.ItemID);

            callback(new Error(Error.OK), result);
        }
Ejemplo n.º 3
0
        public void Save(ProjectItem parent, byte[] previewData, object obj, ProjectEventHandler <ProjectItem> callback)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            Type persistentType = m_typeMap.ToPersistentType(obj.GetType());

            if (persistentType == null)
            {
                throw new ArgumentException(string.Format("PersistentClass for {0} does not exist", obj.GetType()), "obj");
            }

            if (parent == null)
            {
                parent = Root;
            }

            if (!parent.IsFolder)
            {
                throw new ArgumentException("parent is not folder", "parent");
            }

            int ordinal, id;

            if (!GetOrdinalAndId(m_projectInfo.AssetIdentifier, out ordinal, out id))
            {
                return;
            }

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

            persistentObject.ReadFrom(obj);

            AssetItem assetItem = new AssetItem();

            assetItem.ItemID   = m_assetDB.ToRuntimeResourceID(ordinal, id);
            assetItem.Name     = persistentObject.name;
            assetItem.Ext      = GetExt(obj);
            assetItem.TypeGuid = m_typeMap.ToGuid(obj.GetType());
            assetItem.Preview  = new Preview
            {
                ItemID      = assetItem.ItemID,
                PreviewData = previewData
            };

            if (persistentObject is PersistentPrefab)
            {
                PersistentPrefab persistentPrefab = (PersistentPrefab)persistentObject;
                if (persistentPrefab.Descriptors != null)
                {
                    List <PrefabPart> prefabParts = new List <PrefabPart>();
                    PersistentDescriptorsToPrefabPartItems(persistentPrefab.Descriptors, prefabParts);
                    assetItem.Parts = prefabParts.ToArray();
                }
            }

            GetDepsContext getDepsCtx = new GetDepsContext();

            persistentObject.GetDeps(getDepsCtx);
            assetItem.Dependencies = getDepsCtx.Dependencies.ToArray();

            m_storage.Save(m_projectPath, parent.ToString(), assetItem, persistentObject, m_projectInfo, error =>
            {
                if (!error.HasError)
                {
                    m_idToAssetItem.Add(assetItem.ItemID, assetItem);
                    if (assetItem.Parts != null)
                    {
                        for (int i = 0; i < assetItem.Parts.Length; ++i)
                        {
                            m_idToAssetItem.Add(assetItem.Parts[i].PartID, assetItem);
                        }
                    }

                    parent.AddChild(assetItem);
                }

                callback(error, assetItem);
            });
        }