123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- //------------------------------------------------------------
- // Game Framework
- // Copyright © 2013-2021 loyalsoft. All rights reserved.
- // Homepage: http://www.game7000.com/
- // Feedback: http://www.game7000.com/
- //------------------------------------------------------------
- using GameFramework;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEditor;
- namespace UnityGameFramework.Editor.ResourceTools
- {
- public sealed class ResourceSyncToolsController
- {
- public ResourceSyncToolsController()
- {
- }
- public event GameFrameworkAction<int, int> OnLoadingResource = null;
- public event GameFrameworkAction<int, int> OnLoadingAsset = null;
- public event GameFrameworkAction OnCompleted = null;
- public event GameFrameworkAction<int, int, string> OnResourceDataChanged = null;
- public string[] GetAllAssetBundleNames()
- {
- return AssetDatabase.GetAllAssetBundleNames();
- }
- public string[] GetUsedAssetBundleNames()
- {
- HashSet<string> hashSet = new HashSet<string>(GetAllAssetBundleNames());
- hashSet.ExceptWith(GetUnusedAssetBundleNames());
- return hashSet.ToArray();
- }
- public string[] GetUnusedAssetBundleNames()
- {
- return AssetDatabase.GetUnusedAssetBundleNames();
- }
- public string[] GetAssetPathsFromAssetBundle(string assetBundleName)
- {
- return AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
- }
- public string[] GetAssetPathsFromAssetBundleAndAssetName(string assetBundleName, string assetName)
- {
- return AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
- }
- public bool RemoveAssetBundleName(string assetBundleName, bool forceRemove)
- {
- return AssetDatabase.RemoveAssetBundleName(assetBundleName, forceRemove);
- }
- public void RemoveUnusedAssetBundleNames()
- {
- AssetDatabase.RemoveUnusedAssetBundleNames();
- }
- public bool RemoveAllAssetBundleNames()
- {
- HashSet<string> allAssetNames = new HashSet<string>();
- string[] assetBundleNames = GetUsedAssetBundleNames();
- foreach (string assetBundleName in assetBundleNames)
- {
- string[] assetNames = GetAssetPathsFromAssetBundle(assetBundleName);
- foreach (string assetName in assetNames)
- {
- allAssetNames.Add(assetName);
- }
- }
- int assetIndex = 0;
- int assetCount = allAssetNames.Count;
- foreach (string assetName in allAssetNames)
- {
- AssetImporter assetImporter = AssetImporter.GetAtPath(assetName);
- if (assetImporter == null)
- {
- if (OnCompleted != null)
- {
- OnCompleted();
- }
- return false;
- }
- assetImporter.assetBundleVariant = null;
- assetImporter.assetBundleName = null;
- assetImporter.SaveAndReimport();
- if (OnResourceDataChanged != null)
- {
- OnResourceDataChanged(++assetIndex, assetCount, assetName);
- }
- }
- RemoveUnusedAssetBundleNames();
- if (OnCompleted != null)
- {
- OnCompleted();
- }
- return true;
- }
- 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;
- }
- 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, null, 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();
- }
- }
- }
|