[Unity XLua] hot update XLua entry (a): basic articles

Aladdin_XLua

Foreword

Some time ago, Tencent open-sourced an internal thermal framework, XLua, which caused a lot of hot debates in the Unity development community. It was also greatly favored by developers. Then of course I was also curious to learn. We will also put the expanded project on Git later , and we will learn and exchange together! Thanks to XLua authors for creating such a nice framework!

Personal opinion of XLua

  1. Simple and easy to use, easy to use
  2. High scalability, easy to add custom CS module or third-party plug-in
  3. Factory maintenance, reliable
  4. Features: HotFix  
    About this HotFix is ​​not what other hot Lua frameworks have, it is also one of his greatest strengths and features. The principle is to determine the modification logic at the logic level of the logic through the feature flags and make the program support Lua logic code. Instead of walking before C# logic

Expand XLua on your own to support NGUI development

Now that the Lua framework has very little support for NGUI, it may be that the current trend is to use native UGUI, but it is estimated that there are some NGUIs that prefer to use NGUI. After all, NGUI has been used for a long time. The XLua example already supports lua. Using UGUI, I added it myself to support NGUI development. In the follow-up I will also add some UGUI examples. Let's take a look at the expanded NGUI interface renderings, and then explain how to make XLua support third-party plug-ins.

Effect diagram

Quick start

Before learning something, it's best to learn a lot of tutorial documents written by XLua authors, including cases and wikis and issu. If there is anything that you don't understand, you can join us at the last group of the article.

1 custom C# class for Lua access

Here you can learn about the examples written by the author in Test, or more detailed, but I still want to record the process of my own attempts.
(1) Characteristics
XLua supports the use of feature tags to make custom classes available to Lua to access  
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[LuaCallCSharp]
public class CSModelWithAttribute
{
    public static void SayHello1()
    {
        Debug.Log("Hello Aladdin_XLua, I am static model function");
    }
    public void SayHello2()
    {
        Debug.Log("Hello Aladdin_XLua, I am model function");
    }
    public void SayHello3(string s)
    {
        Debug.Log("Hello Aladdin_XLua, I am model function whih param:" + s);
    }
    public string SayHello4(string s)
    {
        Debug.Log("Hello Aladdin_XLua, 我是具有返回值的CS方法:" + s);
        return "你好,我获得了lua,我是C#";
    }
    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);
    }
}
After adding the attribute tag, the program will automatically find the class with the attribute tag at startup and collect it into the lua stack so that we can use Lua to access custom C# classes and methods.
Lua visits:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using UnityEngine;
using System.Collections;
using XLua;
public class SelfExampleSrc : MonoBehaviour
{
    LuaEnv luaenv = new LuaEnv();
    void Start()
    {
        luaenv.DoString(@"
            print('Lua访问特性标记的对象方法')
            local luaM2 = CS.CSModelWithAttribute
            local luaO2 = luaM2()
            luaM2:SayHello1()
            luaO2:SayHello2()
            luaO2:SayHello3('我是阿拉丁')  --每次添加一个CS方法时候都要重新Generate一下啊
            --测试字符串返回
            local value = luaO2:SayHello4('你好,我是lua')
            print(value)                             
            --测试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)
            local luaM3 = CS.CSModelTest
            local luaO3 = luaM3()
            luaO3.TestDelegate('lua中测试委托')
            luaO3.onClick = function(obj)
                print('hello 我是lua')
                print(obj)
            end
            luaO3.onClick('我是lua')
        ");
    }
}
(2) wrap mode
If we write our own C# code, we can add Lua support through the first way. It is more convenient. If we use an open source third-party plugin, how can we quickly use XLua support to use Generate Wrap? This is also the strategy adopted by other Lua frameworks.
a) Classes with namespaces
C#:
1
2
3
4
5
6
7
8
9
10
namespace Aladdin_XLua
{
    public class CSModel
    {
        public void SayHello()
        {
            Debug.Log("Hello Aladdin_XLua");
        }
    }
}

Lua:
1
2
3
4
print('Lua访问有命名空间的对象/静态方法')
local luaModel = CS.Aladdin_XLua.CSModel
local luaObj = luaModel()
luaObj:SayHello()


luaModel is a C# class, the following luaObj is a class generated object, and finally is the access object method, the difference between the Lua colon call and the point call: the colon call default passed in the self parameter, unclear can Baidu related articles.
b) Classless namespaces
C#:
1
2
3
4
5
6
7
public class CSModelWidhoutNameSpace
{
    public void SayHello()
    {
        Debug.Log("Hello Aladdin_XLua without Namespace");
    }
}


Lua:
1
2
3
4
print('Lua访问无命名空间的对象方法')
local luaM = CS.CSModelWidhoutNameSpace
local luaO = luaM()
luaO:SayHello()


If there is no namespace, then CS is directly followed by the class name. In fact, CS is also a namespace on the outer layer. It is only the author who helps us to split the package.

Comments

Popular posts from this blog

Deep analysis of GC optimization for each value type of XLua under Unity

Unity UI Process-Oriented Programming Template for Lua

Tencent Open Hand Mobile Hot Update: Introduction to XLua under Unity3D