Exemplo n.º 1
0
        //加载ABConfig配置表
        public bool LoadAssetBundleConfig()
        {
            m_resourceItemDict.Clear();

            string      configPath = GlobalConfigData.DependenceFile4AssetBundle;
            AssetBundle configAB   = AssetBundle.LoadFromFile(configPath);
            TextAsset   textAsset  = configAB.LoadAsset <TextAsset>(GlobalConfigData.DependenceFileName);

            if (textAsset == null)
            {
                Debug.LogError("AssetBundleConfig is not exists!");
                return(false);
            }
            MemoryStream      ms       = new MemoryStream(textAsset.bytes);
            BinaryFormatter   bf       = new BinaryFormatter();
            AssetBundleConfig abConfig = (AssetBundleConfig)bf.Deserialize(ms);

            ms.Close();

            for (int i = 0; i < abConfig.ABList.Count; i++)
            {
                ResourceItem item   = new ResourceItem();
                ABBase       abBase = abConfig.ABList[i];
                item.Crc          = abBase.Crc;
                item.AssetName    = abBase.AssetName;
                item.ABName       = abBase.ABName;
                item.DependABList = new List <string>(abBase.DependABList);
                if (m_resourceItemDict.ContainsKey(item.Crc) == false)
                {
                    m_resourceItemDict.Add(item.Crc, item);
                }
                else
                {
                    Debug.Log("重复的Crc, 资源名: " + item.AssetName + ", AB包名: " + item.ABName);
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        //制作依赖关系
        private static void MakeDependenceRelationShip()
        {
            string[] assetBundleNameAry      = AssetDatabase.GetAllAssetBundleNames(); //得到所有的AssetBundle的名字
            Dictionary <string, string> dict = new Dictionary <string, string>();

            for (int i = 0; i < assetBundleNameAry.Length; i++)
            {
                string[] assetBundleAllFiles = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleNameAry[i]); //得到某个AssetBundle下的所有被标记的文件
                for (int j = 0; j < assetBundleAllFiles.Length; j++)
                {
                    if (assetBundleAllFiles[j].EndsWith(".cs") == true || CanBeLoadByDynamic(assetBundleAllFiles[j]) == false) //生成依赖文件,我们只生成那些有可能会被实例化的资源,那些不可能被实例化的资源不需要写入依赖文件中
                    {
                        continue;
                    }
                    dict.Add(assetBundleAllFiles[j], assetBundleNameAry[i]); //这个文件和对应这个文件所在的AB包名
                }
            }

            //删除已经没有的AssetBundle包文件
            DirectoryInfo directoryInfo = new DirectoryInfo(GlobalConfigData.AssetBundleBuildTargetPath);

            FileInfo[] fileInfoAry = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
            foreach (FileInfo fileInfo in fileInfoAry)
            {
                bool isExists = false;
                foreach (string assetBundleName in assetBundleNameAry)
                {
                    if (assetBundleName == fileInfo.Name)
                    {
                        isExists = true;
                    }
                }
                if (isExists == false && File.Exists(fileInfo.FullName))
                {
                    Debug.Log(fileInfo.Name + "这个包已经不需要存在了, 删除中...");
                    File.Delete(fileInfo.FullName);
                }
            }

            //写依赖文件
            AssetBundleConfig config = new AssetBundleConfig();

            config.ABList = new List <ABBase>();
            foreach (string path in dict.Keys)
            {
                ABBase abBase = new ABBase();
                abBase.Path         = path;
                abBase.Crc          = Crc.StringToCRC32(path);
                abBase.ABName       = dict[path];
                abBase.AssetName    = path.Remove(0, path.LastIndexOf("/") + 1);
                abBase.DependABList = new List <string>();
                string[] resDependce = AssetDatabase.GetDependencies(path);
                for (int i = 0; i < resDependce.Length; i++)
                {
                    string tempPath = resDependce[i];
                    if (tempPath == path || path.EndsWith(".cs")) //排除对本身文件和.cs脚本文件
                    {
                        continue;
                    }
                    string abName = "";
                    if (dict.TryGetValue(tempPath, out abName))
                    {
                        if (abName == dict[path]) //排除对本身AB包的依赖
                        {
                            continue;
                        }
                        if (!abBase.DependABList.Contains(abName))
                        {
                            abBase.DependABList.Add(abName);
                        }
                    }
                }
                config.ABList.Add(abBase);
            }
            //写入xml
            if (File.Exists(GlobalConfigData.AssetBundleDependenceXmlPath))
            {
                File.Delete(GlobalConfigData.AssetBundleDependenceXmlPath);
            }
            FileStream fileStream = new FileStream(GlobalConfigData.AssetBundleDependenceXmlPath,
                                                   FileMode.Create,
                                                   FileAccess.ReadWrite,
                                                   FileShare.ReadWrite);
            StreamWriter  sw = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
            XmlSerializer xs = new XmlSerializer(config.GetType());

            xs.Serialize(sw, config);
            sw.Close();
            fileStream.Close();

            //写入二进制
            foreach (ABBase item in config.ABList)
            {
                item.Path = "";
            }
            if (File.Exists(GlobalConfigData.AssetBundleDependenceBytePath))
            {
                File.Delete(GlobalConfigData.AssetBundleDependenceBytePath);
            }
            fileStream = new FileStream(GlobalConfigData.AssetBundleDependenceBytePath,
                                        FileMode.Create,
                                        FileAccess.ReadWrite,
                                        FileShare.ReadWrite);
            BinaryFormatter bf = new BinaryFormatter();

            bf.Serialize(fileStream, config);
            fileStream.Close();
        }