6.5AssetBundle

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;//進(jìn)度條

yield? return? 0;

}

//刪除加載界面

}

voidUpdate( ){

}

}

using UnityEngine;

using System.Collections;

using UnityEditor;

using System.IO;

public? class? AssetBundleScript:MonoBehaviour{

//打包

//創(chuàng)建一個同級目錄

[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");

}

//本地加載,只能加載物體本身,網(wǎng)絡(luò)加載將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

//加載材質(zhì)

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當(dāng)前要加載的資源的名字

//資源放置時的文件夾或者是網(wǎng)址

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");

//得到目標(biāo)資源的依賴關(guān)系的列表

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;

}

//加載目標(biāo)資源

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(){

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、什么是AssetBundle AssetBundle 是Unity pro提供的一種用來存儲資源的文件格式,它...
    好怕怕閱讀 7,850評論 1 8
  • 翻譯:莫銘原文地址:AssetBundle usage patterns 本系列中的上一篇文章覆蓋了AssetBu...
    莫銘閱讀 5,652評論 1 12
  • 本文轉(zhuǎn)自666666666 這是場景存儲的信息類,創(chuàng)建好不管它 usingUnityEngine; usingSy...
    吐泡泡的小鯉魚閱讀 2,016評論 0 2
  • 一、什么是AssetBundle 估計很多人只知道Unity的模型之類的東西可以導(dǎo)出成一種叫做AssetBundl...
    好怕怕閱讀 553評論 0 1
  • 我想要每天早上七點(diǎn)準(zhǔn)時起床喝杯溫水洗簌吃早飯收拾穿衣出門上班的時候認(rèn)真上班遇到的同事都是天使上學(xué)的時候認(rèn)真聽課課下...
    _Eileen閱讀 110評論 0 0

友情鏈接更多精彩內(nèi)容