Unity5.1 AssetBundle

资源管理脚本

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using UnityEngine;
 5 using UnityEditor;
 6 
 7 
 8 namespace LOAsset
 9 {
10     public class LOAssetManager
11     {
12         public LOAssetManager()
13         {
14 
15         }
16 
17         /// <summary>
18         /// 处理包裹
19         /// </summary>
20         /// <param name="ab"></param>
21         /// <param name="name"></param>
22         public static void ProcessAssetBundle(AssetBundle ab, string name)
23         {
24             //获取所有的资源名数组
25             string[] all_asset_names = ab.GetAllAssetNames();
26 
27             //获取所有的场景名数组
28             string[] all_scene_names = ab.GetAllScenePaths();
29 
30             if (!TestStringArray(all_asset_names))
31             {
32                 Debug.Log("no assets..");
33             }
34 
35             if (!TestStringArray(all_scene_names))
36             {
37                 Debug.Log("no scenes..");
38             }
39 
40             //获取整个AssetBundle文件夹的配置文件
41             AssetBundleManifest manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
42 
43             //获取
44             string[] all_bundles = manifest.GetAllAssetBundles();
45 
46             if (!TestStringArray(all_asset_names))
47             {
48                 Debug.Log("no bundles..");
49             }
50         }
51 
52         /// <summary>
53         /// 测试函数
54         /// </summary>
55         /// <param name="names"></param>
56         /// <returns></returns>
57         public static bool TestStringArray(string[] names)
58         {
59             if (names == null)
60             {
61                 return false;
62             }
63 
64             foreach (string item in names)
65             {
66                 Debug.Log(item);
67             }
68             return true;
69         }
70     }
71 }

测试脚本

 1 using UnityEngine;
 2 using System.Collections;
 3 using LOAsset;
 4 
 5 public class TestScript : MonoBehaviour
 6 {
 7     /// <summary>
 8     /// 下载资源包
 9     /// </summary>
10     /// <returns></returns>
11     IEnumerator DownloadAssetBundle()
12     {
13         //访问地址头
14         string url = "http://project.lanou3g.com/assetbundles/Others/";
15 
16         //访问资源名
17         string root = "Others";
18 
19         //构造访问www对象
20         WWW root_url = new WWW(url + root);
21 
22         yield return root_url;
23 
24         //下载完成后,获取assetbundle
25         AssetBundle rootBundle = root_url.assetBundle;
26 
27         //处理Bundle...
28         LOAssetManager.ProcessAssetBundle(rootBundle, root);
29 
30         //Cube资源的名字“prefabs/cube.prefab”
31         string cube = "prefabs/cube.prefab";
32 
33         //构造访问对象
34         WWW cube_url = new WWW(url + cube);
35 
36         yield return cube_url;
37 
38         AssetBundle cubeBundle = cube_url.assetBundle;
39 
40         //加载资源...
41         GameObject prefab = cubeBundle.LoadAsset<GameObject>("mycube.prefab");
42 
43         GameObject gameObject = GameObject.Instantiate(prefab);
44 
45         gameObject.transform.position = new Vector3(0, 0, 0);
46     }
47 
48     // Use this for initialization
49     void Start () {
50         //开始加载过程...
51         StartCoroutine("DownloadAssetBundle");
52     }
53     
54     // Update is called once per frame
55     void Update () {
56     
57     }
58 }

      

原文地址:https://www.cnblogs.com/loveforliving/p/4757317.html