美文网首页
6.5AssetBundle

6.5AssetBundle

作者: 胤醚貔貅 | 来源:发表于2017-06-05 17:21 被阅读79次

    usingUnityEngine;

    usingSystem.Collections;

    usingSystem.Collections.Generic;

    usingSystem;

    usingUnityEngine.SceneManagement;

    public class  PlaneController:MonoBehaviour{

    string  filePath;

    void Start( ){

    filePath="Texture/timg";

    //Resources资源异步加载

    StartCoroutine(LoadTexture(delegate(Texture  obj){

    GetComponent<Renderer>( ).material.mainTexture=obj;

    Resources.UnloadUnusedAssets( );

    }));

    }

    IEnumerator LoadTexture(Action <delegate>callBack){//匿名委托

    ResourceRequest  req=Resources.LoadAsync<Texture>(filePath);

    yield  return  req;

    callBack(req.asset  asTexture);

    }

    IEnumerator LoadScence(){

    AsyncOperation op=SceneManager.LoadSceneAsync(0);

    while(op.isDone){

    //Slider.value=op.progress;//进度条

    yield  return  0;

    }

    //删除加载界面

    }

    voidUpdate( ){

    }

    }

    using UnityEngine;

    using System.Collections;

    using UnityEditor;

    using System.IO;

    public  class  AssetBundleScript:MonoBehaviour{

    //打包

    //创建一个同级目录

    [MenuItem("AssetBundle/BuildAssetBoundle")]

    static void  BuildAssetBundle( ){

    string  path="Assets/AssetBundles";

    if(!Directory.Exists(path)){

    Directory.CreateDirectory(path);

    }

    BuildPipeline.BuildAssetBundles(path,BuildAssetBundleOptions.None,BuildTarget.StandaloneOSXIntel64);

    }

    [MenuItem("AssetBundle/GetAssetBundleName")]

    static void GetABName(){

    string[ ]names=AssetDatabase.GetAllAssetBundleNames( );

    foreach(string name in names){

    Debug.Log(name);

    }

    }

    }

    usingUnityEngine;

    usingSystem.Collections;

    usingSystem.IO;

    usingUnityEngine.Networking;

    usingSystem.Collections.Generic;

    publicclassLoadAssetsBundle:MonoBehaviour{

    void Start( ){

    //从本地取

    //AssetBundle   ab=AssetBundle.LoadFromFile(Application.dataPath+"/AssetBundles/cube/cube");

    //if(ab!=null){

    //GameObject  prefab=ab.LoadAsset("cube");

    //Instantiate(prefab,Vector3.zero,Quaternion.identity);

    StartCoroutine("CreatObject");

    }

    //本地加载,只能加载物体本身,网络加载将file换成http

    IEnumeratorCreatObject(){

    //#ifUNITY_ANDROID

    //"jar:file://"+Application.dataPath+"!/assets/AssetBundles/material/red";

    //#elifUNITY_IOS

    //Application.dataPath+"/Raw/AssetBundles/material/red";

    //#elifUNITY_WIN||UNITY_EDITOR

    //"file://"+Application.dataPath+"/AssetBundle/material/red";

    //#endif

    //加载材质

    string url_material="file://"+Application.dataPath+"/AssetBundles/material/red";

    UnityWebRequest  req_material=UnityWebRequest.GetAssetBundle(url_material);

    yield return  req_material.Send( );

    AssetBundle  ab_material=DownloadHandlerAssetBundle.GetContent(req_material);

    Material  material=ab_material.LoadAsset("red");

    Instantiate(material,Vector3.zero,Quaternion.identity);

    //加载cube对象

    stringurl="file://"+Application.dataPath+"/AssetBundles/cube/cube";

    UnityWebRequest req=UnityWebRequest.GetAssetBundle(url);

    yield return req.Send( );

    AssetBundle  ab=DownloadHandlerAssetBundle.GetContent(req);

    GameObjectprefab=ab.LoadAsset("cube");

    GameObjectcube=Instantiate(prefab,Vector3.zero,Quaternion.identity)as GameObject;

    cube.GetComponent<Renderer>.material=material;

    }

    void Update( ){

    }

    }

    usingUnityEngine;

    usingSystem.Collections;

    public  class  LoadAB:MonoBehaviour{

    void Start( ){

    StartCoroutine(Load("cube/cube"));

    }

    IEnumerator  Load(string  name){//name当前要加载的资源的名字

    //资源放置时的文件夹或者是网址

    string   assetPath="file://"+Application.dataPath+"/AssetBundles/";

    //总的Manifest的文件名

    string  manifestName="AssetBundles";

    //加载总清单(AssetBundles.manifest)

    WWW  wwwManifest=WWW.LoadFromCacheOrDownload(assetPath+manifestName,0);

    yield return wwwManifest;

    if(string.IsNullOrEmpty(wwwManifest.error)){

    AssetBundle  manifestAB=wwwManifest.assetBundle;

    AssetBundleManifest  manifest=manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

    //得到目标资源的依赖关系的列表

    string[ ]dps=manifest.GetAllDependencies(name);

    //保存所有的依赖资源

    AssetBundle[ ]abs=new AssetBundle[dps.Length];

    for(inti=0;i<dps.Length;i++){

    string path=assetPath+dps[i];

    WWW www=WWW.LoadFromCacheOrDownload(path,0);

    yield return www;

    abs[i]=www.assetBundle;

    }

    //加载目标资源

    WWW cubeWWW=WWW.LoadFromCacheOrDownload(assetPath+name,0);

    yield return cubeWWW;

    if(string.IsNullOrEmpty(cubeWWW.error)){

    //得到资源列表

    AssetBundle  cubeAB=cubeWWW.assetBundle;

    //获得资源

    string[ ]strs=name.Split(new char[]{'/'});

    GameObject  CubePrefab=cubeAB.LoadAsset(strs[strs.Length-1]);

    Instantiate(CubePrefab,Vector3.zero,Quaternion.identity);

    //cubeWWW.Dispose();

    }

    }

    }

    voidUpdate(){

    }

    }

    相关文章

      网友评论

          本文标题:6.5AssetBundle

          本文链接:https://www.haomeiwen.com/subject/myzifxtx.html