AssetBundle使用
作者:互联网
AssetBundle是什么?
AssetBundle是Unity资源加载的一个重要使用工具,一般在做游戏的时候,如果将所有资源都放在本地,常常会把游戏做的非常大,AssetBundle是将资源放到了服务器上,当需要加载资源的时候,将资源从服务器中下载下来使用
使用 AssetBundle 只需四步:
1.设置包名
2.打包
3.加载包内资源
1.设置要打包的资源
可以手动设置:
也可以在代码里面设置:
AssetImporter asset = AssetImporter.GetAtPath("Assets/GameRes/Cube.prefab");
asset.SetAssetBundleNameAndVariant("cube", "a");
2.进行打包
我们可以制作一个Editor下的工具类来完成我们打包的功能
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class MyTool : Editor
{
[MenuItem("Tools/CreateBundle")]
static void CreateAssetBundle()
{
string path = "AssetBundle";
//如果目录不存在创建目录
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//路径 打包方式 目标平台
BuildPipeline.BuildAssetBundles(path,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
AssetDatabase.Refresh();
}
}
打包成功
其中manifest是一个清单,记录了资源信息和依赖项的一些内容
3.进行打包
本地加载一共有四种方式 如下
1、内存
2、文件
3、WWW
4、UnityWebRequest(推荐)
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class LoadAssetBundle : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//StartCoroutine(FromMemery());
//StartCoroutine(FromFile());
//StartCoroutine(AllFromFile());
//StartCoroutine(FromWWW2());
StartCoroutine(FromUnityWebRequest());
}
//读取方式 1、内存 2、文件 3、WWW 4、unityWebRequest
//内存
IEnumerator FromMemery()
{
string path = @"E:\learn\Unity\AnimTest\AssetBundle\model.u3d";
byte[] bytes = File.ReadAllBytes(path);
AssetBundle assetBundle = AssetBundle.LoadFromMemory(bytes);
GameObject go = assetBundle.LoadAsset("Bike") as GameObject;
Instantiate(go);
yield return null;
}
//文件
IEnumerator FromFile()
{
string path = @"E:\learn\Unity\AnimTest\AssetBundle\model.u3d";
AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
GameObject go = assetBundle.LoadAsset("Step") as GameObject;
Instantiate(go);
yield return null;
}
//读取多个文件
IEnumerator AllFromFile()
{
string path= @"E:\learn\Unity\AnimTest\AssetBundle\model.u3d";
AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
object[] objs=assetBundle.LoadAllAssets();
Debug.Log(objs.Length);
foreach (var go in objs)
{
GameObject.Instantiate(go as GameObject);
}
yield return null;
}
//WWW 无缓存
IEnumerator FromWWW()
{
string path = @"file:///E:\learn\Unity\AnimTest\AssetBundle\model.u3d";
WWW www = new WWW(path);
yield return www;
//如果有错误
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield break;
}
AssetBundle assetBundle = www.assetBundle;
GameObject go = assetBundle.LoadAsset("Step") as GameObject;
Instantiate(go);
yield return null;
www.Dispose();
}
IEnumerator FromWWW2()
{
string path = @"file:///E:\learn\Unity\AnimTest\AssetBundle\model.u3d";
//缓存加载
WWW www = WWW.LoadFromCacheOrDownload(path,1);
yield return www;
//如果有错误
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield break;
}
AssetBundle assetBundle = www.assetBundle;
GameObject go = assetBundle.LoadAsset("Step") as GameObject;
Instantiate(go);
yield return null;
www.Dispose();
}
//unityWebRequest
IEnumerator FromUnityWebRequest()
{
string path = @"file:///E:\learn\Unity\AnimTest\AssetBundle\model.u3d";
UnityWebRequest unityWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(path);
yield return unityWebRequest.SendWebRequest();
AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(unityWebRequest);
GameObject go = assetBundle.LoadAsset("Step") as GameObject;
Instantiate(go);
yield return null;
}
}
利用服务器加载 我们可以通过IIS服务器配置的方式来加载
此时路径就改为使用http的路径
标签:www,GameObject,AssetBundle,assetBundle,使用,go,path 来源: https://www.cnblogs.com/fjnloo/p/16512143.html