[Unity XLua] hot update XLua entry (b): basic articles
3) The type of commission
C#:
1
2
3
4
5
6
7
8
9
10
11
| public class CSModelTest { public SelfVoidDelegate onClick; public delegate void SelfVoidDelegate(GameObject go); void OnClick() { Debug.Log( "测试" ); } public Action< string > TestDelegate = (param) => { Debug.Log( "TestDelegate in c#:" + param); }; } |
The entrustment is also similar to Class, and the entrustment is also a type, so we also need to deal with it like a class, by adding a feature tag or by Wrap processing, where the delegate is placed in the class, in fact, can also be put directly Under the namespace, the .NET library operates like this, but we will see that the NGUI source code will find that the NGUI source code operates in this way, such as the onClick event of the button. If you look at its delegate type, VoidDelegate, you will find that it operates in this way too. My example here is also inside the class.
C#
1
2
3
4
5
6
7
8
9
10
11
| public class CSModelTest { public SelfVoidDelegate onClick; public delegate void SelfVoidDelegate(GameObject go); void OnClick() { Debug.Log( "测试" ); } public Action< string > TestDelegate = (param) => { Debug.Log( "TestDelegate in c#:" + param); }; } |
Lua:
1
2
3
4
5
6
7
8
9
| local luaM3 = CS.CSModelTest local luaO3 = luaM3() luaO3.TestDelegate( 'lua中测试委托' ) luaO3.onClick = function(obj) print( 'hello 我是lua' ) print(obj) end luaO3.onClick( '我是lua' ) |
4) how the function parameters with ref out process
Because Lua is weakly typed and there are no C# types, sometimes some parameters may not be handled well. For example, overloaded with different types of parameters in C#, lua is not very easy to handle. Here you can view the issues in XLua. The author has one. Relevant answers to the questions. Here's how to access ref and out parameters of the function Lua.
C#:
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| public void SayHelloWithRefParam( ref string s) { Debug.Log( "传入的参数是:" + s); s = "Hello 我是C#" ; } public string SayHelloWithRefParamAndReturnString( ref string s) { Debug.Log( "传入的参数是:" + s); s = "Hello 我是C#" ; return "我是返回的字符串" ; } public void SayHelloWithOutParam( out string s) { s = "Hello,我是C#" ; Debug.Log( "Hello Aladdin_XLua, I am model function whih out param:" + s); } |
Lua:
1
2
3
4
5
6
7
8
9
10
11
12
13
| --测试 ref local inputValue = '你好,我是lua' local outputValue = luaO2:SayHelloWithRefParam(inputValue) print(outputValue) --lua是通过字符串返回 local outValue1,outValue2 = luaO2:SayHelloWithRefParamAndReturnString(inputValue) print(outValue1) print(outValue2) --测试 out inputValue = '我是测试lua' outputValue = luaO2:SayHelloWithOutParam(inputValue) print(outputValue) |
In the beginning , when I tested it , I thought that Lua called the ref parameter and returned the modified result. But beyond my expectation, I couldn't modify it. After the author suggested, lua is the ref parameter returned by the return value. If the function itself has a return value, then the last parameter is the returned ref or out parameter. This reader can try it out.
operation result
About Wrap
Wrap is a bridge between C# and Lua. Lua needs to use Wrap to access C#. I believe it is no stranger to see other Lua frameworks. XLua is very convenient for generating Wrap.
We just need to create a new class and then inherit a GenConfig interface. The following is the interface content. There are also descriptions of these types of XLua documents. We only need to add the custom class to the LuaCallCSharp collection, and click Generate. Help us generate the corresponding Wrap file
1
2
3
4
5
6
7
8
9
10
11
12
| //注意:用户自己代码不建议在这里配置,建议通过标签来声明!! public interface GenConfig { //lua中要使用到C#库的配置,比如C#标准库,或者Unity API,第三方库等。 List LuaCallCSharp { get ; } //C#静态调用Lua的配置(包括事件的原型),仅可以配delegate,interface List CSharpCallLua { get ; } //黑名单 List
|
Of course, the author also said that our custom C# code is best not to go through this approach, I just demonstrate how to add here, the following will say that the third-party plug-in support through this general approach.
C#:
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| public static class AladdinGenConfig { //lua扩展第三方或者自定义类库 public class LuaCallCSharpExtern : GenConfig { public List LuaCallCSharp { get { return new List() { typeof (CSModelWidhoutNameSpace), typeof (CSModel), typeof (CSModelTest), } } } } } |
Comments
Post a Comment