Beispiel #1
0
        private void CreatePrefabWithDependencies(ProjectItem dropTarget, ExposeToEditor dragObject, Action <AssetItem[]> done)
        {
            IResourcePreviewUtility previewUtility = IOC.Resolve <IResourcePreviewUtility>();

            m_project.GetDependencies(dragObject.gameObject, true, (error, deps) =>
            {
                object[] objects;
                if (!deps.Contains(dragObject.gameObject))
                {
                    objects = new object[deps.Length + 1];
                    objects[deps.Length] = dragObject.gameObject;
                    for (int i = 0; i < deps.Length; ++i)
                    {
                        objects[i] = deps[i];
                    }
                }
                else
                {
                    objects = deps;
                }

                IUnityObjectFactory uoFactory = IOC.Resolve <IUnityObjectFactory>();
                objects = objects.Where(obj => uoFactory.CanCreateInstance(obj.GetType())).ToArray();

                byte[][] previewData = new byte[objects.Length][];
                for (int i = 0; i < objects.Length; ++i)
                {
                    if (objects[i] is UnityObject)
                    {
                        previewData[i] = previewUtility.CreatePreviewData((UnityObject)objects[i]);
                    }
                }
                CreatePrefab(dropTarget, previewData, objects, done);
            });
        }
        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);
        }
        public static ProjectAsyncOperation CreatePrefab(this IProject project, string folderPath, GameObject prefab, bool includeDeps, Func <UnityObject, byte[]> createPreview = null)
        {
            ProjectAsyncOperation ao = new ProjectAsyncOperation();

            folderPath = string.Format("{0}/{1}", project.Root.Name, folderPath);
            ProjectItem folder = project.Root.Get(folderPath, true) as ProjectItem;

            if (folder is AssetItem)
            {
                throw new ArgumentException("folderPath");
            }

            if (includeDeps)
            {
                project.GetDependencies(prefab, true, (error, deps) =>
                {
                    object[] objects;
                    if (!deps.Contains(prefab))
                    {
                        objects = new object[deps.Length + 1];
                        objects[deps.Length] = prefab;
                        for (int i = 0; i < deps.Length; ++i)
                        {
                            objects[i] = deps[i];
                        }
                    }
                    else
                    {
                        objects = deps;
                    }

                    IUnityObjectFactory uoFactory = IOC.Resolve <IUnityObjectFactory>();
                    objects = objects.Where(obj => uoFactory.CanCreateInstance(obj.GetType())).ToArray();

                    byte[][] previewData = new byte[objects.Length][];
                    if (createPreview != null)
                    {
                        for (int i = 0; i < objects.Length; ++i)
                        {
                            if (objects[i] is UnityObject)
                            {
                                previewData[i] = createPreview((UnityObject)objects[i]);
                            }
                        }
                    }

                    project.Save(new[] { folder }, previewData, objects, null, (saveErr, assetItems) =>
                    {
                        ao.Error       = saveErr;
                        ao.IsCompleted = true;
                    });
                });
            }
            else
            {
                byte[][] previewData = new byte[1][];
                if (createPreview != null)
                {
                    previewData[0] = createPreview(prefab);
                }

                project.Save(new[] { folder }, previewData, new[] { prefab }, null, (saveErr, assetItems) =>
                {
                    ao.Error       = saveErr;
                    ao.IsCompleted = true;
                });
            }

            return(ao);
        }
        protected override object WriteToImpl(object obj)
        {
            ClearReferencesCache();

            Scene scene = (Scene)obj;

            if (Descriptors == null && Data == null)
            {
                DestroyGameObjects(scene);
                return(obj);
            }

            if (Descriptors == null && Data != null || Data != null && Descriptors == null)
            {
                throw new ArgumentException("data is corrupted", "scene");
            }

            if (Descriptors.Length == 0)
            {
                DestroyGameObjects(scene);
                return(obj);
            }

            if (Identifiers == null || Identifiers.Length != Data.Length)
            {
                throw new ArgumentException("data is corrupted", "scene");
            }

            DestroyGameObjects(scene);
            Dictionary <TID, UnityObject> idToUnityObj = new Dictionary <TID, UnityObject>();

            for (int i = 0; i < Descriptors.Length; ++i)
            {
                PersistentDescriptor <TID> descriptor = Descriptors[i];
                if (descriptor != null)
                {
                    CreateGameObjectWithComponents(m_typeMap, descriptor, idToUnityObj, null);
                }
            }

            UnityObject[] assetInstances = null;
            if (AssetIds != null)
            {
                IUnityObjectFactory factory = IOC.Resolve <IUnityObjectFactory>();
                assetInstances = new UnityObject[AssetIds.Length];
                for (int i = 0; i < AssetIds.Length; ++i)
                {
                    Type uoType;
                    PersistentObject <TID> asset = Assets[i];
                    if (asset is PersistentRuntimeSerializableObject <TID> )
                    {
                        PersistentRuntimeSerializableObject <TID> runtimeSerializableObject = (PersistentRuntimeSerializableObject <TID>)asset;
                        uoType = runtimeSerializableObject.ObjectType;
                    }
                    else
                    {
                        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(AssetIds[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 < assetInstances.Length; ++i)
                {
                    UnityObject assetInstance = assetInstances[i];
                    if (assetInstance != null)
                    {
                        PersistentObject <TID> asset = Assets[i];
                        asset.WriteTo(assetInstance);
                    }
                }
            }

            RestoreDataAndResolveDependencies();
            m_assetDB.UnregisterSceneObjects();

            ClearReferencesCache();

            return(scene);
        }