1.在Assets/Editor文件夹里面创建编辑器AB插件文件夹AssetBundle
QQ20210826-162450.png
1.创建脚本BuildAssetBundles.cs
using System.Net;
using UnityEditor;
using System.IO;
using System;
public class BuildAssetBundles
{
// 菜单选项目录
[MenuItem("Assets/Build AssetBundles")]
static public void BuildAllAssetBundles()
{
// 创建文件目录
string dir = "AssetBundles";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
// 构建
// 参数1:路径
// 参数2:压缩算法,none 默认
// 参数3:设备参数,ios,Android,windows等等
BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
UnityEngine.Debug.Log("AssetBundle资源打包完成!");
}
}
3.设置下cube预制体的AB包信息
QQ20210826-163859.png
4.点击菜单生成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说明:编辑器下运行正常,打包的时候生成代码报“没有某方法/属性/字段定义”怎么办?
往往是由于该方法/属性/字段是扩在条件编译里头,只在UNITY_EDITOR下有效,这是可以通过把这方法/属性/字段加到黑名单来解决,加了之后要等编译完成后重新执行代码生成。
解决方案:在Assets\Editor\XLua\Generator.cs 中GetGenConfig函数中添加黑名单
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"},
};
...
// 添加黑名单后,执行菜单xlua ->Clear Generator code ,再执行xlua ->Generator code
}
生成成功!
QQ20210826-204702.png
5.加载ab包
加载ab包有三种方法
// 第一种加载AB的方式LoadFromFile同步加载(本地加载)
AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/testab.ab");
GameObject wallPrefab = ab.LoadAsset<GameObject>("cubeWall");
Instantiate(wallPrefab);
// 第二种加载AB的方式LoadFromMemory同步加载(当AB包使用UDP或者TCP协议的时候可以运用此方法)
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
网友评论