Lua 是一个小巧的脚本语言。其设计目的是为了通过灵活嵌入应用程序中从而为应用程序提供灵活的扩展和定制功能。Lua由标准C编写而成,几乎在所有操作系统和平台上都可以编译,运行。Lua并没有提供强大的库,这是由它的定位决定的。所以Lua不适合作为开发独立应用程序的语言。
Lua脚本可以很容易的被C/C++ 代码调用,也可以反过来调用C/C++的函数,这使得Lua在应用程序中可以被广泛应用。
Lua是Unity3D热更新技术实现的一种方式,分别有uLua、toLua、xLua等,国内热更新用xLua实现的较多,xLua是腾讯发布的。
今天主要介绍xLua和Unity3D实现方式。
Lua教程:https://www.w3cschool.cn/lua/lua-tutorial.html
xLua的源码:https://github.com/Tencent/xLua
首先:下载xlua的Plugins,xLua源码中就有,导入Unity的Assets下的Plugins文件夹
***其次:做xLua的配置(可能下载的Plugins自带配置脚本,已经配置好了), xLua所有的配置都支持三种方式:打标签;静态列表;动态列表。
配置有两必须两建议:
- 列表方式均必须是static的字段/属性
- 列表方式均必须放到一个static类
- 建议不用标签方式
- 建议列表方式配置放Editor目录(如果是Hotfix配置,而且类位于Assembly-CSharp.dll之外的其它dll,必须放Editor目录)
其实,配置这块我也弄的不是很清楚,具体可看源码中的配置文档,大家见谅,有什么见解可交流。
//XLua.BlackList配置方式
[BlackList]
public static List<List<string>> BlackList = new List<List<string>>() {
new List<string>(){"UnityEngine.GameObject", "networkView"},
new List<string>(){"System.IO.FileInfo", "GetAccessControl","System.Security.AccessControl.AccessControlSections"},
..........
};
C# 访问Lua
//***Lua的代码***
string script = @"
a = 1
b = 'hello world'
c = true
d = {
f1 = 12, f2 = 34,
1, 2, 3,
add = function(self, a, b)
print('d.add called')
return a + b
end
}
function e()
print('i am e')
end
function f(a, b)
print('a', a, 'b', b)
return 1, {f1 = 1024}
end
function ret_e()
print('ret_e called')
return e
end";
*****Unity3D的C#代码:*****(主要部分)
// 固定标准写法部分
LuaEnv luaenv = null;
void Start(){
luaenv = new LuaEnv();
//script就是加载过来的Lua语言脚本
luaenv.DoString(script);
}
void Update()
{
if (luaenv != null){
luaenv.Tick();
}
}
void OnDestroy()
{
luaenv.Dispose();
}
//访问数据部分
void GetInfo(){
//a、b、c表示对面的变量名称,数字类型统称是Number类型,没有int、double等之分。
int a = luaenv.Global.Get<int>("a");
string b = luaenv.Global.Get<string>("b")
bool c = luaenv.Global.Get<bool>("c")
///映射到有对应字段的class
public class DClass
{
public int f1;
public int f2;
}
DClass d = luaenv.Global.Get<DClass>("d");
//或者
//映射到Dictionary<string, double>,by value
Dictionary<string, double> d1 = luaenv.Global.Get<Dictionary<string, double>>("d");
//或者
////映射到interface实例
[CSharpCallLua]
public interface ItfD
{
int f1 { get; set; }
int f2 { get; set; }
int add(int a, int b);
}
ItfD d3 = luaenv.Global.Get<ItfD>("d");
//映射到一个delgate,要求delegate加到生成列表,否则返回null
Action e = luaenv.Global.Get<Action>("e");
[CSharpCallLua]
public delegate int FDelegate(int a, string b, out DClass c);
FDelegate f = luaenv.Global.Get<FDelegate>("f");
//delegate可以返回更复杂的类型,甚至是另外一个delegate
[CSharpCallLua]
public delegate Action GetE();
GetE ret_e = luaenv.Global.Get<GetE>("ret_e");//返回ret_e也是一个函数,可以当成一个函数委托
.......
}
Lua访问C#(资源热更新最常用的调用)
//Unity3D的C#代码
固定代码跟上述C# 访问xLua中C#固定标准代码一样,这里就不写了
//C#实现的需要Lua语言访问的类和属性方法
注意:这里要访问的类,接口,结构体,枚举紧接上行都要加[LuaCallCSharp]
namespace Tutorial
{
[LuaCallCSharp]
public class BaseClass
{
public static void BSFunc()
{
Debug.Log("Derived Static Func, BSF = " + BSF);
}
public static int BSF = 1;
public void BMFunc()
{
Debug.Log("Derived Member Func, BMF = " + BMF);
}
public int BMF { get; set; }
}
public struct Param1
{
public int x;
public string y;
}
[LuaCallCSharp]
public enum TestEnum
{
E1,
E2
}
[LuaCallCSharp]
public class DerivedClass : BaseClass
{
[LuaCallCSharp]
public enum TestEnumInner
{
E3,
E4
}
public void DMFunc()
{
Debug.Log("Derived Member Func, DMF = " + DMF);
}
public int DMF { get; set; }
public double ComplexFunc(Param1 p1, ref int p2, out string p3, Action luafunc, out Action csfunc)
{
Debug.Log("P1 = {x=" + p1.x + ",y=" + p1.y + "},p2 = " + p2);
luafunc();
p2 = p2 * p1.x;
p3 = "hello " + p1.y;
csfunc = () =>
{
Debug.Log("csharp callback invoked!");
};
return 1.23;
}
public void TestFunc(int i)
{
Debug.Log("TestFunc(int i)");
}
public void TestFunc(string i)
{
Debug.Log("TestFunc(string i)");
}
public static DerivedClass operator +(DerivedClass a, DerivedClass b)
{
DerivedClass ret = new DerivedClass();
ret.DMF = a.DMF + b.DMF;
return ret;
}
public void DefaultValueFunc(int a = 100, string b = "cccc", string c = null)
{
UnityEngine.Debug.Log("DefaultValueFunc: a=" + a + ",b=" + b + ",c=" + c);
}
public void VariableParamsFunc(int a, params string[] strs)
{
UnityEngine.Debug.Log("VariableParamsFunc: a =" + a);
foreach (var str in strs)
{
UnityEngine.Debug.Log("str:" + str);
}
}
public TestEnum EnumTestFunc(TestEnum e)
{
Debug.Log("EnumTestFunc: e=" + e);
return TestEnum.E2;
}
public Action<string> TestDelegate = (param) =>
{
Debug.Log("TestDelegate in c#:" + param);
};
public event Action TestEvent;
public void CallEvent()
{
TestEvent();
}
public ulong TestLong(long n)
{
return (ulong)(n + 1);
}
class InnerCalc : ICalc
{
public int add(int a, int b)
{
return a + b;
}
public int id = 100;
}
public ICalc GetCalc()
{
return new InnerCalc();
}
public void GenericMethod<T>()
{
Debug.Log("GenericMethod<" + typeof(T) + ">");
}
}
[LuaCallCSharp]
public interface ICalc
{
int add(int a, int b);
}
[LuaCallCSharp]
public static class DerivedClassExtensions
{
public static int GetSomeData(this DerivedClass obj)
{
Debug.Log("GetSomeData ret = " + obj.DMF);
return obj.DMF;
}
public static int GetSomeBaseData(this BaseClass obj)
{
Debug.Log("GetSomeBaseData ret = " + obj.BMF);
return obj.BMF;
}
public static void GenericMethodOfString(this DerivedClass obj)
{
obj.GenericMethod<string>();
}
}
[LuaCallCSharp]
public struct A
{
public int a;
}
[LuaCallCSharp]
public struct B
{
public A b;
public double c;
}
void Foo(B b)
{
Debug.Log(" ");
}
}
//xLua访问Unity3D对象以及代码
//--表示Lua语言注释
function demo()
--new C#对象,helloworld表示创建的Unity3D对象名字,这块也可以省略
local newGameObj = CS.UnityEngine.GameObject('helloworld')
--访问静态属性,方法
local GameObject = CS.UnityEngine.GameObject
print('UnityEngine.Time.deltaTime:', CS.UnityEngine.Time.deltaTime) --读静态属性
CS.UnityEngine.Time.timeScale = 0.5 --写静态属性
GameObject.Find('helloworld') --静态方法调用,表示查找Unity3D的helloworld对象
CS.UnityEngine.GameObject.Find("对象"):GetCompontent("脚本名")
--typeof,Unity3D添加脚本
newGameObj:AddComponent(typeof(CS.UnityEngine.ParticleSystem))
--访问类成员属性,方法,Tutorial表示命名空间,DerivedClass表示类名
local DerivedClass = CS.Tutorial.DerivedClass
local testobj = DerivedClass()
testobj.DMF = 1024--设置成员属性调用
testobj:DMFunc()--成员方法调用
DerivedClass.BSF = 2048--静态属性调用
DerivedClass.BSFunc();--静态方法调用
--复杂方法调用
local ret, p2, p3, csfunc = testobj:ComplexFunc({x=3, y = 'john'}, 100, function()
print('i am lua callback')
end)
print('ComplexFunc ret:', ret, p2, p3, csfunc)
csfunc()
--重载方法调用
testobj:TestFunc(100)
testobj:TestFunc('hello')
--操作符
local testobj2 = DerivedClass()
testobj2.DMF = 2048
print('(testobj + testobj2).DMF = ', (testobj + testobj2).DMF)
--默认值
testobj:DefaultValueFunc(1)
testobj:DefaultValueFunc(3, 'hello', 'john')
--可变参数
testobj:VariableParamsFunc(5, 'hello', 'john')
--Extension methods
print(testobj:GetSomeData())
print(testobj:GetSomeBaseData()) --访问基类的Extension methods
testobj:GenericMethodOfString() --通过Extension methods实现访问泛化方法
--枚举类型
local e = testobj:EnumTestFunc(CS.Tutorial.TestEnum.E1)
print(CS.Tutorial.TestEnum.__CastFrom(1), CS.Tutorial.TestEnum.__CastFrom('E1'))
--Delegate
testobj.TestDelegate('hello') --直接调用
local function lua_delegate(str)
print('TestDelegate in lua:', str)
end
testobj.TestDelegate = lua_delegate + testobj.TestDelegate --combine,这里演示的是C#delegate作为右值,左值也支持
testobj.TestDelegate('hello')
testobj.TestDelegate = testobj.TestDelegate - lua_delegate --remove
testobj.TestDelegate('hello')
--事件
local function lua_event_callback1() print('lua_event_callback1') end
local function lua_event_callback2() print('lua_event_callback2') end
testobj:TestEvent('+', lua_event_callback1)
testobj:CallEvent()
testobj:TestEvent('+', lua_event_callback2)
testobj:CallEvent()
testobj:TestEvent('-', lua_event_callback1)
testobj:CallEvent()
testobj:TestEvent('-', lua_event_callback2)
--64位支持
local l = testobj:TestLong(11)
print(type(l), l, l + 100, 10000 + l)
--复杂类型使用(使用table自动转换)
testobj:Foo({b={a=100},c=200})
--cast
local calc = testobj:GetCalc()
print('assess instance of InnerCalc via reflection', calc:add(1, 2))
assert(calc.id == 100)
//强转类型(一般遇到接口会使用的到)
cast(calc, typeof(CS.Tutorial.ICalc))
print('cast to interface ICalc', calc:add(1, 2))
assert(calc.id == nil)
end
//可以直接执行,也可以协程执行
--demo()
--协程下使用
local co = coroutine.create(function()
demo()
end)
assert(coroutine.resume(co))
简单的xLua和Unity3D实现热更新技术代码介绍,但是万变不离其宗,就是这些简单的语句实现。
最后重点:将Lua脚本写好后,可放在StreamingAssets文件夹下面,使用异步加载读取