Unity3D 打AB包并读取StreamingAssets下的AB包

打AB包

*脚本放到Assets/Editor文件夹下,没有就创建一个

using UnityEditor;
using System.IO;
public class CreateAssetBundles : Editor
{
    //编辑器扩展,添加菜单选项,要求必须是静态方法
    [MenuItem("Assets/CreateBundle")]
    static void BuildAllAssetBundles()
    {
        //监测目录,没有则创建
        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";  //子选项精灵图片文件路径
        WWW www = new WWW(path);
        yield return www;                       
        
        AssetBundle ab = www.assetBundle;

        object sprite = ab.LoadAsset("1", typeof(Sprite)); //加载ab包中的资源名为 1 文件的数据,并转为 Sprite类型,返回Object对象 (这是精灵图片)
        image.sprite = (Sprite)sprite;                     //转为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  内存加载
        //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("Cube");
        //Instantiate(CubePrefab);

        //manifest 加载依赖资源
        AssetBundle manifestAB = AssetBundle.LoadFromFile("Assets/AssetBundles/AssetBundles");
        AssetBundleManifest manifest = manifestAB.LoadAsset("AssetBundleManifest");
        string[] str = manifest.GetAllDependencies("cube.ab");
        foreach (string name in str)
        {
            print(name);
            AssetBundle.LoadFromFile("Assets/AssetBundles/" + name);
        }
    }
}

你可能感兴趣的:(Unity3D 打AB包并读取StreamingAssets下的AB包)