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不能加载中文路径的问题。