Ejemplo n.º 1
0
        public override string ToString()
        {
            string content = "";

            for (int i = 0; i < ABList.Count; i++)
            {
                ABBase abbase = ABList[i];
                content += abbase.Path + ", ";
                content += abbase.Crc + ", ";
                content += abbase.ABName + ", ";
                content += abbase.AssetName + ", ";
                content += "Depend: ";
                for (int j = 0; j < abbase.DependABList.Count; j++)
                {
                    content += abbase.DependABList[j] + ", ";
                }
            }
            return(content);
        }
        //读取资源依赖文件, 初始化所有资源都一一对应的ResourceItem类对象
        public bool LoadAssetBundleConfig()
        {
            m_resourceItemDict.Clear();

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

            if (textAsset == null)
            {
                Debug.Log("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);
        }
Ejemplo n.º 3
0
        //制作依赖关系
        private static void MakeDependenceRelationShip()
        {
            string[] assetBundleNameAry         = AssetDatabase.GetAllAssetBundleNames(); //得到所有的AssetBundle的名字
            Dictionary <string, string> dict    = new Dictionary <string, string>();      //记录需要被加载的资源
            Dictionary <string, string> allDict = 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") == false)
                    {
                        allDict.Add(assetBundleAllFiles[j], assetBundleNameAry[i]);
                    }
                    if (assetBundleAllFiles[j].EndsWith(".cs") == true || CanBeLoadByDynamic(assetBundleAllFiles[j]) == false) //生成依赖文件,我们只生成那些有可能会被实例化的资源,那些不可能被实例化的资源不需要写入依赖文件中
                    {
                        continue;
                    }
                    dict.Add(assetBundleAllFiles[j], assetBundleNameAry[i]); //这个文件Assets/相对路径和对应这个文件所在的AB包名
                }
            }

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

            FileInfo[] fileInfoAry = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
            foreach (FileInfo fileInfo in fileInfoAry)
            {
                bool isExists = false;
                foreach (string assetBundleName in assetBundleNameAry)
                {
                    if (assetBundleName == fileInfo.Name) //AssetBundle包名和打包时标记名一致
                    {
                        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          = CrcHelper.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 (allDict.TryGetValue(tempPath, out abName))
                    {
                        if (abName == allDict[path])
                        {
                            continue;
                        }
                        if (!abBase.DependABList.Contains(abName))
                        {
                            abBase.DependABList.Add(abName);
                        }
                    }
                }
                config.ABList.Add(abBase);
            }
            //写入xml
            if (File.Exists(ABPathConfig.AssetBundleDependenceXmlPath))
            {
                File.Delete(ABPathConfig.AssetBundleDependenceXmlPath);
            }
            FileStream fileStream = new FileStream(ABPathConfig.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(ABPathConfig.AssetBundleDependenceBytePath))
            {
                File.Delete(ABPathConfig.AssetBundleDependenceBytePath);
            }
            fileStream = new FileStream(ABPathConfig.AssetBundleDependenceBytePath,
                                        FileMode.Create,
                                        FileAccess.ReadWrite,
                                        FileShare.ReadWrite);
            BinaryFormatter bf = new BinaryFormatter();

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