1.在Assets/Editor文件夾里面創(chuàng)建編輯器AB插件文件夾AssetBundle
1.創(chuàng)建腳本BuildAssetBundles.cs
using System.Net;
using UnityEditor;
using System.IO;
using System;
public class BuildAssetBundles
{
// 菜單選項(xiàng)目錄
[MenuItem("Assets/Build AssetBundles")]
static public void BuildAllAssetBundles()
{
// 創(chuàng)建文件目錄
string dir = "AssetBundles";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
// 構(gòu)建
// 參數(shù)1:路徑
// 參數(shù)2:壓縮算法,none 默認(rèn)
// 參數(shù)3:設(shè)備參數(shù)呵恢,ios砌函,Android关筒,windows等等
BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
UnityEngine.Debug.Log("AssetBundle資源打包完成憎账!");
}
}
3.設(shè)置下cube預(yù)制體的AB包信息
4.點(diǎn)擊菜單生成ab包
Assets/Scripts/XLua/Gen/UnityEngine_LightWrap.cs(191,39): error CS1061: 'Light' does not contain a definition for 'SetLightDirty' and no accessible extension method 'SetLightDirty' accepting a first argument of type 'Light' could be found (are you missing a using directive or an assembly reference?)
Assets/Scripts/XLua/Gen/UnityEngine_LightWrap.cs(783,60): error CS1061: 'Light' does not contain a definition for 'shadowRadius' and no accessible extension method 'shadowRadius' accepting a first argument of type 'Light' could be found (are you missing a using directive or an assembly reference?)
Assets/Scripts/XLua/Gen/UnityEngine_LightWrap.cs(797,60): error CS1061: 'Light' does not contain a definition for 'shadowAngle' and no accessible extension method 'shadowAngle' accepting a first argument of type 'Light' could be found (are you missing a using directive or an assembly reference?)
Assets/Scripts/XLua/Gen/UnityEngine_LightWrap.cs(1292,35): error CS1061: 'Light' does not contain a definition for 'shadowRadius' and no accessible extension method 'shadowRadius' accepting a first argument of type 'Light' could be found (are you missing a using directive or an assembly reference?)
Assets/Scripts/XLua/Gen/UnityEngine_LightWrap.cs(1307,35): error CS1061: 'Light' does not contain a definition for 'shadowAngle' and no accessible extension method 'shadowAngle' accepting a first argument of type 'Light' could be found (are you missing a using directive or an assembly reference?)
Error building Player because scripts had compiler errors
官方文檔FQA說明:編輯器下運(yùn)行正常疟暖,打包的時(shí)候生成代碼報(bào)“沒有某方法/屬性/字段定義”怎么辦绝骚?
往往是由于該方法/屬性/字段是擴(kuò)在條件編譯里頭贪嫂,只在UNITY_EDITOR下有效寺庄,這是可以通過把這方法/屬性/字段加到黑名單來解決,加了之后要等編譯完成后重新執(zhí)行代碼生成力崇。
解決方案:在Assets\Editor\XLua\Generator.cs 中GetGenConfig函數(shù)中添加黑名單
public static void GetGenConfig(IEnumerable<Type> check_type)
{
...
BlackList = new List<List<string>>()
{
...
new List<string>(){"UnityEngine.Light", "SetLightDirty"},
new List<string>(){"UnityEngine.Light", "shadowRadius"},
new List<string>(){"UnityEngine.Light", "shadowAngle"},
new List<string>(){"UnityEngine.Light", "shadowRadius"},
new List<string>(){"UnityEngine.Light", "shadowAngle"},
};
...
// 添加黑名單后铣揉,執(zhí)行菜單xlua ->Clear Generator code ,再執(zhí)行xlua ->Generator code
}
生成成功!5.加載ab包
加載ab包有三種方法
// 第一種加載AB的方式LoadFromFile同步加載(本地加載)
AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/testab.ab");
GameObject wallPrefab = ab.LoadAsset<GameObject>("cubeWall");
Instantiate(wallPrefab);
// 第二種加載AB的方式LoadFromMemory同步加載(當(dāng)AB包使用UDP或者TCP協(xié)議的時(shí)候可以運(yùn)用此方法)
string path = "AssetBundles/testab.ab";
AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");
Instantiate(wallPrefab);
// 第三種使用UnityWebRequest
//string uri = @"file:///O:\AssetBundleTestProjectAssetBundles/testab.ab";
string uri = @"http://localhost/AssetBundles/testab.ab";
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);
yield return request.SendWebRequest();
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//使用資源
GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");
Instantiate(wallPrefab);
參考資料
https://github.com/Tencent/xLua/blob/master/Assets/XLua/Doc/faq.md
https://blog.csdn.net/u014361280/article/details/99712651
https://github.com/Tencent/xLua/issues/482