Пример #1
0
        public bool SyncFromProject()
        {
            ResourceCollection resourceCollection = new ResourceCollection();

            string[] assetBundleNames = GetUsedAssetBundleNames();
            foreach (string assetBundleName in assetBundleNames)
            {
                string name        = assetBundleName;
                string variant     = null;
                int    dotPosition = assetBundleName.LastIndexOf('.');
                if (dotPosition > 0 && dotPosition < assetBundleName.Length - 1)
                {
                    name    = assetBundleName.Substring(0, dotPosition);
                    variant = assetBundleName.Substring(dotPosition + 1);
                }

                if (!resourceCollection.AddResource(name, variant, LoadType.LoadFromFile, false))
                {
                    return(false);
                }

                string[] assetNames = GetAssetPathsFromAssetBundle(assetBundleName);
                foreach (string assetName in assetNames)
                {
                    string guid = AssetDatabase.AssetPathToGUID(assetName);
                    if (string.IsNullOrEmpty(guid))
                    {
                        return(false);
                    }

                    if (!resourceCollection.AssignAsset(guid, name, variant))
                    {
                        return(false);
                    }
                }
            }

            return(resourceCollection.Save());
        }
        public bool SyncToProject()
        {
            ResourceCollection resourceCollection = new ResourceCollection();

            resourceCollection.OnLoadingResource += delegate(int index, int count)
            {
                if (OnLoadingResource != null)
                {
                    OnLoadingResource(index, count);
                }
            };

            resourceCollection.OnLoadingAsset += delegate(int index, int count)
            {
                if (OnLoadingAsset != null)
                {
                    OnLoadingAsset(index, count);
                }
            };

            resourceCollection.OnLoadCompleted += delegate()
            {
                if (OnCompleted != null)
                {
                    OnCompleted();
                }
            };

            if (!resourceCollection.Load())
            {
                return(false);
            }

            int assetIndex = 0;
            int assetCount = resourceCollection.AssetCount;

            Resource[] resources = resourceCollection.GetResources();
            foreach (Resource resource in resources)
            {
                if (resource.IsLoadFromBinary)
                {
                    continue;
                }

                Asset[] assets = resource.GetAssets();
                foreach (Asset asset in assets)
                {
                    AssetImporter assetImporter = AssetImporter.GetAtPath(asset.Name);
                    if (assetImporter == null)
                    {
                        if (OnCompleted != null)
                        {
                            OnCompleted();
                        }

                        return(false);
                    }

                    assetImporter.assetBundleName    = resource.Name;
                    assetImporter.assetBundleVariant = resource.Variant;
                    assetImporter.SaveAndReimport();

                    if (OnResourceDataChanged != null)
                    {
                        OnResourceDataChanged(++assetIndex, assetCount, asset.Name);
                    }
                }
            }

            if (OnCompleted != null)
            {
                OnCompleted();
            }

            return(true);
        }