1、參考資料
https://gameinstitute.qq.com/community/detail/100031
https://www.cnblogs.com/Roz-001/p/11237198.html
2店茶、生成AB包
首先給資產(chǎn)文件指定包名肚豺,然后代碼生成對(duì)應(yīng)的ab包。
同一AB包內(nèi)的資產(chǎn)名稱不能重復(fù)
using System.IO;
using UnityEditor;
using UnityEngine;
public class AssetBundlesBuilder
{
const string _assetBundleDirectory = "Assets/AssetBundles";
[MenuItem("Assets/Build AssetBundles")]
static void BuildAllAssetBundles()
{
if (!Directory.Exists(_assetBundleDirectory))
{
Directory.CreateDirectory(_assetBundleDirectory);
}
BuildPipeline.BuildAssetBundles(_assetBundleDirectory, BuildAssetBundleOptions.None,
BuildTarget.StandaloneWindows);
}
}
3谷异、加載AB包中的資產(chǎn)
更多加載方式見參考連接分尸。可以自行構(gòu)建包和文件路徑的對(duì)應(yīng)關(guān)系表實(shí)現(xiàn)根據(jù)項(xiàng)目中的文件路徑加載AB包中的資源歹嘹。
using System.IO;
using UnityEditor;
using UnityEngine;
public class ABLoader
{
AssetBundle _ab;
public ABLoader(string abPath)
{
_ab = AssetBundle.LoadFromFile(abPath);
}
public GameObject Load(string name)
{
GameObject go = _ab.LoadAsset<GameObject>(name);
return go;
}
}
using System;
using UnityEngine;
public class Test : MonoBehaviour
{
void OnEnable()
{
var abLoader = new ABLoader("Assets/AssetBundles/objs");
var go = abLoader.Load("AB_Cube");
GameObject.Instantiate(go);
}
}