打AB包
*腳本放到Assets/Editor文件夾下,沒有就創(chuàng)建一個(gè)
using UnityEditor;
using System.IO;
public class CreateAssetBundles : Editor
{
//編輯器擴(kuò)展,添加菜單選項(xiàng),要求必須是靜態(tài)方法
[MenuItem("Assets/CreateBundle")]
static void BuildAllAssetBundles()
{
//監(jiān)測(cè)目錄匆背,沒有則創(chuàng)建
string dir = "Assets/AssetBundles";
if (Directory.Exists(dir) == false)
{
Directory.CreateDirectory(dir);
}
//打包
BuildPipeline.BuildAssetBundles(dir,
BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
//刷新
AssetDatabase.Refresh();
}
}
讀取圖片并賦值
#if UNITY_EDITOR
string filepath = Application.dataPath +"/StreamingAssets"+"/my.xml";
#elif UNITY_IPHONE
string filepath = Application.dataPath +"/Raw"+"/my.xml";
#elif UNITY_ANDROID
string filepath = "jar:file://" + Application.dataPath + "!/assets/"+"/my.xml;
#endif
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour
{
public Image image;
IEnumerator Start()
{
string path ="file://" + Application.dataPath + "/StreamingAssets/sprite.unity3d"; //子選項(xiàng)精靈圖片文件路徑
WWW www = new WWW(path);
yield return www;
AssetBundle ab = www.assetBundle;
object sprite = ab.LoadAsset("1", typeof(Sprite)); //加載ab包中的資源名為 1 文件的數(shù)據(jù)典尾,并轉(zhuǎn)為 Sprite類型墨技,返回Object對(duì)象 (這是精靈圖片)
image.sprite = (Sprite)sprite; //轉(zhuǎn)為Sprite類型惩阶,給Image 賦值
}
}
加載AB包
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;
public class LoadAB : MonoBehaviour
{
IEnumerator Start()
{
string path = "Assets/AssetBundles/cube.ab";
//第一種加載AB方式 LoadFromMemoryAsync 內(nèi)存加載
//1、異步
//AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
//yield return request;
//AssetBundle ab = request.assetBundle;
//2扣汪、同步
//AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
//第二種加載AB的方式 LoadFromFile 本地加載
//1断楷、異步
//AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
//yield return request;
//AssetBundle ab = request.assetBundle;
//2、同步
//AssetBundle ab = AssetBundle.LoadFromFile(path);
//第三種加載AB的方式 WWW
//while (Caching.ready == false)
//{
// yield return null;
//}
////file:// 或者 file:/// 具體路徑
//WWW www = WWW.LoadFromCacheOrDownload(@"file:///C:\_UnityFile\_LHL\AssetBundleTest\Assets\AssetBundles\cube.ab", 1);
//WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles\cube.ab", 1);
//yield return www;
//if (string.IsNullOrEmpty(www.error) == false)
//{
// Debug.Log(www.error);
// yield break;
//}
//AssetBundle ab = www.assetBundle;
//第四種加載AB的方式 UnityWebRequest
string url = @"file:///C:\_UnityFile\_LHL\AssetBundleTest\Assets\AssetBundles\cube.ab";
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
yield return request.SendWebRequest();
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
//使用資源
//GameObject CubePrefab = ab.LoadAsset<GameObject>("Cube");
//Instantiate(CubePrefab);
//manifest 加載依賴資源
AssetBundle manifestAB = AssetBundle.LoadFromFile("Assets/AssetBundles/AssetBundles");
AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
string[] str = manifest.GetAllDependencies("cube.ab");
foreach (string name in str)
{
print(name);
AssetBundle.LoadFromFile("Assets/AssetBundles/" + name);
}
}
}