public static void LoadPrefab(string path) { AssetBundle ab = AssetBundle.LoadFromFile(AssetBundleManager.BundleTargetPath + AssetBundleManager.m_ABConfigABName); TextAsset ta = ab.LoadAsset <TextAsset>("AssetBundleConfig"); MemoryStream ms = new MemoryStream(ta.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig ab_config = (AssetBundleConfig)bf.Deserialize(ms); ms.Close(); uint crc = Crc32.GetCrc32(path); AssetBundleBase ab_base = null; for (int i = 0; i < ab_config.ABList.Count; i++) { if (crc == ab_config.ABList[i].Crc) { ab_base = ab_config.ABList[i]; break; } } if (ab_base == null) { Debug.LogError("LoadAssetManager.LoadPrefab:预制不存在,path:" + path); return; } for (int i = 0; i < ab_base.ABDependce.Count; i++) { AssetBundle.LoadFromFile(AssetBundleManager.BundleTargetPath + "/" + ab_base.ABDependce[i]); } AssetBundle asset_bundle = AssetBundle.LoadFromFile(AssetBundleManager.BundleTargetPath + "/" + ab_base.ABName); GameObject.Instantiate(asset_bundle.LoadAsset <GameObject>(ab_base.ABName)); }
/// <summary> /// 读取AB包的配置 /// </summary> /// <returns></returns> public bool LoadAssetBundleConfig() { //编译器中streamingAssets目录不放东西 从外部文件夹读取 //所以返回false 否则读不到 #if UNITY_EDITOR if (!ResourceManager.Instance.m_LoadFormAssetBundle) { return(false); } #endif //游戏开始时加载 string path = BundleTargetPath + m_ABConfigABName;//Config的AB路径 AssetBundle ab_config = AssetBundle.LoadFromFile(path); //这个string参数不区分大小写 Mainfest的Asset文件 TextAsset ta = ab_config.LoadAsset <TextAsset>(m_ABConfigABName); if (ta == null) { Debug.LogError("AssetBundleManager.LoadAssetBundleConfig() not exist path:" + path); return(false); } //反序列化 MemoryStream ms = new MemoryStream(ta.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig config = (AssetBundleConfig)bf.Deserialize(ms); ms.Close(); //赋值ResourceItem for (int i = 0; i < config.ABList.Count; i++) { AssetBundleBase ab_base = config.ABList[i]; // ab_base.Print(); ResourceItem item = new ResourceItem(); item.m_Crc = ab_base.Crc; item.m_ABName = ab_base.ABName; item.m_AssetName = ab_base.AssetName; item.m_ABDependce = ab_base.ABDependce; if (m_ResourceItemDic.ContainsKey(item.m_Crc)) { Debug.LogError("AssetBundleManager.LoadAssetBundleConfig() Crc is exist ABName:" + ab_base.ABName + " AssetName:" + ab_base.AssetName); continue; } //加载的资源信息保存 m_ResourceItemDic.Add(item.m_Crc, item); } return(true); }
/// <summary> /// 参照カウンターの取得 /// </summary> /// <param name="manager">マネージャー</param> /// <returns>参照カウンター</returns> public static int GetReferenceCount(this AssetBundleBase assetBundle) { return(assetBundle.referenceCount); }
//将需要的配置生成xml 和 binary static void WriteData(Dictionary <string, string> path_dic) { AssetBundleConfig ab_config = new AssetBundleConfig(); ab_config.ABList = new List <AssetBundleBase>(); foreach (string path in path_dic.Keys) { //path在有效目录下 if (!LogTool.IsPathContains(path, m_VaildDir)) { continue; } AssetBundleBase ab_base = new AssetBundleBase(); ab_base.Path = path; ab_base.ABName = path_dic[path]; ab_base.AssetName = path.Remove(0, path.LastIndexOf('/') + 1); ab_base.Crc = Crc32.GetCrc32(path); ab_base.ABDependce = new List <string>(); string[] all_depend = AssetDatabase.GetDependencies(path); for (int i = 0; i < all_depend.Length; i++) { if (path == all_depend[i] || all_depend[i].EndsWith(".cs")) { continue; } string name; if (path_dic.TryGetValue(all_depend[i], out name)) { if (name == ab_base.ABName)//ABName已经添加过了 { continue; } if (!ab_base.ABDependce.Contains(name)) { ab_base.ABDependce.Add(name); } } } ab_config.ABList.Add(ab_base); } //写入xml string xml_path = Application.dataPath + "/Script/7-Frame/2-Bundle/AssetBundleConfig.xml"; if (File.Exists(xml_path)) { File.Delete(xml_path); } FileStream fs = new FileStream(xml_path, FileMode.Create); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); XmlSerializer xs = new XmlSerializer(ab_config.GetType()); xs.Serialize(sw, ab_config); sw.Close(); fs.Close(); //写入binary foreach (AssetBundleBase ab_base in ab_config.ABList) { ab_base.Path = "";//使用CRC计算出path,所以不需要path } fs = new FileStream(BINARY_PATH, FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, ab_config); fs.Close(); }