static void CreateXML(AssetBundleConfig config, PackageABType packageABType) { //写入xml string xmlPath = ""; if (packageABType == PackageABType.PhoneAB) { xmlPath = EditorAssetPath.ABInfoPath + "/AssetbundleConfig.xml"; } else { xmlPath = EditorAssetPath.ABInfoPath + buildTarget.ToString() + "/AssetbundleConfig.xml"; } if (File.Exists(xmlPath)) { File.Delete(xmlPath); } FileStream fileStream = new FileStream(xmlPath, 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(); }
public void CacheABConfig(AssetBundleConfig config) { mResConfigItem.Clear(); for (int i = 0; i < config.ABList.Count; i++) { ABBase abBase = config.ABList[i]; //使用中间类进行保存 ResItem item = new ResItem(); item.mCrc = abBase.Crc; item.mResPath = abBase.Path; item.ABName = abBase.ABName; item.DependAssetBundle = abBase.ABDependce; if (mResConfigItem.ContainsKey(item.mCrc)) { AFLogger.e("重复的Crc:" + item.mCrc + " ab包名:" + item.ABName); } else { mResConfigItem.Add(item.mCrc, item); } } }
//写入数据 //resPathDic : 路径对应的AB包 static void WriteData(Dictionary <string, string> resPathDic, ConfigWritingMode configWritingMode, PackageABType packageABType) { AssetBundleConfig config = new AssetBundleConfig(); config.ABList = new List <ABBase>(); foreach (string path in resPathDic.Keys) { //进行赋值 ABBase abBase = new ABBase(); abBase.Path = path; abBase.Crc = Crc32.GetCrc32(path); abBase.ABName = resPathDic[path]; abBase.AssetName = path.Remove(0, path.LastIndexOf("/", System.StringComparison.Ordinal) + 1); abBase.ABDependce = 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", System.StringComparison.Ordinal)) { continue; } string abName = ""; //所依赖的资源是不是在其他AB包里面 if (resPathDic.TryGetValue(tempPath, out abName)) { if (abName == resPathDic[path]) { continue; } //判断是否已经添加到aBBase包里面 if (!abBase.ABDependce.Contains(abName)) { abBase.ABDependce.Add(abName); } } } config.ABList.Add(abBase); } switch (configWritingMode) { case ConfigWritingMode.TXT: string txtPath = EditorAssetPath.ABInfoPath + buildTarget.ToString() + "/AssetbundleConfig.txt"; if (File.Exists(txtPath)) { File.Delete(txtPath); } FileStream fileStream1 = new FileStream(txtPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter sw1 = new StreamWriter(fileStream1, System.Text.Encoding.UTF8); string configJson = SerializeHelper.ToJson(config); sw1.Write(configJson); sw1.Close(); fileStream1.Close(); break; case ConfigWritingMode.XML: CreateXML(config, packageABType); break; case ConfigWritingMode.Binary: foreach (ABBase abBase in config.ABList) { abBase.Path = ""; } string BinaryPath = "Assets/AssetbundleConfig.bytes"; FileStream fs = new FileStream(PathTool.ProjectPath + BinaryPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); fs.Seek(0, SeekOrigin.Begin); fs.SetLength(0); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, config); fs.Close(); CreateXML(config, packageABType); AssetDatabase.Refresh(); SetABName("assetbundleconfig", BinaryPath); break; } }
/// <summary> /// 加载Androidmanifest /// </summary> /// <returns></returns> public bool LoadAssetManifest() { AssetBundleConfig config = null; switch (ABConfig.configWritingMode) { case ConfigWritingMode.Binary: if (FileHelper.JudgeFilePathExit(GetABConfigLocalPath())) { if (configAB != null) { configAB.Unload(false); } configAB = AssetBundle.LoadFromFile(GetABConfigLocalPath()); TextAsset textAsset = configAB.LoadAsset <TextAsset>(ABConfigName); if (textAsset == null) { Debug.LogError("AssetBundleConfig is no exist!"); return(false); } //反序列化,得到打包的信息 MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); config = (AssetBundleConfig)bf.Deserialize(stream); stream.Close(); } else { AFLogger.e("AssetbundleConfig文件不存在"); return(false); } break; case ConfigWritingMode.TXT: string abJson = ""; if (ABConfig.ABResLoadfrom == ABResLoadFrom.PersistentDataPathAB) { abJson = FileHelper.ReadTxtToStr(GetABConfigLocalPath()); if (abJson.Equals("")) { AFLogger.e("AssetbundleConfig文件不存在或者内容为空"); return(false); } } else if (ABConfig.ABResLoadfrom == ABResLoadFrom.StreamingAssetAB) { string abConfigPath = "AF-ABForLocal/AF-InfoFile" + GetVersionStr() + "/" + GetStrByPlatform() + ABConfigName; TextAsset textAsset = ResManager.Instance.LoadSync(ResLoadInfo.Allocate(ResFromType.ResourcesRes, abConfigPath, false)) as TextAsset; abJson = textAsset.text; } config = SerializeHelper.FromJson <AssetBundleConfig>(abJson); break; case ConfigWritingMode.XML: XmlDocument xml = new XmlDocument(); MemoryStream ms = null; if (ABConfig.ABResLoadfrom == ABResLoadFrom.PersistentDataPathAB) { xml.Load(GetABConfigLocalPath()); } else { string abConfigPath = "AF-ABForLocal/AF-InfoFile" + GetVersionStr() + "/" + GetStrByPlatform() + ABConfigName; TextAsset textAsset = ResManager.Instance.LoadSync(ResLoadInfo.Allocate(ResFromType.ResourcesRes, abConfigPath, false)) as TextAsset; //由于编码问题,要设置编码方式 byte[] encodeString = System.Text.Encoding.UTF8.GetBytes(textAsset.text); //以流的形式来读取 ms = new MemoryStream(encodeString); xml.Load(ms); } if (xml == null) { AFLogger.e("AssetbundleConfig文件不存在或者内容为空"); return(false); } XmlSerializer xmldes = new XmlSerializer(typeof(AssetBundleConfig)); StringReader sr = new StringReader(xml.InnerXml); config = (AssetBundleConfig)xmldes.Deserialize(sr); if (ms != null) { ms.Close(); ms.Dispose(); } if (sr != null) { sr.Close(); sr.Dispose(); } break; } ResManager.Instance.CacheABConfig(config); return(true); }