Beispiel #1
0
        public ProjectAsyncOperation <AssetItem> Save(ProjectItem parent, byte[] previewData, object obj, string nameOverride, ProjectEventHandler <AssetItem> callback)
        {
            if (m_root == null)
            {
                throw new InvalidOperationException("Project is not opened. Use OpenProject method");
            }

            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

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

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

            ProjectAsyncOperation <AssetItem> ao = new ProjectAsyncOperation <AssetItem>();

            if (persistentType == typeof(PersistentGameObject))
            {
                persistentType = typeof(PersistentRuntimePrefab);
            }

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

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

            int assetIdBackup = m_projectInfo.AssetIdentifier;
            int rootOrdinal;
            int rootId;

            if (!GetOrdinalAndId(ref m_projectInfo.AssetIdentifier, out rootOrdinal, out rootId))
            {
                OnExhausted(callback, ao, assetIdBackup);
                return(ao);
            }

            if (obj is GameObject)
            {
                Dictionary <int, UnityObject> idToObj = new Dictionary <int, UnityObject>();
                GameObject go = (GameObject)obj;
                idToObj.Add(unchecked ((int)m_assetDB.ToDynamicResourceID(rootOrdinal, rootId)), go);

                Transform[] transforms = go.GetComponentsInChildren <Transform>(true);
                for (int i = 0; i < transforms.Length; ++i)
                {
                    Transform tf = transforms[i];
                    if (tf.gameObject != go)
                    {
                        int ordinal;
                        int id;
                        if (!GetOrdinalAndId(ref m_projectInfo.AssetIdentifier, out ordinal, out id))
                        {
                            OnExhausted(callback, ao, assetIdBackup);
                            return(ao);
                        }
                        idToObj.Add(unchecked ((int)m_assetDB.ToDynamicResourceID(ordinal, id)), tf.gameObject);
                    }

                    Component[] components = tf.GetComponents <Component>();
                    for (int j = 0; j < components.Length; ++j)
                    {
                        Component comp = components[j];
                        int       ordinal;
                        int       id;
                        if (!GetOrdinalAndId(ref m_projectInfo.AssetIdentifier, out ordinal, out id))
                        {
                            OnExhausted(callback, ao, assetIdBackup);
                            return(ao);
                        }
                        idToObj.Add(unchecked ((int)m_assetDB.ToDynamicResourceID(ordinal, id)), comp);
                    }
                }

                m_assetDB.RegisterDynamicResources(idToObj);
            }
            else if (obj is UnityObject)
            {
                m_assetDB.RegisterDynamicResource((int)m_assetDB.ToDynamicResourceID(rootOrdinal, rootId), (UnityObject)obj);
            }

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

            persistentObject.ReadFrom(obj);

            if (!string.IsNullOrEmpty(nameOverride))
            {
                persistentObject.name = nameOverride;
            }

            AssetItem assetItem = new AssetItem();

            if (obj is Scene)
            {
                assetItem.ItemID = m_assetDB.ToSceneID(rootOrdinal, rootId);
            }
            else
            {
                assetItem.ItemID = m_assetDB.ToDynamicResourceID(rootOrdinal, rootId);
            }

            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 PersistentRuntimePrefab)
            {
                PersistentRuntimePrefab persistentPrefab = (PersistentRuntimePrefab)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)
                {
                    if (!(obj is Scene))
                    {
                        if (assetItem.Parts != null)
                        {
                            for (int i = 0; i < assetItem.Parts.Length; ++i)
                            {
                                m_idToAssetItem.Add(assetItem.Parts[i].PartID, assetItem);
                            }
                        }
                        else
                        {
                            m_idToAssetItem.Add(assetItem.ItemID, assetItem);
                        }
                    }

                    parent.AddChild(assetItem);
                }

                if (callback != null)
                {
                    callback(error, assetItem);
                }
                ao.Error       = error;
                ao.Result      = assetItem;
                ao.IsCompleted = true;
            });

            return(ao);
        }