AssetsBundle基础
1.打包:
[MenuItem("AssetBundleTools/BuildAllAssetBundles")]
public static void BuildAllAB()
{
//打包AB输出路径
string strABOutPathDIR = string.Empty;
//获取"StreamingAssets"数值
strABOutPathDIR = Application.streamingAssetsPath;
//判断生成输出目录文件夹
if (!Directory.Exists(strABOutPathDIR))
{
Directory.CreateDirectory(strABOutPathDIR);
}
//打包生成
BuildPipeline.BuildAssetBundles(strABOutPathDIR,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
}
2.有依赖关系的文件要先加载依赖关系文件 .
/***
*
* Title: "AssetBundle原理"项目
*
* Description:
* 功能:
* 演示AssetBundle基本加载
* 分为两种:
* 1: 加载非“对象预设”方式。(例如: 贴图、材质、音频...)
* 2: 加载“对象预设”(*.Prefab) 方式。
*
* Author: Liuguozhu
*
* Date: 2017.10
*
* Modify:
*
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace DemoSpace
{
public class AssetBundleLoadDemo : MonoBehaviour
{
//测试对象,改变贴图
//public GameObject goCubeChangeTextur;
//测试对象,显示方位
public Transform TraShowPos;
//定义URL与资源名称
//private string _URL1;
//private string _AssetName1;
private string _URL0; //只加载AB包URL路径
private string _URL2;
private string _AssetName2;
private void Awake()
{
////AB 包下载地址
//_URL1 = "file://" + Application.streamingAssetsPath + "/textures1";
////(AB包内部)资源名称
//_AssetName1 = "unitychan_tile6";
//只加载“素材AB包”
_URL0= "file://" + Application.streamingAssetsPath + "/texture1";
//AB 包下载地址
_URL2 = "file://" + Application.streamingAssetsPath + "/prefabs1";
//(AB包内部)资源名称
_AssetName2 = "FloorCube.prefab";
}
void Start () {
//测试加载“非GameObject”资源
//StartCoroutine(LoadNonObjectFromAB(_URL1, goCubeChangeTextur, _AssetName1));
//测试加载“非GameObject”资源
StartCoroutine(LoadFromAB(_URL0));//先加载“素材AB”包
StartCoroutine(LoadPrefabsFromAB(_URL2, _AssetName2, TraShowPos));//再加载AB预设包,且提取资源。
}
/// <summary>
/// 加载“非GameObject”资源
/// </summary>
/// <param name="ABURL">AB包URL</param>
/// <param name="goShowObj">操作且显示的对象</param>
/// <param name="assetName">加载资源的名称</param>
/// <returns></returns>
IEnumerator LoadNonObjectFromAB(string ABURL, GameObject goShowObj, string assetName)
{
//参数检查
if(string.IsNullOrEmpty(ABURL) || goShowObj==null)
{
Debug.LogError(GetType()+ "/LoadNonObjectFromAB()/输入的参数不合法,请检查");
}
using (WWW www=new WWW(ABURL))
{
yield return www;
AssetBundle ab = www.assetBundle;
if (ab != null)
{
if (assetName=="")
{
goShowObj.GetComponent<Renderer>().material.mainTexture = (Texture)ab.mainAsset;
}
else {
goShowObj.GetComponent<Renderer>().material.mainTexture = (Texture)ab.LoadAsset(assetName);
}
//卸载资源(只卸载AB包本身)
ab.Unload(false);
}
else {
Debug.LogError(GetType() + "/LoadNonObjectFromAB()/WWW 下载错误,请检查 URL: "+ ABURL + " 错误信息:"+www.error);
}
}
}
/// <summary>
/// 加载“预设”资源
/// </summary>
/// <param name="ABURL">AB包URL</param>
/// <param name="assetName">加载资源的名称</param>
/// <param name="showPosition">显示的方位</param>
/// <returns></returns>
IEnumerator LoadPrefabsFromAB(string ABURL, string assetName,Transform showPosition=null)
{
//参数检查
if (string.IsNullOrEmpty(ABURL))
{
Debug.LogError(GetType() + "/LoadPrefabsFromAB()/输入的参数不合法,请检查");
}
using (WWW www = new WWW(ABURL))
{
yield return www;
AssetBundle ab = www.assetBundle;
if (ab != null)
{
if (assetName=="")
{
//加载主资源
if (showPosition != null)
{
GameObject goCloneObj=(GameObject)Instantiate(ab.mainAsset);
//克隆的对象显示位置
goCloneObj.transform.position = showPosition.transform.position;
}
else {
//克隆加载的预设对象
Instantiate(ab.mainAsset);
}
}
else {
//实例化指定资源
if (showPosition != null)
{
GameObject goCloneObj = (GameObject)Instantiate(ab.LoadAsset(assetName));
//克隆的对象显示位置
goCloneObj.transform.position = showPosition.transform.position;
}
else
{
//克隆加载的预设对象
Instantiate(ab.LoadAsset(assetName));
}
}
//卸载资源(只卸载AB包本身)
ab.Unload(false);
}
else
{
Debug.LogError(GetType() + "/LoadNonObjectFromAB()/WWW 下载错误,请检查 URL: " + ABURL + " 错误信息:" + www.error);
}
}
}
//加载AB包(不提取资源)
IEnumerator LoadFromAB(string ABURL)
{
//参数检查
if (string.IsNullOrEmpty(ABURL))
{
Debug.LogError(GetType() + "/LoadFromAB()/输入的参数不合法,请检查");
}
using (WWW www = new WWW(ABURL))
{
yield return www;
AssetBundle ab = www.assetBundle;
if (ab == null)
{
Debug.LogError(GetType() + "/LoadNonObjectFromAB()/WWW 下载错误,请检查 URL: " + ABURL + " 错误信息:" + www.error);
}
}
}
}
}