unity AssetBundle打包

AssetBundle分两部分 一部分是将资源打包 代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;   //引入Unity编辑器,命名空间
using System.IO;     //引入的C#IO,命名空间

public class BuildAssetBundle {

    /// <summary>
    /// 打包生成所有的AssetBundles(包)
    /// </summary>
    [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);
    }


}

代码写好后要先在工程目录下选中要打包的资源,在inspector中AssetBundle选项后面给资源要打的包起名字

然后使用 BuildAllAB()方法就开始打包并将包存放在Assets/StreamingAssets目录中;

第二部分是使用包的资源:

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


    }
}

通过修改URL的地址和调用对应的加载方法就可以完成 AssetBundle包的应用了

猜你喜欢

转载自blog.csdn.net/weixin_41573444/article/details/82221718