Unity AssetBundles打包和加载

一、文章详解:AssetBundle的打包和加载

二、搭建场景

图片右下角,根据名称来打包,相同名字的打在同一个包里面。项目地址


打包要求:

“picture.pa”信息里面打包的信息名要记好,等到加载的时候,需用到。

注:“picture.pa”我为了看里面的内容先放到Resources文件夹下,等看完信息,需把它放回StreamingAssets文件下


二、脚本

1.打包脚本

打包脚本需放在Editor文件名下,自己选择打包好放在哪个路径下面。根据需求可以更改打包平台Android或者IOS等等(BuildTarget.Android)。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class CreateAssetBundles : Editor
{
    [MenuItem("Tools/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        BuildPipeline.BuildAssetBundles(Application.dataPath+ "/StreamingAssets",BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.Android);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        Debug.LogWarning("打包成功");
    }
}

2.加载脚本

加载资源的时候,注意名称是否写对,图片要注意格式。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test2 : MonoBehaviour
{
    public static string BundleURL;
    private GameObject g;
    public Image image;

    public Image prefabPicture;
    private void Awake()
    {
        //平台预处理
        BundleURL =
#if UNITY_ANDROID
                    "jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
                    Application.dataPath + "/Raw/";  
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
 "file://" + Application.dataPath + "/StreamingAssets/";//Test_AssetBundle 资源束文件的位置 
#else
                    string.Empty;  
#endif
    }

    public void OnPicture()
    {
        //图片打包加载
        StartCoroutine(DownLoadAssetAndScene(BundleURL + "cai.pa"));
    }

    public void OnPrefab()
    {
       //预制体打包加载
        StartCoroutine(DownPrefabAndScene(BundleURL + "picture.pa"));
    }

    IEnumerator DownLoadAssetAndScene(string path)
    {
        //加载
        WWW asset = new WWW(path);

        yield return asset;

        AssetBundle bundle = asset.assetBundle;
        //资源加载 
        //加载的是打包到资源包里面,每个资源的名字
        //g = Instantiate(bundle.LoadAsset("GameObject.prefab")) as GameObject;
        image.gameObject.SetActive(true);
        for (int i = 0; i < 92; i++)
        {

            image.sprite = bundle.LoadAsset<Sprite>(i + ".png");//"Resources" +
            yield return new WaitForSeconds(0.11f);
        }

        image.gameObject.SetActive(false);
        //资源加载完毕后记得Unload,不然再次加载资源的时候无法加载
        bundle.Unload(false);
    }

    IEnumerator DownPrefabAndScene(string path)
    {
        //加载
        WWW asset = new WWW(path);

        yield return asset;

        AssetBundle bundle = asset.assetBundle;
        //资源加载 
        //加载的是打包到资源包里面,每个资源的名字
        g = Instantiate(bundle.LoadAsset("Picture.prefab")) as GameObject;
        prefabPicture.gameObject.SetActive(true);

        for (int i = 0; i < g.GetComponent<PictureControl>().LuoShen_1.Length; i++)
        {
            prefabPicture.sprite = g.GetComponent<PictureControl>().LuoShen_1[i];
            yield return new WaitForSeconds(0.11f);
        }
        //资源加载完毕后记得Unload,不然再次加载资源的时候无法加载
        prefabPicture.gameObject.SetActive(false);
        bundle.Unload(false);
    }
}



猜你喜欢

转载自blog.csdn.net/Until_/article/details/79389023