assetbundle分包

寻找被多个包引用的资源单独出来生成一个

  class AssetRefCache
    {
        public long m_Guid;
        int m_RefCount;
        public string m_ABNames;
        public AssetRefCache()
        {
            m_RefCount = 0;
        }

        public void AddRef()
        {
            m_RefCount++;
        }
    }

    class PackCache
    {
        public string m_Name;
        public List m_RefPacks;
        public PackCache(string name)
        {
            m_Name = name;
            m_RefPacks = new List();
        }
    }


    public class CheckReference
    {

        static Dictionary m_CacheDics;
        static Dictionary m_SplitAssetDics;
        static Dictionary m_CachePackDics;


        static private List GetFolderNames(string path)
        {
            List dirs = new List();
            foreach (string strs in Directory.GetDirectories(path))
            {
                dirs.Add(strs);
            }
            return dirs;
        }

        static List GetFileNames(string path)
        {
            List dirs = new List();
            foreach (string strs in Directory.GetFiles(path))
            {
                dirs.Add(strs);
            }
            return dirs;
        }

        [MenuItem("AssetBundle/AssetBundleUnpack", false, 10)]
        static private void AssetBundleUnpack()
        {
            EditorSettings.serializationMode = SerializationMode.ForceText;
            string path = AssetDatabase.GetAssetPath(Selection.activeObject);
            if (path.Equals(""))
                return;
            m_SplitAssetDics = new Dictionary();
            m_CacheDics = new Dictionary();
            m_CachePackDics = new Dictionary();

            SplitPack(GetFolderNames(path).ToArray());
            string outputPath = Path.Combine(Utility.AssetBundlesOutputPath, Utility.GetPlatformName());
            if (!Directory.Exists(outputPath))
                Directory.CreateDirectory(outputPath);
            BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.None,
                EditorUserBuildSettings.activeBuildTarget);
        }
        /// 
        /// 分割沉余资源
        /// 
        static private void SplitPack(string[] paths)
        {
            
            for (int i=0;i cache in m_SplitAssetDics)
            {
                string guid = AssetDatabase.AssetPathToGUID(cache.Key);
                SetAssetBundleName(guid, cache.Key);
                m_CachePackDics[cache.Value.m_ABNames].m_RefPacks.Add(guid);
            }
        }

        static private void CacheFolder(string path, string abName)
        {
            path.Replace("\\", "/");
            foreach (string str in Directory.GetDirectories(path))
            {
                CacheFile(str, abName);
            }
        }
        static private void CacheFile(string path, string abName)
        {
            path.Replace("\\", "/");
            foreach (string str in Directory.GetFiles(path))
            { 
               CacheDependencies(str, abName); 
            }

        }

        static private void CacheDependencies(string path, string abName)
        {
            path.Replace("\\", "/");
            string[] strs = AssetDatabase.GetDependencies(path);
            foreach (string str in strs)
            {
                AssetRefCache cache;
                if (m_CacheDics.TryGetValue(str, out cache))
                {
                    if (cache.m_ABNames.Equals(abName))
                    {
                        cache.AddRef();
                        if(!m_SplitAssetDics.ContainsKey(str))
                            m_SplitAssetDics.Add(str, cache);
                    }
                }
                else
                {
                    cache = new AssetRefCache();
                    cache.m_ABNames = abName;
                    m_CacheDics.Add(str, cache); 
                }
            }
            SetAssetBundleName(abName, path);
        }


        /// 
        /// 修改ab name
        /// 
        /// 
        static private void SetAssetBundleName(string name, string path)
        {
            path = path.Replace("\\", "/");
            path = path.Replace(".meta", "");
            var importer = AssetImporter.GetAtPath(path);
            if (importer && importer.assetBundleName != name)
            {
                name.Replace("\\", "/");
                importer.assetBundleName = name;
            } 
        }

    } 


你可能感兴趣的:(unity)