// 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)); } }
//写依赖包 private static void WriteData(Dictionary <string, string> assetPathDic) { AssetBundleLoadProfile assetBundleLoadProfile = new AssetBundleLoadProfile(); assetBundleLoadProfile.ABList = new List <ABBase>(); foreach (var item in assetPathDic) { ABBase aBBase = new ABBase { Path = item.Key, Crc = CRC32.GetCRC32(item.Key), AssetBundleName = item.Value, AssetName = item.Key.Remove(0, item.Key.LastIndexOf('/') + 1), DependentBundles = new List <string>() }; string[] dependencies = AssetDatabase.GetDependencies(aBBase.Path); for (int i = 0; i < dependencies.Length; i++) { string path = dependencies[i]; if (path == aBBase.Path || path.EndsWith(".cs")) { continue; } string assetBundleName; if (assetPathDic.TryGetValue(path, out assetBundleName)) { if (assetBundleName.Equals(item.Value)) { continue; } if (!aBBase.DependentBundles.Contains(assetBundleName)) { aBBase.DependentBundles.Add(assetBundleName); } } } assetBundleLoadProfile.ABList.Add(aBBase); } if (File.Exists(GetFrameProfile().ABLoadXmlPath)) { File.Delete(GetFrameProfile().ABLoadXmlPath); } FileStream fs = new FileStream(GetFrameProfile().ABLoadXmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); XmlSerializer xmlSerializer = new XmlSerializer(typeof(AssetBundleLoadProfile)); xmlSerializer.Serialize(fs, assetBundleLoadProfile); fs.Close(); assetBundleLoadProfile.ABList.ForEach(aBBase => { aBBase.Path = ""; }); FileStream fs2 = new FileStream(GetFrameProfile().ABLoadBytesPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite ); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs2, assetBundleLoadProfile); fs2.Close(); // AssetDatabase.Refresh(); }