Introduction to unity3d development (IV) — implementing components with Lua

Time:2022-1-17

Wuyi Yinxia, whose real name is Guan Jianchang, has a 12-year game career. This tutorial takes unity 3D + vs code + c# + tolua as an example.

 

1、 Lua component base class

1. Create a new com directory under the assets / Lua directory to store Lua components

2. Create a new component in the assets / Lua / COM directory Lua file, add Lua component base class component to realize the life cycle of unity3d components

Assets/Lua/com/Component.lua

 1 [email protected] Component @Component class
 2 local Component = {}
 3 
 4 --- Awake
 5 function Component:Awake()
 6 end
 7 
 8 --- OnEnable
 9 function Component:OnEnable()
10 end
11 
12 --- Start
13 function Component:Start()
14 end
15 
16 --- Update
17 function Component:Update()
18 end
19 
20 --- FixedUpdate
21 function Component:FixedUpdate()
22 end
23 
24 --- LateUpdate
25 function Component:LateUpdate()
26 end
27 
28 --- OnGUI
29 function Component:OnGUI()
30 end
31 
32 --- OnDisable
33 function Component:OnDisable()
34 end
35 
36 --- OnDestroy
37 function Component:OnDestroy()
38 end
39 
40 --- ExtendComponent
41 [email protected] Component
42 function ExtendComponent()
43     return CreateComponent(Component)
44 end
45 
46 --- CreateComponent
47 [email protected] componentClass Component
48 [email protected] Component
49 function CreateComponent(componentClass)
50     local o = {}
51     
52     for k, v in pairs(componentClass) do
53         o[k] = v
54     end
55 
56     return o
57 end
58 
59 local com = {
60     ExtendComponent = ExtendComponent,
61     CreateComponent = CreateComponent,
62 }
63 
64 return com

3. The base class {component only implements the empty life cycle method, and the subclass only needs to implement the required life cycle method. The life cycle method not implemented by the subclass will have the default empty implementation.

4. When calling Lua’s method through c# call, the inherited method in meta table mode will be null. Here, inherit the base class by copying the key and call com The extendcomponent () method returns a subclass table that inherits component.

5. Subclasses through www The createcomponent method creates an object.

6. A simple sample component, testcomponent, is shown below

Assets/Lua/com/TestComponent.lua
 1 local com = require("Assets.Lua.com.Component")
 2 
 3 [email protected] TestComponent @TestComponent class
 4 TestComponent = com.ExtendComponent()
 5 
 6 function TestComponent.new(paramList)
 7     local o = com.CreateComponent(TestComponent)
 8 
 9     -- member fields
10     o.num = paramList[0] -- Array by C#, index begin from 0
11     return o
12 end
13 
14 function TestComponent:Awake()
15     print(self.num)
16     print("TestComponent:Awake")
17 end
18 
19 function TestComponent:Start()
20     print("TestComponent:Start")
21 end
22 
23 function TestComponent:OnDestroy()
24     print("TestComponent:OnDestroy")
25 end

be careful:

1) Here, testcomponent is a global variable, because c# directly accesses global variables, and local variables cannot be accessed directly.

2) The array parameter c# passed is paramlist, and the subscript starts from 0

 

2、 Generic c# component script

 1 using UnityEngine;
 2 using LuaInterface;
 3 
 4 public class LuaComponent : MonoBehaviour
 5 {
 6     public string luaClassName = "";
 7     public string[] paramList = null;
 8 
 9     private LuaState luaState = null;
10     private LuaTable luaObj = null;
11 
12     void Awake()
13     {
14         LuaClient luaClient = LuaClient.Instance;
15         this.luaState = luaClient.GetLooper().luaState;
16         this.luaState.DoFile(this.luaClassName + ".lua");
17         this.luaObj = callLuaNew();
18 
19         callLuaFunc("Awake");
20     }
21 
22     void OnEnable()
23     {
24         callLuaFunc("OnEnable");
25     }
26 
27     // Start is called before the first frame update
28     void Start()
29     {
30         callLuaFunc("Start");
31     }
32 
33     // Update is called once per frame
34     void Update()
35     {
36         callLuaFunc("Update");
37     }
38 
39     void FixedUpdate()
40     {
41         callLuaFunc("FixedUpdate");
42     }
43 
44     void LateUpdate()
45     {
46         callLuaFunc("LateUpdate");
47     }
48 
49     void OnGUI()
50     {
51         callLuaFunc("OnGUI");
52     }
53 
54     void OnDisable()
55     {
56         if (LuaClient.Instance != null)
57         {
58             callLuaFunc("OnDisable");
59         }
60     }
61 
62     void OnDestroy()
63     {
64         if (LuaClient.Instance != null)
65         {
66             callLuaFunc("OnDestroy");
67         }
68 
69         this.luaState = null;
70         this.luaObj = null;
71     }
72 
73     public LuaTable callLuaNew()
74     {
75         LuaFunction luaFunc = luaState.GetFunction(this.luaClassName + "." + "new");
76         luaFunc.BeginPCall();
77         luaFunc.Push(this.paramList);
78         luaFunc.PCall();
79         LuaTable table = luaFunc.CheckLuaTable();
80         luaFunc.EndPCall();
81         luaFunc.Dispose();
82         luaFunc = null;
83         return table;
84     }
85 
86     private void callLuaFunc(string funcName)
87     {
88         LuaFunction luaFunc = luaState.GetFunction(this.luaClassName + "." + funcName);
89         luaFunc.BeginPCall();
90         luaFunc.Push(this.luaObj);
91         luaFunc.PCall();
92         luaFunc.EndPCall();
93         luaFunc.Dispose();
94         luaFunc = null;
95     }
96 }

 

1. This component exposes two fields to unity3d: luaclassname and paramlist. Lua class name and initialization parameter list can be set in unity3d (the number of parameters can be adjusted in unity3d)

2. Load the with the same name as luaclassname in the {wake lifecycle Lua file, call the new method of lua’s luaclassname class, pass paramlist as a parameter, and get the Lua object.

3. Other lifecycle methods call the same name method of lua class, so that specific lifecycle logic can be implemented in Lua.

 

3、 Add Lua component search path

In order to let Lua virtual machine know the path of components, in main CS override the wake lifecycle method of the parent class and add the search path Lua / com

Assets/CSharp/Main.cs

1 new void Awake()
2 {
3     base.Awake();
4 
5     // search path
6     string fullPath = Application.dataPath + "/Lua/com";
7     luaState.AddSearchPath(fullPath);
8 }

 

4、 Test effect

1. In the GameObject menu, select create empty to add an empty GameObject

2. Add Lua component to the empty GameObject in the property panel, set Lua class name to testcomponent, and the parameter with subscript 0 to 12

3. Running effect log

 

Recommended Today

Tutorial on sending e-mail using net:: SMTP class in Ruby

Simple Mail Transfer Protocol(SMTP)SendE-mailAnd routing protocol processing between e-mail servers. RubyIt provides the connection of simple mail transfer protocol (SMTP) client of net:: SMTP class, and provides two new methods: new and start New takes two parameters: Server name defaults to localhost Port number defaults to 25 The start method takes these parameters: Server – […]