UNITY之AssetBundle
来源:互联网 发布:高通900e端口转9008 编辑:程序博客网 时间:2024/06/10 07:42
1:在Assets目录下新建两个文件夹>Editor,AssetBundles
2:在Editor中新建脚本
using UnityEngine;
using System.Collections;
using UnityEditor;
public class ImportAsset : MonoBehaviour {
[@MenuItem("Test/BuildAssetBundles")]
static void BuildAssetBundles(){
BuildPipeline.BuildAssetBundles ("Assets/AssetBundles");
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
3:设置预设体,材质,纹理,的AssetBundle
4:运行Test/BuildAssetBundles,则此AssetBundles文件夹里会有相应的AssetBundle文件
5:开启本地服务器:python -m SimpleHTTPServer 8080
6:能后将AssetBundles文件夹里的AssetBundle文件放到本地文稿里
7:从本地服务器上获取相应的预设体,材质,纹理
using UnityEngine;
using System.Collections;
public class DownLoadAsset : MonoBehaviour {
public GameObject goCube;//掩饰修改纹理
public GameObject goSphere;//展示修改材质
public Transform newPos;//展示根据预设体创建对象
//url=IP+文件名
private string url1 = "http://127.0.0.1:8080/Documents/haha.assetbundle";
private string url2 = "http://127.0.0.1:8080/Documents/hi.assetbundle";
private string url3 = "http://127.0.0.1:8080/Documents/cube.assetbundle";
// Use this for initialization
void Start () {
StartCoroutine (download_Texture(url1));
StartCoroutine (download_Material(url2));
StartCoroutine (download_prefab(url3));
}
// Update is called once per frame
void Update () {
}
IEnumerator download_Texture(string url){
WWW downAsset = new WWW (url);
yield return downAsset;
goCube.GetComponent<Renderer> ().material.mainTexture = (Texture)downAsset.assetBundle.LoadAsset ("haha");
downAsset.assetBundle.Unload (false);
}
IEnumerator download_Material(string url){
WWW downAsset = new WWW (url);
yield return downAsset;
goSphere.GetComponent<Renderer> ().material = (Material)downAsset.assetBundle.LoadAsset ("hi");
downAsset.assetBundle.Unload (false);
}
// IEnumerator download_prefab(string url){
// WWW downAsset = new WWW (url);
// yield return downAsset;
// GameObject obj = (GameObject)downAsset.assetBundle.LoadAsset ("Cube");
// GameObject.Instantiate (obj,newPos.position,newPos.rotation);
// downAsset.assetBundle.Unload (false);
// }
IEnumerator download_prefab(string url){
WWW downloadAsset = new WWW (url);
//同步加载资源
// yield return downloadAsset;
// GameObject gpPrefab = (GameObject)Instantiate (downloadAsset.assetBundle.LoadAsset("Cube"));
// gpPrefab.GetComponent<Renderer> ().material.color = Color.red;
// gpPrefab.transform.position = newPos.transform.position;
// downloadAsset.assetBundle.Unload (false);
// downloadAsset.Dispose ();
//异步加载资源
yield return downloadAsset;//直到downloadAsset下载完才会运行下面代码
AssetBundle bundle = downloadAsset.assetBundle;
AssetBundleRequest request = bundle.LoadAssetAsync ("Cube",typeof(GameObject));
yield return request;
GameObject gpPrefab = (GameObject)Instantiate (request.asset);
gpPrefab.GetComponent<Renderer> ().material.color = Color.red;
gpPrefab.transform.position = newPos.transform.position;
downloadAsset.assetBundle.Unload (false);
downloadAsset.Dispose ();
}
}
8:从网上或file获取
using UnityEngine;
using System.Collections;
public class DownLoadScript : MonoBehaviour {
public GameObject Cube;
public GameObject Sphere;
// Use this for initialization
void Start () {
// StartCoroutine (LoadByHttp());
// StartCoroutine (LoadByFile());
// StartCoroutine (HttpWwwForm());
}
// Update is called once per frame
void Update () {
}
IEnumerator LoadByHttp(){
WWW texture = new WWW ("http://pic36.nipic.com/20131117/5848817_155724710182_2.jpg");
yield return texture;
Sphere.GetComponent<MeshRenderer> ().material.mainTexture = texture.texture;
texture.Dispose ();
}
IEnumerator LoadByFile(){
WWW www = new WWW ("file:////Users/students/Desktop/NiHao.png");
yield return 0;
Cube.GetComponent<MeshRenderer> ().material.mainTexture = www.texture;
www.Dispose ();
}
//向网络提交表单
IEnumerator HttpWwwForm(){
WWWForm wwwForm = new WWWForm ();
wwwForm.AddField ("user_name","nihaokekeyi");
wwwForm.AddField ("user_password","123456");
//get
//WWW www = new WWW ("http://172.16.10.2:8080/service/registeruser?user_name=nihaokekeyi&user_password=123456");
//post
WWW www = new WWW ("http://172.16.10.2:8080/service/registeruser",wwwForm);
yield return www;
print (www.text);
}
}
因为FileStream是允许中文路径的,可以先通过文件流把AssetBundle读取到内存,在通过CreateFromMemory 创建AssetBundle资源,这样的话就避开了WWW不能加载中文路径的问题。
- Unity之AssetBundle资源管理
- UNITY之AssetBundle
- unity之AssetBundle
- Unity之 AssetBundle应用
- Unity资源打包之Assetbundle
- Unity之资源打包Assetbundle
- Unity 热更新之AssetBundle
- Unity资源解决方案之AssetBundle
- Unity资源打包之Assetbundle
- Unity 热更新之AssetBundle
- Unity 热更新之AssetBundle
- unity AssetBundle
- unity-AssetBundle
- <Unity> AssetBundle
- Unity AssetBundle
- unity动态加载之AssetBundle应用
- unity动态加载之AssetBundle原理
- 【进阶】Unity资源解决方案之AssetBundle
- 常用网站
- DTC命令arm-linux-gnueabihf-objcopy工具和mkimage工具的使用
- java网络编程
- Js中&&和||的运用
- 类和对象
- UNITY之AssetBundle
- [动画]android动画篇:View动画(补间动画)
- wxPython事件处理
- 51nod 1126 求递推序列的第N项(矩阵快速幂、循环)
- react native 与IOS 初识对比
- php中的date()函数d y m l等字母的表示方法
- UVA - 1586 Molar mass
- Swift上写百度地图记录
- SpringMVC+WebSocket的通用接口映射实现