仅供参考记录
一:普通打包方式
using UnityEditor;
using System.IO;
public class CreateAssetBundles
{
[MenuItem("Assets/Build AssetBundles")]
static void BuildAllAssetBundles()
{
string assetBundleDirectory = "Assets/StreamingAssets/AssetBundles";
if (!Directory.Exists(assetBundleDirectory))
{
Directory.CreateDirectory(assetBundleDirectory);
}
BuildPipeline.BuildAssetBundles(assetBundleDirectory,
BuildAssetBundleOptions.None,
BuildTarget.StandaloneWindows);
}
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class LoadFromFileExample : MonoBehaviour
{
public string assetBundleName;
void Start()
{
StartCoroutine(InstantiateObject(assetBundleName));
}
IEnumerator InstantiateObject(string assetBundleName)
{
string path = string.Empty;
if (Application.platform == RuntimePlatform.Android)
{
path = "jar:file://" + Application.dataPath + "!/assets/" + assetBundleName;
}
else
{
path = "file:///" + Application.dataPath + "/StreamingAssets/AssetBundles/"+ assetBundleName;
}
// string url = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;
var request = UnityEngine.Networking.UnityWebRequestAssetBundle.GetAssetBundle(path, 0);
yield return request.SendWebRequest();
AssetBundle bundle = UnityEngine.Networking.DownloadHandlerAssetBundle.GetContent(request);
if (bundle!=null)
{
//加载包中资源
string[] str = bundle.GetAllAssetNames();
foreach (string str2 in str)
{
Debug.Log(str2);
}
GameObject cube = bundle.LoadAsset<GameObject>("assets/xxx/circulatory_limb.fbx");
Instantiate(cube);
}
}
}
二:使用AssetBundle-Browser可视化打包
从UnityPackageManager引入 https://github.com/Unity-Technologies/AssetBundles-Browser/tree/AssetStore
可视化界面从Window-AssetBundle Browser打开
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class AssetBundleLoaderMgr
{
/// <summary>
/// 初始化,加载AssetBundleManifest,方便后面查找依赖
/// </summary>
public void Init()
{
string streamingAssetsAbPath = Path.Combine(Application.streamingAssetsPath, "StandaloneWindows");
AssetBundle streamingAssetsAb = AssetBundle.LoadFromFile(streamingAssetsAbPath);
m_manifest = streamingAssetsAb.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
}
/// <summary>
/// 加载AssetBundle
/// </summary>
/// <param name="abName">AssetBundle名称</param>
/// <returns></returns>
public AssetBundle LoadAssetBundle(string abName)
{
AssetBundle ab = null;
if (!m_abDic.ContainsKey(abName))
{
string abResPath = Path.Combine(Application.streamingAssetsPath, abName);
ab = AssetBundle.LoadFromFile(abResPath);
m_abDic[abName] = ab;
}
else
{
ab = m_abDic[abName];
}
return ab;
}
/// <summary>
/// 从AssetBundle中加载Asset
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="abName">AssetBundle名</param>
/// <param name="assetName">Asset名</param>
/// <returns></returns>
public T LoadAsset<T>(string abName, string assetName) where T : Object
{
AssetBundle ab = LoadAssetBundle(abName);
T t = ab.LoadAsset<T>(assetName);
return t;
}
/// <summary>
/// 缓存加载的AssetBundle,防止多次加载
/// </summary>
private Dictionary<string, AssetBundle> m_abDic = new Dictionary<string, AssetBundle>();
/// <summary>
/// 它保存了各个AssetBundle的依赖信息
/// </summary>
private AssetBundleManifest m_manifest;
/// <summary>
/// 单例
/// </summary>
private static AssetBundleLoaderMgr s_instance;
public static AssetBundleLoaderMgr instance
{
get
{
if (null == s_instance)
s_instance = new AssetBundleLoaderMgr();
return s_instance;
}
}
}
void Start()
{
//初始化
AssetBundleLoaderMgr.instance.Init();
}
void LoadAsset()
{
//加载预设
GameObject prefab = AssetBundleLoaderMgr.instance.LoadAsset<GameObject>("bundle标签", "文件名");
//实例化预设
Instantiate(prefab);
总结:当前使用Unity2021版本,Application.streamingAssetsPath已经能自动识别Android路径了不需要再使用"jar:file://" + Application.dataPath + "!/assets/"
参考自: