Unity UI Process-Oriented Programming Template for Lua

The template is based on the xLua implementation, and you can learn about xLua if you are not familiar with it. It is also easy to switch to other Lua thermal solutions.

Send the sample address first

The window is as follows

The left side of the window is a list, select the items in the list, the content of the current selection will be displayed on the right.
The following is a Lua file that implements the window function.
Require 'item' -- list items
Local ue = CS.UnityEngine
--window
Panel = {
 --Awake event
 --data is a table that contains the UI's gameObject and registered UI components and can also add its own variables
 Awake = function (data)
  Data.list = {}; - Save the data of the generated item
  Local go = ue.Resources.Load('Item')
  For index=1,3 do
   Local item = ue.Object.Instantiate(go)
   Item.transform.parent = data.leftView.transform
   item.transform.localScale = ue.Vector3.one
   Data.list[index] = item:GetComponent('FuncLuaBehavior').luaData
   Item.Init(data.list[index], data, index)
  End
 End;
 -- Select an item
 SelectItem = function (data, num)
  For index= 1, #data.list do
   Data.num:GetComponent('Text').text=num
   Item.SetLight(data.list[index], index==num and true or false)
  End
 End;
}
In the above file, only two functions are defined. The data data is processed as a function parameter. This is a process-oriented programming method. What is the data specifically? How is it defined? Look at the code below.
[LuaCallCSharp]
Public class FuncLuaBehavior : MonoBehaviour {
    Public string luaFile;
    Public Injection[] injections;
    Internal static LuaEnv luaEnv = new LuaEnv(); //all lua behaviour shared one luaenv only!
    LuaTable dataTable; // Lua table, including data created by C# and created in Lua
    LuaTable funcTable; // Event response functions and function functions implemented by Lua
    Public LuaTable luaData
    {
        Get { return dataTable; }
    }
    Void Awake()
    {
        //Load Lua file
        luaEnv.DoString(string.Format("require '{0}'", luaFile ));
        // Define the dataTable
        dataTable = luaEnv.NewTable();
        //Insert UI components that need to be processed into dataTable
        dataTable.Set("gameObject", gameObject);
        Foreach (var injection in injections)
        {
            dataTable.Set(injection.name, injection.value);
        }
        // Get a reference to the function table in the Lua file
        funcTable = luaEnv.Global.Get(luaFile);
        If (funcTable != null)
        {
            Var luaAwake = funcTable.Get<action>("Awake");
            If (luaAwake != null)
            {
                luaAwake(dataTable);
            }
        }
    }
}</action
The above code borrows from the implementation of LuaBehaviour in xLua. The process-oriented support is mainly dataTable and funcTable. The dataTable is the "data" of the window, created in Awake and inserted into the UI components that need to be processed. funcTable refers to the table defined in the Lua file, which is our previous Lua file. Then we trigger the Awake event in Lua. The parameter of the event is dataTable.
This is the structure of the entire template.
The following is another part of the Lua file in this example. Combine the comments in the code to understand the entire implementation.
Local ue = CS.UnityEngine
--List items
Item = {
 --Awake event
 Awake = function (data)
  Data.button:GetComponent("Button").onClick:AddListener(function()
   Panel.SelectItem(data.panel, data._num)
  End)
 End;
 --initialization
 Init = function (data, panel, num)
  Data.panel = panel -- Save parent window reference
  Data._num = num -- save number
  Data.num:GetComponent("Text").text = num
 End;
 --Set checked state
 SetLight = function (data, light)
  Data.light:SetActive(light)
 End;
}

Why not object-oriented but process-oriented?

  • Process-oriented than the object-oriented simple and clear, easy to use, but the UI function is also relatively simple, is the incident response, with the process can solve the problem
  • Lua's object-oriented meta-based implementation, write a class declaration requires a lot of lines of code, compared to the high-level language class declaration is also complex
  • The scope of variables defined in Lua has global and current files, but it can only correspond to one C# object. If there are multiple objects in C#, multiple variables need to be defined to correspond to it. Implementing such a function as a backpack is troublesome. Using process-oriented, data and functional separation, Lua implements functionality and data is defined and managed by C#. The lifecycle of the data and UI components corresponds to Awake and is destroyed on OnDestroy. The data is passed as a parameter to the Lua event and the corresponding UI component is processed in the Lua event response.

Comments

Popular posts from this blog

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

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