1. 生成 AssetBundle 文件
Editor 文件夹下添加代码:
1 [MenuItem("Assets/Build AssetBundles")] 2 static void BuildAllAssetBundles() 3 { 4 string assetBundleDirectory = "Assets/AssetBundles"; 5 if(!Directory.Exists(assetBundleDirectory) 6 { 7 Directory.CreateDirectory(assetBundleDirectory); 8 } 9 BuildPipeline.BuildAssetBundles(assetBundleDirectory, 10 BuildAssetBundleOptions.None, BuildTarget.Standalone); 11 }
2. 划分文件夹
指定 AssetBundle 时文件夹之间用 / 分开。
3. 加载资源
1 function Start() { 2 var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle")); 3 if (myLoadedAssetBundle == null) { 4 Debug.Log("Failed to load AssetBundle!"); 5 return; 6 } 7 var prefab = myLoadedAssetBundle.LoadAsset.<GameObject>("MyObject"); 8 Instantiate(prefab);}
4. AssetBundle 分组策略
4. AssetBundle 的使用
5. AssetBundleManifest 使用
1 AssetBundle assetBundle = AssetBundle.LoadFromFile(manifestFilePath); 2 AssetBundleManifest manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); 3 string[] dependencies = manifest.GetAllDependencies("assetBundle"); //Pass the name of the bundle you want the dependencies for. 4 foreach(string dependency in dependencies) 5 { 6 AssetBundle.LoadFromFile(Path.Combine(assetBundlePath, dependency)); 7 }
6. AssetBundle 卸载
AssetBundle.Unload(bool);
7. AssetBundle 浏览工具
Unity Asset Bundle Browser tool。