Ulua plug-in www.ulua.org, the following is to say is the Ulua development framework.
The first is the Lualoader class, which loads a LUA table so that the LUA table hangs on the game object like a unity component, the code is as follows:
Using luainterface;using system;using Unityengine;public class lualoader:monobehaviour{public string Name; Luatable m_table; Luafunction M_updatefunc; Luafunction M_fixedupdatefunc; void Awake () {if (string. IsNullOrEmpty (name)) throw new ArgumentException ("name"); m_table = luahelper.getluatable (Name); if (m_table = = null) throw new ArgumentNullException ("No table in" + Name); Init lua m_table["transform"] = transform; m_table["Gameobject"] = Gameobject; Awake Callmethod ("Awake"); M_updatefunc = GetMethod ("Update"); M_fixedupdatefunc = GetMethod ("Fixedupdate"); } void Start () {Callmethod ("start"); } void Update () {if (M_updatefunc! = null) M_updatefunc.call (time.deltatime); } void Fixedupdate () {if (M_fixedupdatefunc! = null) M_fixedupdatefunc.call (); } void Onenable () { Callmethod ("onenable"); } void Ondisable () {Callmethod ("ondisable"); } void OnDestroy () {Callmethod ("OnDestroy"); Free memory m_table["transform"] = null; m_table["Gameobject"] = null; M_table. Release (); m_table = null; if (m_updatefunc! = null) m_updatefunc.release (); if (m_fixedupdatefunc! = null) m_fixedupdatefunc.release (); LuaScriptMgr.Instance.LuaGC (); } luafunction GetMethod (string methodName) {return m_table[methodname] as luafunction; } void Callmethod (string name) {var func = GetMethod (name); if (func! = null) {func. Call (); Func. Release (); Free Memory}} public luatable Table {get {return m_table;} }}
The second is the interaction between Lua and C #, which provides two helper classes, one Luahelper, a game-logic-agnostic approach, and the other is Luautils, where Lua's approach to accessing C # code (related to Game logic) is encapsulated.
Luahelper key Several method codes are as follows:
Using luainterface;using resource;using system;using system.io;using system.linq;using System.Security.Cryptography; Using system.text;using unityengine;public static class luahelper{#region getluacomponent public static luatable Ge Tluacomponent (Transform Transform, String type) {var loaders = Transform. Getcomponents<lualoader> (); var rightloader = loaders. FirstOrDefault (lt = lt). Table! = null && Lt. Table.name = = type); return rightloader! = null? RightLoader.Table:null; } public static luatable getluacomponent (Gameobject gameobject, String type) {return getluacomponent (gameobj Ect.transform, type); } #endregion///<summary>//Load table from AB package for LUA use//</summary>//<param name= "name" > Table name </param> public static void Loadluatable (string name) {if (string. IsNullOrEmpty (name)) throw new ArgumentException ("name"); luatable table = Luascriptmgr.insTance. Getluatable (name); if (table = = null) {using (var Loadlua = new Loadluahandler (name)) luascriptmgr.instance . Dostring (Loadlua.text); }} public static luatable getluatable (string name) {if (string. IsNullOrEmpty (name)) throw new ArgumentException ("name"); luatable table = LuaScriptMgr.Instance.GetLuaTable (name); if (table = = null) {using (var Loadlua = new Loadluahandler (name)) luascriptmgr.instance . Dostring (Loadlua.text); Table = LuaScriptMgr.Instance.GetLuaTable (name); } return table; public static Luafunction getluafunction (String className, String funcName) {if (string. IsNullOrEmpty (ClassName)) throw new ArgumentException (ClassName); if (string. IsNullOrEmpty (FuncName)) throw new ArgumentException (FuncName); luatable table = getluatable (className); return Table[funcname]As Luafunction; } public static object[] CallFunction (String className, String funcName, params object[] args) {luafunction Func = Getluafunction (ClassName, funcName); if (func = = null) throw new ArgumentNullException (string. Format ("Cann ' t find Lua function: {0}.{ 1} ", ClassName, FuncName)); var Returnargs = args = = null? Func. Call (): Func. Call (args); Func. Release (); return Returnargs; }}
In addition, the ability to patch C # through LUA code is to detect patches and patches at the first frame of the UIPanel onenable, with the C # code as follows:
Using luainterface;using system;using system.collections.generic;using unityengine;public class LuaPatchManager: idisposable{list<patch> m_patches; #region Singleton static Luapatchmanager s_instance; public static Luapatchmanager Instance {get {return s_instance;} } #endregion #region Patch class Patch:idisposable {luafunction m_validate; Luafunction M_correct; Public Patch (luafunction validate, luafunction correct) {if (validate = null) throw new ArgumentNullException ("Validate"); if (correct = = null) throw new ArgumentNullException ("correct"); M_validate = Validate; M_correct = correct; } public bool Validate (UIPanel uipanel) {var objs = m_validate. Call (UIPanel); return (BOOL) objs[0]; } public void Correct (UIPanel uipanel) {m_correct. Call (UIPanel); } #region IDisposable public void Dispose () {Dispose (true); Gc. SuppressFinalize (this); } void Dispose (bool disposing) {if (disposing) {m_validate. Release (); M_validate = null; M_correct. Release (); M_correct = null; }} ~patch () {Dispose (false); } #endregion} #endregion private Luapatchmanager (luatable listtable) {if (listtable = = NULL | | ListTable.Values.Count <= 0) throw new ArgumentException ("listtable = = NULL | | ListTable.Values.Count <= 0 "); M_patches = new list<patch> (); foreach (string name in listtable.values) {var patch = luahelper.getluatable (name); if (patch! = null) {var validatefunc = patch["Validate"] as luafunction; var correctfunc = patch["Correct"] as luafunction; if (Validatefunc = null && Correctfunc! = null) m_patches. ADD (New Patch (Validatefunc, Correctfunc)); Patch. Release (); }} listtable.release (); LuaScriptMgr.Instance.LuaGC (); public static void Load () {luatable listtable = null; String targetfilename = "Luapatchlist"; try {listtable = luahelper.getluatable (TargetFileName); } catch {debug.logwarning ("No file:" + TargetFileName); if (listtable! = null) {if (ListTable.Values.Count > 0) s_instance = new Lua Patchmanager (listtable); Listtable.release (); }} public void Dopatch (UIPanel uipanel) {if (UIPanel = = null) throw new ArgumentNullException ("UIPanel"); for (int i = 0; i < m_patches. Count; i++) {Patch p = M_patchEs[i]; if (P.validate (UIPanel)) {p.correct (UIPanel); Break }}} #region IDisposable public void Dispose () {Dispose (true); Gc. SuppressFinalize (this); } void Dispose (bool disposing) {if (disposing) {for (int i = 0; i < m_patches. Count; i++) M_patches[i]. Dispose (); M_patches. Clear (); M_patches = null; }} ~luapatchmanager () {Dispose (false); } #endregion}
The LUA code is as follows:
luapatchlist={};//An example of a patch is as follows: Maininfocontrollerpatch={};local function clicktest () tipsshowcontroller.show ("Who is You? "); end--Verify that this uipanel is a uipanel--function maininfocontrollerpatch.validate (UIPanel) return that you want to patch Uipanel.transform.parent~=nil and uipanel.transform.parent.name== "Maininfocontroller (Clone)"; end-- Correct the display on this UIPanel, execute the method etc.--function maininfocontrollerpatch.correct (uipanel) Local Titlelabel=uipanel.transform:find ("LabelName"): Getcomponent ("UILabel"); titlelabel.text= "Tianjie"; local Mustbuybutton=uipanel.transform:find (" Buttonfashion "): Getcomponent (" UIButton "); MustBuyButton.onClick:Clear (); Eventdelegate.add (Mustbuybutton.onclick,delegatefactory.eventdelegate_callback (ClickTest)); end
Unity3d Hot-Update solution, using the framework developed by the Ulua plugin.