void TestLoadAB() { AssetBundle configAB = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/assetbundleconfig"); TextAsset textAsset = configAB.LoadAsset <TextAsset>("AssetbundleConfig"); MemoryStream ms = new MemoryStream(textAsset.bytes); BinaryFormatter bf = new BinaryFormatter(); AssetBundleConfig test = (AssetBundleConfig)bf.Deserialize(ms); ms.Close(); string path = "Assets/GameData/Prefabs/Attack.prefab"; int crc = CRC32.GetCRC32(path); ABBase aBBase = null; for (int i = 0; i < test.ABList.Count; i++) { if (test.ABList[i].Crc == crc) { aBBase = test.ABList[i]; } } for (int i = 0; i < aBBase.ABDepends.Count; i++) { AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + aBBase.ABDepends[i]); } AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + aBBase.ABName); GameObject obj2 = Instantiate(assetBundle.LoadAsset <GameObject>(aBBase.AssetName)); }
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(); aBBase.Path = path; aBBase.Crc = CRC32.GetCRC32(path); aBBase.ABName = resPathDic[path]; aBBase.AssetName = path.Remove(0, path.LastIndexOf("/", System.StringComparison.Ordinal) + 1); aBBase.ABDepends = new List <string>(); string[] resDependence = AssetDatabase.GetDependencies(path); for (int i = 0; i < resDependence.Length; i++) { string tempPath = resDependence[i]; if (tempPath == path || path.EndsWith(".cs", System.StringComparison.Ordinal)) { continue; } string abName = ""; if (resPathDic.TryGetValue(tempPath, out abName)) { if (abName == resPathDic[path]) { continue; } if (!aBBase.ABDepends.Contains(abName)) { aBBase.ABDepends.Add(abName); } } } config.ABList.Add(aBBase); } // 写入XML string xmlPath = "Assets/GameData/Data/ABData/AssetbundleConfig.xml"; if (File.Exists(xmlPath)) { File.Delete(xmlPath); } FileStream fs = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); XmlSerializer xs = new XmlSerializer(config.GetType()); xs.Serialize(sw, config); sw.Close(); fs.Close(); // 写入二进制文件 foreach (ABBase abBase in config.ABList) { abBase.Path = ""; } string bytePath = "Assets/GameData/Data/ABData/AssetbundleConfig.bytes"; FileStream fs2 = new FileStream(bytePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs2, config); fs2.Close(); }