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

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.IO;  
  4. public class LoadAsset : MonoBehaviour {  
  5.     
  6.     AssetBundleCreateRequest asset;//定义一个资源包创建请求  
  7.  IEnumerator LoadAssetBundle()  
  8.     {  
  9.            
  10.         FileStream AssetIO = new FileStream(Application.dataPath + @"/StreamingAssets/好孩子/001.dat", FileMode.Open, FileAccess.ReadWrite); //创建文件流(对象现含有中文)      
  11.         byte[] assetbytes = new byte[AssetIO.Length];  
  12.         AssetIO.Read(assetbytes, 0, (int)AssetIO.Length);  
  13.         AssetIO.Close();  
  14.     
  15.         asset = AssetBundle.CreateFromMemory(assetbytes);//从内存中创建资源          
  16.         yield return asset;  
  17.            
  18.         AssetBundle LoadAsset = asset.assetBundle;//这样就能得到我们需要的资源包了  
  19.         if (asset.isDone)  
  20.           {  
  21.           Instantiate(LoadAsset.Load("Test_001"));        
  22.           }  
  23.     }  

0 0