/// <summary> /// 预加载所有AssetBundle资源 /// </summary> public void LoadAllAssetBundle() { abResourceDict.Clear(); AssetBundle ab = AssetBundle.LoadFromFile(ConstConfig.ASSETBUNDLEPATH + "/assetbundleconfig"); TextAsset textAsset = ab.LoadAsset <TextAsset>("AssetBundleConfig.bytes"); if (textAsset == null) { Debug.LogError("Asset丢失或未找到资源"); return; } MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); ABBundleConfig config = (ABBundleConfig)bf.Deserialize(stream); stream.Close(); for (int i = 0; i < config.ABList.Count; i++) { ABBase abBase = config.ABList[i]; ResourceItem item = new ResourceItem(); item.m_Crc = abBase.Crc; item.m_ABName = abBase.ABName; item.m_AssetName = abBase.AssetName; item.m_ABDependce = abBase.ABDependce; if (abResourceDict.ContainsKey(item.m_Crc)) { Debug.LogError("存在重复的Crc, AB包名:" + item.m_ABName + " 资源名:" + item.m_AssetName); } else { abResourceDict.Add(item.m_Crc, item); } } }
void TestLoad() { AssetBundle assetBundleconfig = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/assetbundleconfig"); TextAsset textAsset = assetBundleconfig.LoadAsset <TextAsset>("AssetBundleConfig"); MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig testserilize = (AssetBundleConfig)bf.Deserialize(stream); stream.Close(); string path = "Assets/Prefabs/Attack.prefab"; string crc = CRC32.GetCRC32(path).ToString(); ABBase abbase = null; for (int i = 0; i < testserilize.ABList.Count; i++) { if (testserilize.ABList[i].Crc == crc) { abbase = testserilize.ABList[i]; } } for (int i = 0; i < abbase.ABDependce.Count; i++) { AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abbase.ABDependce[i]); } AssetBundle assetbundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abbase.ABName); GameObject obj = GameObject.Instantiate(assetbundle.LoadAsset <GameObject>(abbase.AssetName)); }
void TestLoadAB() { //TextAsset textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/GameData/Data/ABData/AssetbundleConfig.bytes"); AssetBundle configAB = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/assetbundleconfig"); TextAsset textAsset = configAB.LoadAsset <TextAsset>("AssetBundleConfig"); MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig testSerilize = (AssetBundleConfig)bf.Deserialize(stream); stream.Close(); string path = "Assets/GameData/Prefabs/Attack.prefab"; uint crc = CRC32.GetCRC32(path); ABBase abBase = null; for (int i = 0; i < testSerilize.ABList.Count; i++) { if (testSerilize.ABList[i].Crc == crc) { abBase = testSerilize.ABList[i]; break; } } // 加载依赖ab包 for (int i = 0; i < abBase.ABDependce.Count; i++) { AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABDependce[i]); } AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABName); GameObject obj = GameObject.Instantiate(assetBundle.LoadAsset <GameObject>(abBase.AssetName)); }
public bool LoadAssetBundleConfig() { string configPath = Application.streamingAssetsPath + "/assetbundleconfig"; AssetBundle configAB = AssetBundle.LoadFromFile(configPath); TextAsset textAsset = configAB.LoadAsset <TextAsset>("assetbundleconfig"); if (textAsset == null) { Debug.Log("AssetBundleConfig is not exist."); return(false); } MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig config = (AssetBundleConfig)bf.Deserialize(stream); stream.Close(); for (int i = 0; i < config.ABList.Count; i++) { ABBase abBase = config.ABList[i]; ResourceItem item = new ResourceItem(); item.m_CRC = abBase.Crc; item.m_AssetName = abBase.AssetName; item.m_ABName = abBase.ABName; item.m_DependAssetBundle = abBase.ABDependce; if (m_ResourceItemDic.ContainsKey(item.m_CRC)) { Debug.Log("重复的CRC:" + item.m_AssetName + " ab包名:" + item.m_ABName); } else { m_ResourceItemDic.Add(item.m_CRC, item); } } return(true); }
private void TestLoadAB() { // AssetBundle configAB = AssetBundle.LoadFromFile(string.Format("{0}/RealFram/Data/ABData/AssetBundleConfig", Application.dataPath)); // TextAsset textAsset = configAB.LoadAsset<TextAsset>("AssetBundleConfig"); TextAsset textAsset = Resources.Load <TextAsset>("AssetBundleConfig"); AssetBundleConfig config = null; using (MemoryStream ms = new MemoryStream(textAsset.bytes)) { BinaryFormatter bf = new BinaryFormatter(); config = bf.Deserialize(ms) as AssetBundleConfig; } string path = "Assets/RealFram/ABTest/Cube.prefab"; uint crc = Crc32.GetCrc32(path); ABBase abBase = null; for (int i = 0; i < config.ABList.Count; i++) { if (config.ABList[i].Crc == crc) { abBase = config.ABList[i]; } } for (int i = 0; i < abBase.ABDependce.Count; i++) { AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABDependce[i]); } AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.dataPath + "/" + "Resources/" + abBase.ABName); GameObject obj = GameObject.Instantiate(assetBundle.LoadAsset <GameObject>(abBase.AssetName)); GameObject obj2 = GameObject.Instantiate(assetBundle.LoadAsset <GameObject>("cylinder")); }
private void Start() { AssetBundle abConfig = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/abconfig"); TextAsset textAsset = abConfig.LoadAsset <TextAsset>("AssetBundleConfig"); MemoryStream ms = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig config = (AssetBundleConfig)bf.Deserialize(ms); ms.Close(); string path = "Assets/GameData/Prefabs/Attack.prefab"; uint crc = CRC32.GetCRC32(path); ABBase abBase = null; for (int i = 0; i < config.ABList.Count; i++) { if (config.ABList[i].Crc == crc) { abBase = config.ABList[i]; } } for (int i = 0; i < abBase.ABDependce.Count; i++) // 加载依赖项 { AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABDependce[i]); } AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABName); // 加载 ab GameObject go = GameObject.Instantiate(ab.LoadAsset <GameObject>(abBase.AssetName)); // 实例化资源 }
void TestLoadAB() { AssetBundle configAB = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/assetbundleconfig"); TextAsset ta = configAB.LoadAsset <TextAsset>("AssetBundleConfig"); MemoryStream ms = new MemoryStream(ta.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig assetBundleConfig = (AssetBundleConfig)bf.Deserialize(ms); ms.Close(); string path = "Assets/GameData/Prefabs/Attack.prefab"; uint crc = CRC32.GetCRC32(path); ABBase aBBase = null; for (int i = 0; i < assetBundleConfig.ABBaseList.Count; i++) { if (assetBundleConfig.ABBaseList[i].Crc == crc) { aBBase = assetBundleConfig.ABBaseList[i]; } } //先加载预制体依赖项 for (int i = 0; i < aBBase.ABDependceList.Count; i++) { AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + aBBase.ABDependceList[i]); } //再加载预制体 AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + aBBase.ABName); GameObject obj = GameObject.Instantiate(assetBundle.LoadAsset <GameObject>(aBBase.ABName)); }
void Test() { string path = "Assets/AssetBundleConfig.bytes"; TextAsset t = AssetDatabase.LoadAssetAtPath <TextAsset>(path); MemoryStream ms = new MemoryStream(t.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig ab = (AssetBundleConfig)bf.Deserialize(ms); string path2 = "Assets/Prefabs/Models/Boss_Bruce/bruce.FBX"; uint crc = CRC32.GetCRC32(path2); ABBase abBase = null; for (int i = 0; i < ab.ABList.Count; ++i) { if (ab.ABList[i].Crc == crc) { abBase = ab.ABList[i]; continue; } } AssetBundle asset = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABName); Debug.Log(asset); GameObject obj = GameObject.Instantiate(asset.LoadAsset <GameObject>(abBase.AssetName)); }
void TestLoadAB() { AssetBundle asset = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/AssetBundleConfig"); TextAsset t = asset.LoadAsset <TextAsset>("AssetBundleConfig"); MemoryStream stream = new MemoryStream(t.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig abc = bf.Deserialize(stream) as AssetBundleConfig; stream.Close(); string path = "Assets/GameData/Prefabs/Attack.prefab"; uint crc = CRC32.GetCRC32(path); ABBase abBase = null; for (int i = 0; i < abc.ABList.Count; i++) { if (abc.ABList[i].Crc == crc) { abBase = abc.ABList[i]; } } for (int i = 0; i < abBase.ABDenpends.Count; i++) { AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABDenpends[i]); } AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABName); GameObject go = Instantiate(assetBundle.LoadAsset <GameObject>(abBase.AssetName), Vector3.forward, Quaternion.identity); }
// private void Load() // { // AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/attack"); // GameObject go = Instantiate(ab.LoadAsset<GameObject>("attack")); // Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>("Assets/GameData/Prefabs/Attack.prefab")); // } private void LoadAseetBundle() { AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/loadconfig"); MemoryStream ms = new MemoryStream(assetBundle.LoadAsset <TextAsset>("AssetBundleLoadConfig").bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleLoadProfile loadProfile = (AssetBundleLoadProfile)bf.Deserialize(ms); ms.Close(); string path = "Assets/GameData/Prefabs/Attack.prefab"; uint crc = CRC32.GetCRC32(path); ABBase abBase = null; for (int i = 0; i < loadProfile.ABList.Count; i++) { if (loadProfile.ABList[i].Crc == crc) { abBase = loadProfile.ABList[i]; } } for (int i = 0; i < abBase.DependentBundles.Count; i++) { AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.DependentBundles[i]); } if (abBase != null) { AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.AssetBundleName); GameObject go = Instantiate(ab.LoadAsset <GameObject>(abBase.AssetName)); } }
T LoadAsset <T>(string path) where T : UnityEngine.Object { string assetName = path.Remove(0, path.LastIndexOf("/") + 1); ABBase abBase = FindABBase(path); AssetBundle assetBundle = LoadAssetBundle(abBase); return(assetBundle.LoadAsset <T>(assetName)); }
/// <summary> /// 写入数据 /// </summary> /// <param name="resPathDic">key 为全路径,value为包名</param> static void WriteData(Dictionary <string, string> resPathDic) { //创建数据 AssetBundleConfig assetBundleConfig = new AssetBundleConfig(); assetBundleConfig.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("/") + 1); aBBase.ABDependce = new List <string>(); string[] resDependce = AssetDatabase.GetDependencies(path); for (int i = 0; i < resDependce.Length; i++) { string pathTemp = resDependce[i]; if (pathTemp == path || path.EndsWith(".cs")) { continue; } string abName = ""; if (resPathDic.TryGetValue(pathTemp, out abName)) { if (abName == resPathDic[path]) { continue; } if (!aBBase.ABDependce.Contains(abName)) { aBBase.ABDependce.Add(abName); } } } assetBundleConfig.ABList.Add(aBBase); } //写入XML string xmlPath = Application.dataPath + "/AssetbundleConfig.xml"; WriteReadData.CreateXML <AssetBundleConfig>(xmlPath, assetBundleConfig); //写入二进制 //二进制格式不需要Path数据 foreach (ABBase ab in assetBundleConfig.ABList) { ab.Path = ""; } //string binaryPath = BUNDLETARGETPATH + "/AssetbundleConfig.bytes"; string binaryPath = "Assets/GameData/Data/ABData/AssetBundleConfig.bytes"; WriteReadData.CreateBinary <AssetBundleConfig>(binaryPath, assetBundleConfig); }
/// <summary> /// 加载AssetBundleConfig /// </summary> /// <returns></returns> public bool LoadAssetBundleConfig() { // TextAsset textAsset = null; //#if UNITY_EDITOR // textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>(m_AssetConfigPath); //#endif // if (textAsset == null) // { // Debug.LogError("assetbundleConfig不存在!"+ m_AssetConfigPath); // return false; // } #if UNITY_EDITOR if (!ResourceManger.Instance.m_LoadFromAssetBundle) { return(false); } #endif m_ResourceItemDic.Clear(); string assetConfigPath = ABLoadpath + m_ABConfigABName; AssetBundle abConfigBundle = AssetBundle.LoadFromFile(assetConfigPath); TextAsset textAsset = abConfigBundle.LoadAsset <TextAsset>(m_ABConfigABName); if (textAsset == null) { Debug.LogError("assetbundleConfig不存在!"); return(false); } MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter formatter = new BinaryFormatter(); AssetBundleConfig config = formatter.Deserialize(stream) as AssetBundleConfig; stream.Close(); for (int i = 0; i < config.ABList.Count; i++) { ABBase ab = config.ABList[i]; ResourceItem item = new ResourceItem(); item.m_Crc = ab.Crc; item.m_AssetName = ab.AssetName; item.m_AssetBundleName = ab.ABName; item.m_DependAssetBundle = ab.ABDependce; if (m_ResourceItemDic.ContainsKey(item.m_Crc)) { Debug.LogError("重复的Crc 资源名:" + item.m_AssetName + "ab包名:" + item.m_AssetBundleName); } else { m_ResourceItemDic.Add(item.m_Crc, item); } } return(true); }
//写入配置表 static void WriteData(Dictionary <string, string> resPathDic) { //初始化数据 AssetBundleConfig config = new AssetBundleConfig(); config.ABList = new List <ABBase>(); foreach (string path in resPathDic.Keys) { if (!ValidPath(path)) { continue; //不是有效路径 } ABBase ab = new ABBase(); ab.ABName = resPathDic[path]; ab.Path = path; ab.AssetName = path.Remove(0, path.LastIndexOf("/") + 1); ab.Crc = Crc.GetCRC32(path); ab.ABDependce = new List <string>(); //查找关联 string[] depes = AssetDatabase.GetDependencies(path); foreach (string depe in depes) { if (depe.EndsWith(".cs") || depe == path) { continue; } //查看包名 string abName = ""; if (resPathDic.TryGetValue(depe, out abName)) { if (abName == ab.ABName) { continue; } else if (!ab.ABDependce.Contains(abName)) { ab.ABDependce.Add(abName); } } } config.ABList.Add(ab); } //写入xml WriteXml(config); //写入二进制 WriteBinary(config); }
/// <summary> /// 加载ab配置表 /// </summary> /// <returns></returns> public bool LoadAssetBundleConfig() { #if UNITY_EDITOR if (!ResourceManager.Instance.m_LoadFormAssetBundle) { return(false); } #endif m_ResouceItemDic.Clear(); string configPath = ABLoadPath + m_ABConfigABName; string hotABPath = HotPatchManager.Instance.ComputeABPath(m_ABConfigABName); configPath = string.IsNullOrEmpty(hotABPath) ? configPath : hotABPath; byte[] bytes = AES.AESFileByteDecrypt(configPath, "Ocean"); AssetBundle configAB = AssetBundle.LoadFromMemory(bytes); TextAsset textAsset = configAB.LoadAsset <TextAsset>(m_ABConfigABName); if (textAsset == null) { Debug.LogError("AssetBundleConfig is no exist!"); return(false); } MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig config = (AssetBundleConfig)bf.Deserialize(stream); stream.Close(); for (int i = 0; i < config.ABList.Count; i++) { ABBase abBase = config.ABList[i]; ResouceItem item = new ResouceItem(); item.m_Crc = abBase.Crc; item.m_AssetName = abBase.AssetName; item.m_ABName = abBase.ABName; item.m_DependAssetBundle = abBase.ABDependce; if (m_ResouceItemDic.ContainsKey(item.m_Crc)) { Debug.LogError("重复的Crc 资源名:" + item.m_AssetName + " ab包名:" + item.m_ABName); } else { m_ResouceItemDic.Add(item.m_Crc, item); } } return(true); }
/// <summary> /// 加载ab配置表 /// </summary> /// <returns></returns> public bool LoadAssetBundleConfig() { #if UNITY_EDITOR if (!ResourceManager.Instance.m_LoadFromAssetBundle) { return(false); } #endif m_ResouceItemDic.Clear(); string configPath = ABLoadPath + m_ABConfigABName; AssetBundle configAB = AssetBundle.LoadFromFile(configPath); TextAsset textAsset = configAB.LoadAsset <TextAsset>(m_ABConfigABName); if (textAsset == null) { Debug.LogError("AssetBundleConfig is no exist !"); return(false); } //解析 反序列化 MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig config = (AssetBundleConfig)bf.Deserialize(stream); stream.Close(); for (int i = 0; i < config.ABList.Count; i++) { ABBase abBase = config.ABList[i]; ResourceItem item = new ResourceItem(); item.m_Crc = abBase.Crc; item.m_AssetName = abBase.AssetName; item.m_ABName = abBase.ABName; item.m_DependAssetBundle = abBase.ABDependce; if (m_ResouceItemDic.ContainsKey(item.m_Crc)) { Debug.LogError("重复的CRC:资源名:" + item.m_ABName + " ab包名" + item.m_ABName); } else { m_ResouceItemDic.Add(item.m_Crc, item); } } return(true); }
//写入数据并生成配置表 static void WriteData(Dictionary <string, string> resPathDic) { AssetBundleConfig assetBundleConfig = new AssetBundleConfig(); assetBundleConfig.ABList = new List <ABBase>(); foreach (var key in resPathDic.Keys) { ABBase abBase = new ABBase(); abBase.Path = key; abBase.Crc = CRC32.GetCRC32(key); abBase.ABName = resPathDic[key]; abBase.AssetName = key.Remove(0, key.LastIndexOf("/") + 1); abBase.ABDependce = new List <string>(); abBase.ABDependce = m_AllPrefabDir[resPathDic[key]]; assetBundleConfig.ABList.Add(abBase); } //写入xml if (File.Exists(WriteXMLPath)) { File.Delete(WriteXMLPath); } FileStream fs = new FileStream(WriteXMLPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); XmlSerializer xs = new XmlSerializer(assetBundleConfig.GetType()); //实现序列化 xs.Serialize(sw, assetBundleConfig); sw.Close(); fs.Close(); //写入二进制 if (File.Exists(bytePath)) { File.Delete(bytePath); } fs = new FileStream(bytePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, assetBundleConfig); fs.Close(); }
public bool LoadAssetBundleConfig() { ABItemDic.Clear(); AssetBundle ab = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, Constants.ABConfigBinABName)); TextAsset ta = ab.LoadAsset <TextAsset>(Constants.ABConfigBinABName); MemoryStream ms = new MemoryStream(ta.bytes); BinaryFormatter formatter = new BinaryFormatter(); AssetBundleConfig abConfig = formatter.Deserialize(ms) as AssetBundleConfig; ms.Close(); for (int i = 0; i < abConfig.abList.Count; i++) { ABBase abBase = abConfig.abList[i]; ResourceManager.Instance.AddResourceItem(abBase.Crc, abBase.Path, abBase.ABName, abBase.DependencyABName); } return(true); }
//加载ab配置表 public bool LoadABConfig() { m_ResouceItemDic.Clear(); string configPath = ABLoadPath + m_ABConfigABName; AssetBundle configAB = AssetBundle.LoadFromFile(configPath); TextAsset textAsset = configAB.LoadAsset <TextAsset>(m_ABConfigABName); if (textAsset == null) { Debug.LogError("ABConfig is no exist!"); return(false); } AssetBundleConfig abCOnfig; using (MemoryStream stream = new MemoryStream(textAsset.bytes)){ BinaryFormatter bf = new BinaryFormatter(); abCOnfig = (AssetBundleConfig)bf.Deserialize(stream); stream.Close(); } for (int i = 0; i < abCOnfig.ABList.Count; i++) { ABBase aBase = abCOnfig.ABList[i]; SVResourceItem item = new SVResourceItem(); item.m_ABName = aBase.ABName; item.m_AssetName = aBase.AssetName; item.m_Crc = aBase.Crc; item.m_DependAssetBundle = aBase.ABDependce; // item.m_Guid = if (m_ResouceItemDic.ContainsKey(item.m_Crc)) { Debug.LogError("重复的Crc 资源名为:"); } else { m_ResouceItemDic.Add(item.m_Crc, item); } } return(true); }
/// <summary> /// 加载AB配置表,存储其中AB包及依赖信息到m_ResourceItemDic /// </summary> /// <returns>加载是否成功</returns> public bool LoadAssetBundleConfig() { string configPath = Application.streamingAssetsPath + "/assetbundleconfig"; AssetBundle configAB = AssetBundle.LoadFromFile(configPath); TextAsset textAsset = configAB.LoadAsset <TextAsset>("assetbundleconfig"); if (textAsset == null) { Debug.LogError("AssetBundleConfig is not exit!"); return(false); } //TODO;SerializerHelper.cs MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig abc = bf.Deserialize(stream) as AssetBundleConfig; stream.Close(); for (int i = 0; i < abc.ABList.Count; i++) { ABBase abBase = abc.ABList[i]; ResourceItem item = new ResourceItem(); item.m_Crc = abBase.Crc; item.m_ABName = abBase.ABName; item.m_AssetName = abBase.AssetName; item.m_DependAssetBundle = abBase.ABDenpends; if (m_ResourceItemDic.ContainsKey(item.m_Crc)) { Debug.LogError("重复的Crc:" + $"{item.m_AssetName} --- {item.m_ABName}"); } else { m_ResourceItemDic.Add(item.m_Crc, item); } } return(true); }
/// <summary> /// 加载配置表 /// </summary> protected internal bool LoadAssetBundleConfig() { m_ConfigItemDic.Clear(); string configPath = ABLoadPath + m_ABConfigABName; AssetBundle configAB = AssetBundle.LoadFromFile(configPath); TextAsset textAsset = configAB.LoadAsset <TextAsset>(m_ABConfigABName); if (textAsset == null) { Log.Error("AssetBundleConfig is no exist!"); return(false); } MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig config = (AssetBundleConfig)bf.Deserialize(stream); stream.Close(); for (int i = 0; i < config.ABList.Count; i++) { ABBase abBase = config.ABList[i]; ConfigItem item = new ConfigItem(); item.Crc = abBase.Crc; item.AssetName = abBase.AssetName; item.ABName = abBase.ABName; item.DependAssetBundle = abBase.ABDependce; if (m_ConfigItemDic.ContainsKey(item.Crc)) { Log.Error("重复的Crc 资源名:" + item.AssetName + " ab包名:" + item.ABName); } else { m_ConfigItemDic.Add(item.Crc, item); } } return(true); }
AssetBundle LoadAssetBundle(ABBase abBase) { if (_assetBundles.ContainsKey(abBase.ABName)) { return(_assetBundles[abBase.ABName]); } for (int i = 0; i < abBase.ABDependce.Count; i++) { if (_assetBundles.ContainsKey(abBase.ABDependce[i])) { continue; } string bundlePath = Application.streamingAssetsPath + "/" + abBase.ABDependce[i]; AssetBundle ab = AssetBundle.LoadFromFile(bundlePath); _assetBundles.Add(abBase.ABDependce[i], ab); } AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABName); _assetBundles.Add(abBase.ABName, assetBundle); return(assetBundle); }
/// <summary> /// 加载AB配置表 /// </summary> /// <returns>是否加载成功</returns> public bool LoadAssetBundleConfig() { _resItemDic.Clear(); string configPath = Application.streamingAssetsPath + "/assetbundleconfig"; AssetBundle configAB = AssetBundle.LoadFromFile(configPath); TextAsset textAsset = configAB.LoadAsset <TextAsset>("assetbundleconfig"); if (textAsset == null) { Debug.LogError("AssetBundleConfig is noe exist!"); return(false); } //反序列化 MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig config = bf.Deserialize(stream) as AssetBundleConfig; stream.Close(); for (int i = 0; i < config.ABList.Count; i++) { ABBase abBase = config.ABList[i]; ResourceItem item = new ResourceItem(); item._crc = abBase.Crc; item._assetName = abBase.AssetName; item._abName = abBase.ABName; item._dependAssetBundle = abBase.ABDependce; if (_resItemDic.ContainsKey(item._crc)) { Debug.LogError("重复的CRC 资源名:" + item._assetName + " AB包名:" + item._abName); } else { _resItemDic.Add(item._crc, item); } } return(true); }
/// <summary> /// 加载AB配置表 /// </summary> /// <returns></returns> public bool LoadAssetBundleConfig() { m_ResourceItemDic.Clear(); m_AssetBundleItemDic.Clear(); string configPath = "Assets/AssetBundles/assetbundleconfig"; AssetBundle configAB = AssetBundle.LoadFromFile(configPath); TextAsset textAsset = configAB.LoadAsset <TextAsset>("AssetbundleConfig"); if (textAsset == null) { Debug.LogError("AssetBundleConfig is no exit!"); return(false); } AssetBundleConfig assetBundleConfig = WriteReadData.ReadBinary <AssetBundleConfig>(textAsset); for (int i = 0; i < assetBundleConfig.ABList.Count; i++) { ABBase temp = assetBundleConfig.ABList[i]; ResourceItem item = new ResourceItem(); item.m_Crc = temp.Crc; item.m_AssetName = temp.AssetName; item.m_ABName = temp.ABName; item.m_DependAssetBundle = temp.ABDependce; item.m_FullPath = temp.Path; if (m_ResourceItemDic.ContainsKey(item.m_Crc)) { Debug.LogError("重复的Crc 资源名:" + item.m_AssetName + "ab包名:" + item.m_ABName); } else { m_ResourceItemDic.Add(item.m_Crc, item); } } return(true); }
/// <summary> /// 加载 AssetBundle 配置表 /// </summary> /// <returns></returns> public bool LoadAssetBundleConfig() { resourceItemDic.Clear(); string configPath = Application.streamingAssetsPath + "/abconfig"; AssetBundle abConfig = AssetBundle.LoadFromFile(configPath); TextAsset textAsset = abConfig.LoadAsset <TextAsset>("AssetBundleConfig"); if (textAsset == null) { Debug.LogError("AssetBundle is no exist!"); return(false); } MemoryStream ms = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig config = (AssetBundleConfig)bf.Deserialize(ms); ms.Close(); // 添加到字典里 for (int i = 0; i < config.ABList.Count; i++) { ABBase abBase = config.ABList[i]; ResourceItem item = new ResourceItem(); item.crc = abBase.Crc; item.assetName = abBase.AssetName; item.abName = abBase.ABName; item.dependAB = abBase.ABDependce; if (resourceItemDic.ContainsKey(item.crc)) { Debug.LogError("重复的 crc 资源名:" + item.assetName + ",ab 包名:" + item.abName); } else { resourceItemDic.Add(item.crc, item); } } return(true); }
// Update is called once per frame void LoadAssetBundle() { //加载ab包信息配置列表 AssetBundle configAB = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/assetbundleconfig"); TextAsset textAsset = configAB.LoadAsset <TextAsset>("AssetBundleConfig"); Debug.Log(textAsset.bytes.Length); MemoryStream stream = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig abc = bf.Deserialize(stream) as AssetBundleConfig; stream.Close(); string path = "Assets/HSJExample/AssetBundle/Prefabs/Giraffe.prefab"; uint crc = Crc32.GetCRC32(path); Debug.Log(crc); ABBase abBase = null; for (int i = 0; i < abc.ABList.Count; i++) { Debug.Log(abc.ABList[i].Crc + abc.ABList[i].Path); if (abc.ABList[i].Crc == crc) { abBase = abc.ABList[i]; } } for (int i = 0; i < abBase.AbDependce.Count; i++) { AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.AbDependce[i]); } AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABName); GameObject obj = Instantiate(assetBundle.LoadAsset <GameObject>("Giraffe")); }
static void WriteData(Dictionary <string, string> resPathDic) { AssetBundleConfig config = new AssetBundleConfig(); config.ABList = new List <ABBase>(); foreach (var path in resPathDic.Keys) { //if (!ValidPath(path))//是不是shader sound prefab下的资源 不是的话就不管 // continue; ABBase abBase = new ABBase(); abBase.Path = path; abBase.Crc = Crc32.GetCrc32(path); abBase.ABName = resPathDic[path]; abBase.AssetName = path.Remove(0, path.LastIndexOf("/") + 1);//资源的名称 abBase.ABDependence = new List <string>(); string[] resDependece = AssetDatabase.GetDependencies(path); for (int i = 0; i < resDependece.Length; i++) { string tempPath = resDependece[i]; if (tempPath == path || path.EndsWith(".cs")) { continue; } string abName = ""; if (resPathDic.TryGetValue(tempPath, out abName)) { if (abName == resPathDic[path]) { continue; } if (!abBase.ABDependence.Contains(abName)) { abBase.ABDependence.Add(abName); } } } config.ABList.Add(abBase); } //写入xml string xmlPath = Application.dataPath + "/AssetBundleConfig.xml"; //SerializeUtil.XMLSerialize(xmlPath, config); 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(); ////写入二进制 foreach (ABBase abBase in config.ABList) { abBase.Path = ""; } FileStream fs = new FileStream("Assets/GameData/Data/ABData/AssetBundleConfig.bytes", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); fs.Seek(0, SeekOrigin.Begin); fs.SetLength(0); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, config); fs.Close(); //AssetDatabase.Refresh(); //SetABName("assetbundleconfig", ABBYTEPATH); }
public static void SVGenerateConfigeFiles() { // 生成abbundleConfige.xml 及 二进制文件 string[] bundleNames = AssetDatabase.GetAllAssetBundleNames(); // Debug.LogWarning("bundleName: " + bundleNames.Length); Dictionary <string, string> resList = new Dictionary <string, string> (); AssetBundleConfig config = new AssetBundleConfig(); config.ABList = new List <ABBase>(); foreach (string name in bundleNames) { string[] pathNames = AssetDatabase.GetAssetPathsFromAssetBundle(name); foreach (string path in pathNames) { if (!ValidPath(path)) { continue; } resList.Add(path, name); } } foreach (string path in resList.Keys) { ABBase aBase = new ABBase(); List <string> temDepends = new List <string>(); aBase.ABDependce = temDepends; aBase.ABName = resList[path]; aBase.AssetName = path.Remove(0, path.LastIndexOf("/") + 1); aBase.Crc = Crc32.GetCrc32(path); aBase.Path = path; string[] depends = AssetDatabase.GetDependencies(path); foreach (string bundlePath in depends) { if (bundlePath.EndsWith(".cs")) { continue; } if (resList.TryGetValue(bundlePath, out string temBundleName)) { if (aBase.ABName == temBundleName) { continue; } temDepends.Add(temBundleName); } } config.ABList.Add(aBase); } //写入xml string xmlPath = Application.dataPath + "/AssetbundleConfig.xml"; if (File.Exists(xmlPath)) { File.Delete(xmlPath); } using (FileStream fileStream = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite)) { using (StreamWriter sr = new StreamWriter(fileStream, Encoding.UTF8)) { XmlSerializer xmlSer = new XmlSerializer(typeof(AssetBundleConfig)); xmlSer.Serialize(sr, config); sr.Close(); fileStream.Close(); } } //写入二进制 foreach (ABBase abBase in config.ABList) { abBase.Path = ""; } using (FileStream fileStream = new FileStream(ABBYTEPATH, FileMode.Create, FileAccess.ReadWrite)) { fileStream.Seek(0, SeekOrigin.Begin); fileStream.SetLength(0); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fileStream, config); fileStream.Close(); } SetABName("assetbundleconfig", ABBYTEPATH); Debug.LogWarning("generate configfile success"); }
/// <summary> /// 创建配置表 /// </summary> static void CreateConfigTable(Dictionary <string, string> resultPathDic) { ABBundleConfig bundleConfig = new ABBundleConfig(); bundleConfig.ABList = new List <ABBase>(); foreach (string path in resultPathDic.Keys) { if (!ValidPath(path)) { continue; } ABBase abBase = new ABBase(); abBase.Path = path; abBase.Crc = CRC32.GetCRC32(path); abBase.ABName = resultPathDic[path]; abBase.AssetName = path.Remove(0, path.LastIndexOf("/") + 1); //资源名是全路径名去掉最后一个'/'前面的字符串 abBase.ABDependce = new List <string>(); string[] allDependce = AssetDatabase.GetDependencies(path); for (int i = 0; i < allDependce.Length; i++) { string tempPath = allDependce[i]; if (tempPath == path || path.EndsWith(".cs")) { continue; } string abName = ""; if (resultPathDic.TryGetValue(tempPath, out abName)) { if (abName == resultPathDic[path]) { continue; } if (!abBase.ABDependce.Contains(abName)) { abBase.ABDependce.Add(abName); } } } bundleConfig.ABList.Add(abBase); } //写入xml string xmlPath = Application.dataPath + "/AssetBundleConfig.xml"; if (File.Exists(xmlPath)) { File.Delete(xmlPath); } FileStream fs1 = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter sw1 = new StreamWriter(fs1, System.Text.Encoding.UTF8); XmlSerializer xmlSerializer = new XmlSerializer(bundleConfig.GetType()); xmlSerializer.Serialize(sw1, bundleConfig); sw1.Close(); fs1.Close(); //写入二进制 if (File.Exists(ABCONFIGBINNERPATH)) { File.Delete(ABCONFIGBINNERPATH); } FileStream fs2 = new FileStream(ABCONFIGBINNERPATH, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); BinaryFormatter bf2 = new BinaryFormatter(); bf2.Serialize(fs2, bundleConfig); fs2.Close(); AssetDatabase.Refresh(); SetABName("assetbundleconfig", ABCONFIGBINNERPATH); }
/// <summary> /// 写入数据 /// </summary> static void WriteData(Dictionary <string, string> resPathDic) { AssetBundleConfig config = new AssetBundleConfig(); config.ABList = new List <ABBase>(); foreach (string path in resPathDic.Keys) { ABBase abBase = new ABBase { Path = path, Crc = Crc32.GetCrc32(path), ABName = resPathDic[path], AssetName = path.Remove(0, path.LastIndexOf('/') + 1), ABDependence = new List <string>() }; string[] resDependency = AssetDatabase.GetDependencies(path); for (int i = 0; i < resDependency.Length; i++) { string tempPath = resDependency[i]; if (tempPath == path || path.EndsWith(".cs")) { continue; } if (resPathDic.TryGetValue(tempPath, out var abName)) { if (abName == resPathDic[path]) { continue; } if (!abBase.ABDependence.Contains(abName)) { abBase.ABDependence.Add(abName); } } } config.ABList.Add(abBase); } //写入xml string xmlPath = Application.dataPath + "/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(); //写入二进制 foreach (ABBase abBase in config.ABList) { abBase.Path = ""; } FileStream fs = new FileStream(ABByteName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); fs.Seek(0, SeekOrigin.Begin); fs.SetLength(0); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, config); fs.Close(); AssetDatabase.Refresh(); SetABName("assetbundleconfig", ABByteName); }