Unity—AssetBundle的打包及四种加载资源方式

转载自技术情殇的博客.

原文地址:http://blog.sina.com.cn/s/blog_140bb6bd40102xajb.html

AssetBundle打包:脚本放在Editor文件夹内

具体代码如下:
 C# Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using  UnityEditor;
using  System.IO;

public   class  CreateAssetBundles  {

    [MenuItem(
"自定义菜单/资源打包" )]
    
static   void  BuildAllAssetBundles()
    {
        
//定义文件夹名字
         string  dir =  "AssetBundles" ;
        
//文件夹不存在,则创建
         if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);

        }

        
//资源打包
        BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
    
}

AssetBundle的四种方式资源加载,脚本绑在场景任意物体即可
具体代码如下:
 C# Code 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using  System.Collections;
using  System.Collections.Generic;
using  UnityEngine;
using  System.IO;
using  UnityEngine.Networking;

public   class  DownLoad : MonoBehaviour {

    IEnumerator Start () 
   {

        
//资源包路径
         string  path1 =  "AssetBundles/cubewall.unity3d" ;
        
//共享依赖资源包路径
         string  path2 =  "AssetBundles/share.unity3d" ;
        
        
//第一种加载AB的方式 ,从内存中加载 LoadFromMemory
         #region
        
//方法一:异步加载
         //加载资源
        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path1));
        yield 
return  request;
        
//加载共同依赖资源,如贴图、材质
        AssetBundleCreateRequest request2 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path2));
        yield 
return  request2;
        AssetBundle ab = request.assetBundle;
        AssetBundle ab2 = request2.assetBundle;

        
//使用里面的资源
        GameObject wallPrefab1 = ab.LoadAsset( "CubeWall" );
        Instantiate(wallPrefab1);
       
        
//方法二:同步加载(无需用协程)
         //加载资源
        AssetBundle ab3 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path1));
        
//加载共同依赖资源,如贴图、材质
        AssetBundle ab4 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path2));

        
//使用里面的资源
        GameObject wallPrefab2 = ab.LoadAsset( "CubeWall" );
        Instantiate(wallPrefab2);
        
#endregion


        
//第二种加载AB的方式 ,从本地加载 LoadFromFile(无需用协程)
         #region
        AssetBundle ab5 = AssetBundle.LoadFromFile(path1);
        AssetBundle ab6 = AssetBundle.LoadFromFile(path2);

        GameObject wallPrefab3 = ab5.LoadAsset(
"CubeWall" );
        Instantiate(wallPrefab3);
        
#endregion

        
//第三种加载AB的方式 ,从本地或服务器加载 WWW(将被弃用)
         #region
        
//是否准备好
         while  (Caching.ready ==  false )
        {
            yield 
return   null ;
        }
        
//从本地加载
         //WWW www = WWW.LoadFromCacheOrDownload(@"file:/E:AssetBundleProject\AssetBundleProject\AssetBundles", 1);
         //从服务器加载
        WWW www = WWW.LoadFromCacheOrDownload(@ "http://localhost/AssetBundles/cubewall.unity3d" 1 );
        yield 
return  www;
        
//是否报错
         if  ( string .IsNullOrEmpty(www.error) ==  false )
        {
            Debug.Log(www.error); yield 
break ;
        }
        AssetBundle ab7 = www.assetBundle;

        
//使用里面的资源
        GameObject wallPrefab4 = ab7.LoadAsset( "CubeWall" );
        Instantiate(wallPrefab4);
        
#endregion

        
//第四种加载AB方式 从服务器端下载 UnityWebRequest(新版Unity使用)
         #region
        
//服务器路径 localhost为IP
         string  uri = @ "http://localhost/AssetBundles/cubewall.unity3d" ;
        UnityWebRequest request3 = UnityWebRequest.GetAssetBundle(uri);
        yield 
return  request3.Send();

        
//AssetBundle ab8 = ((DownloadHandlerAssetBundle)request3.downloadHandler).assetBundle;
        AssetBundle ab8 = DownloadHandlerAssetBundle.GetContent(request3);

        
//使用里面的资源
        GameObject wallPrefab5 = ab8.LoadAsset( "CubeWall" );
        Instantiate(wallPrefab5);

        
//加载cubewall.unity3d资源包所依赖的资源包
        AssetBundle manifestAB =  AssetBundle.LoadFromFile( "AssetBundles/AssetBundles" );
        AssetBundleManifest manifest = manifestAB.LoadAsset(
"AssetBundleManifest" );

        
//foreach(string name in manifest.GetAllAssetBundles())
         //{
         //    print(name);
         //}

        
//cubewall.unity3d资源包所依赖的资源包的名字
         string [] strs =  manifest.GetAllDependencies( "cubewall.unity3d" );
        
foreach  ( string  name  in  strs)
        {
            AssetBundle.LoadFromFile(
"AssetBundles/"  + name);
        }
        
#endregion
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值