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