标签:www GameObject AssetBundle assetBundle 使用 go path
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
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。