// Use this for initialization void Start() { //加载ab的 AssetBundle abData = AssetBundle.LoadFromFile(MPathUtils.ASSETBUNDLE_PATH + "/" + MPathUtils.ASSETBUNDLE_AB_DATA_NAME); TextAsset textAsset = abData.LoadAsset <TextAsset>("MAssetBundleConfig.bytes"); MemoryStream memoryStream = new MemoryStream(textAsset.bytes); BinaryFormatter binaryFormatter = new BinaryFormatter(); MAssetBundleConfig mAssetBundleConfig = (MAssetBundleConfig)binaryFormatter.Deserialize(memoryStream); memoryStream.Close(); string prefabPath = "Assets/Resources/UI/Prefabs/TestPrefab.prefab"; uint crc = MCrcHelper.GetCRC32(prefabPath); MAssetBundleBase mAssetBundleBase = null; for (int i = 0; i < mAssetBundleConfig.AssetBundleList.Count; i++) { if (mAssetBundleConfig.AssetBundleList[i].Crc == crc) { mAssetBundleBase = mAssetBundleConfig.AssetBundleList[i]; } } for (int i = 0; i < mAssetBundleBase.AbDependence.Count; i++) { AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + mAssetBundleBase.AbDependence[i]); } AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + mAssetBundleBase.AbName); GameObject @object = GameObject.Instantiate(assetBundle.LoadAsset <GameObject>(mAssetBundleBase.AssetName)); }
//数据导出 public static void WriteToData(Dictionary <string, string> resPathDic) { MAssetBundleConfig mAssetBundleConfig = new MAssetBundleConfig(); mAssetBundleConfig.AssetBundleList = new List <MAssetBundleBase>(); foreach (var item in resPathDic) { string nowPath = item.Key; MAssetBundleBase mAssetBundleBase = new MAssetBundleBase(); mAssetBundleBase.Path = nowPath; mAssetBundleBase.Crc = MCrcHelper.GetCRC32(nowPath); mAssetBundleBase.AbName = item.Value; mAssetBundleBase.AssetName = item.Key.Remove(0, nowPath.LastIndexOf("/") + 1); mAssetBundleBase.AbDependence = new List <string>(); //先获取到这个路径的所有依赖项的路径 然后遍历依赖项 string[] resDependences = AssetDatabase.GetDependencies(nowPath); for (int i = 0; i < resDependences.Length; i++) { string tempPath = resDependences[i]; //如果当前遍历路径==查询路径 或者 是脚本文件 if (tempPath == nowPath || tempPath.EndsWith(".cs")) { continue; } //然后根据上文传进来的Dic 判定每一个依赖路径所对应的AB名字 进行添加 string abName; if (resPathDic.TryGetValue(tempPath, out abName)) { if (abName == resPathDic[nowPath]) { continue; } if (!mAssetBundleBase.AbDependence.Contains(abName)) { mAssetBundleBase.AbDependence.Add(abName); } } } //数据插入 mAssetBundleConfig.AssetBundleList.Add(mAssetBundleBase); } //写入XML if (File.Exists(MPathUtils.ASSETBUNDLE_XML_PATH)) { File.Delete(MPathUtils.ASSETBUNDLE_XML_PATH); } FileStream fileStream = new FileStream(MPathUtils.ASSETBUNDLE_XML_PATH, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8); XmlSerializer xmlSerializer = new XmlSerializer(mAssetBundleConfig.GetType()); xmlSerializer.Serialize(streamWriter, mAssetBundleConfig); streamWriter.Close(); fileStream.Close(); //写入Bytes if (File.Exists(MPathUtils.ASSETBUNDLE_BYTES_PATH)) { File.Delete(MPathUtils.ASSETBUNDLE_BYTES_PATH); } FileStream bytesStresm = new FileStream(MPathUtils.ASSETBUNDLE_BYTES_PATH, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); BinaryFormatter binaryFormatter = new BinaryFormatter(); //写入二进制 不写入Path 优化Bytes大小 for (int i = 0; i < mAssetBundleConfig.AssetBundleList.Count; i++) { mAssetBundleConfig.AssetBundleList[i].Path = null; } binaryFormatter.Serialize(bytesStresm, mAssetBundleConfig); bytesStresm.Close(); }