1
|
BuildPipeline.BuildAssetBundles(Path, BuildAssetBundleOptions, BuildTarget);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
using System.IO; using UnityEditor;
public class CreateAssetBundles { [MenuItem("Assets/Build AssetBundles")] static void BuildAllAssetBundles() { string abPath = "Assets/AssetBundles"; if (!Directory.Exists(abPath)) { Directory.CreateDirectory(abPath); } BuildPipeline.BuildAssetBundles(abPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); } }
|
1 2 3 4
|
AssetBundleCreateRequest LoadFromMemoryAsync(byte[] binary, uint crc); AssetBundle LoadFromMemory(byte[] binary, uint crc);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine;
public class LoadFromFile : MonoBehaviour { private string path = "Assets/AssetBundles/scene/cubewall"; private void Start() { } #region LoadFromMemory方式 IEnumerator LoadFromMemoryAsync() { AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path)); yield return request; AssetBundle ab = request.assetBundle; if (ab == null) { Debug.Log("加载失败"); } else { GameObject obj = ab.LoadAsset<GameObject>("Cube"); Instantiate(obj); } } private void LoadFromMemory() { AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path)); if (ab == null) { Debug.Log("加载失败"); } else { GameObject obj = ab.LoadAsset<GameObject>("Cube"); Instantiate(obj); } } #endregion }
|
1 2 3 4 5
|
AssetBundleCreateRequest LoadFromFileAsync(string path, uint crc, ulong offset); AssetBundle LoadFromFile
(string path, uint crc, ulong offset);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine;
public class LoadFromFile : MonoBehaviour { private string path = "Assets/AssetBundles/scene/cubewall"; private void Start() { } #region LoadFromFile方式 private void LoadFromFileFunc() { AssetBundle ab = AssetBundle.LoadFromFile(path); if (ab == null) { Debug.Log("加载失败"); } else { GameObject obj = ab.LoadAsset<GameObject>("Cube"); Instantiate(obj); } } IEnumerator LoadFromFileAsync() { AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path); yield return request; AssetBundle ab = request.assetBundle; if (ab == null) { Debug.Log("加载失败"); } else { GameObject obj = ab.LoadAsset<GameObject>("Cube"); Instantiate(obj); } } #endregion }
|
A从远程服务器下载 AssetBundle 或加载本地 AssetBundle 非常有用(将弃用)
从远程加载 AssetBundle 将自动缓存 AssetBundle:
若 AssetBundle 被压缩,则将启动工作线程来解压缩包并将其写入缓存
若捆绑包被解压缩并缓存,它就会像 AssetBundle.LoadFromFile 一样加载
1 2 3 4
|
public static WWW LoadFromCacheOrDownload(string url, int version, uint crc);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine;
public class LoadFromFile : MonoBehaviour { private string path = "Assets/AssetBundles/scene/cubewall"; private void Start() { StartCoroutine("WWWWLoadFromLocal"); } #region WWW方式 IEnumerator WWWWLoadFromLocal() { while (!Caching.ready) { yield return null; } WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/scene/cubewall", 1); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log(www.error); yield break; } AssetBundle ab = www.assetBundle; if (ab == null) { Debug.Log("加载失败"); } else { GameObject obj = ab.LoadAsset<GameObject>("Cube"); Instantiate(obj); } } #endregion }
|
1 2 3 4
|
UnityWebRequest UnityWebRequestAssetBundle.GetAssetBundle(Uri uri, uint version, uint crc);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Networking;
public class LoadFromFile : MonoBehaviour { private string path = "Assets/AssetBundles/scene/cubewall"; private void Start() { StartCoroutine("UnityWebRequestFunc"); } #region UnityWebRequest方式 IEnumerator UnityWebRequestFunc() {
string uri = @"http://localhost/AssetBundles/scene/cubewall"; UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri, 0); yield return request.SendWebRequest(); AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request); if (ab == null) { Debug.Log("加载失败"); } else { GameObject obj = ab.LoadAsset<GameObject>("Cube"); Instantiate(obj); } } #endregion }
|
1
|
GameObject obj = ab.LoadAsset<GameObject>("物体名字");
|
1 2 3 4 5 6
|
object[] objArray = ab.LoadAllAssets(); foreach(object o in objArray) { Instantiate(o as GameObject); }
|
1 2 3 4
|
AssetBundleRequest ab_request = ab.LoadAssetAsync<GameObject>("Cube_01"); yield return ab_request; var obj = ab_request.asset; Instantiate(obj);
|
1 2 3 4 5 6 7
|
AssetBundleRequest ab_request = ab.LoadAllAssetsAsync(); yield return ab_request; object[] objArray = ab_request.allAssets; foreach (object o in objArray) { Instantiate(o as GameObject); }
|
http://koonick.gitee.io/koonick-blog/2020/12/10/Unity_AssetBundles/index.html
版权声明:
本博客所有文章均采用
BY-NC-SA
许可协议,转载请注明出处!