在上一篇文章 Unity3D熱更新之LuaFramework篇[04]--自定義UI監聽方法 中,我對LuaBehaviour腳本進行了擴展,添加了兩個新的UI監聽方法,也提到最好能單寫一個腳本處理此事。本篇文章就來繼續這個工做。html
一、建立UI監聽腳本
打開以前的工程,在Assets/LuaFrameworks/Scripts/Common下,建立一個UIEventEx.cs腳本,將LuaBehaviour.cs中的AddButtonClick以及AddInputFieldEndEditHandler方法遷移過來,並擴展了一些其它方法,代碼以下:git
UIEventEx.cs1 using LuaInterface; 2 using System.Collections; 3 using System.Collections.Generic; 4 using UnityEngine; 5 using UnityEngine.EventSystems; 6 using UnityEngine.UI; 7 8 /// <summary> 9 /// 自定義的添加UI監聽的方法,能夠用lua中調用以作事件綁定 10 /// </summary> 11 public class UIEventEx { 12 //添加監聽 13 public static void AddButtonClick(GameObject go, LuaFunction luafunc) 14 { 15 if (go == null || luafunc == null) 16 return; 17 18 Button btn = go.GetComponent<Button>(); 19 if (btn == null) 20 return; 21 22 btn.onClick.AddListener 23 ( 24 delegate () 25 { 26 luafunc.Call(go); 27 } 28 ); 29 } 30 31 //添加監聽(外帶數據中轉功能) 32 public static void AddButtonClick(GameObject go, LuaFunction luafunc, LuaTable luatable) 33 { 34 if (go == null || luafunc == null) 35 return; 36 37 Button btn = go.GetComponent<Button>(); 38 if (btn == null) 39 return; 40 41 btn.onClick.AddListener 42 ( 43 delegate () 44 { 45 luafunc.Call(go, luatable); 46 } 47 ); 48 } 49 50 /// <summary> 51 /// 給Toggle組件添加監聽 52 /// </summary> 53 public static void AddToggle(GameObject go, LuaFunction luafunc, LuaTable luatable) 54 { 55 if (go == null || luafunc == null) return; 56 57 Toggle toggle = go.GetComponent<Toggle>(); 58 59 if (toggle == null) return; 60 61 go.GetComponent<Toggle>().onValueChanged.AddListener( 62 delegate (bool select) { 63 luafunc.Call(luatable, select); 64 } 65 ); 66 } 67 68 69 /// <summary> 70 /// 給Toggle組件添加監聽 71 /// </summary> 72 public static void AddToggle(GameObject go, LuaFunction luafunc) 73 { 74 if (go == null || luafunc == null) return; 75 76 Toggle toggle = go.GetComponent<Toggle>(); 77 78 if (toggle == null) return; 79 80 go.GetComponent<Toggle>().onValueChanged.AddListener( 81 delegate (bool select) { 82 luafunc.Call(select); 83 } 84 ); 85 } 86 87 //給輸入組件(InputField)添加結束編輯(OnEndEdit)監聽 88 public static void AddInputFieldEndEditHandler(GameObject go, LuaFunction luafunc) 89 { 90 if (go == null || luafunc == null) return; 91 92 InputField input = go.GetComponent<InputField>(); 93 94 if (input == null) 95 { 96 Debug.LogError(go.name + "找不到InputField組件"); 97 return; 98 } 99 100 go.GetComponent<InputField>().onEndEdit.AddListener( 101 delegate (string text) { 102 luafunc.Call(text); 103 } 104 ); 105 } 106 107 /// <summary> 108 /// 添加對光標按下|擡起事件的支持 109 /// </summary> 110 /// <param name="go">目標對象</param> 111 /// <param name="luafunc">按下事件</param> 112 /// <param name="luafunc2">擡起事件</param> 113 public static void AddPointerDownUpSupport(GameObject go, LuaFunction luafunc, LuaFunction luafunc2) 114 { 115 if (go == null) return; 116 117 EventsSupport es = go.AddComponent<EventsSupport>(); 118 119 es.InitDownUpHandler((PointerEventData pointerEventData) => { 120 if (luafunc != null) 121 { 122 luafunc.Call(go, pointerEventData); 123 } 124 125 }, (PointerEventData pointerEventData) => { 126 if (luafunc2 != null) 127 { 128 luafunc2.Call(go, pointerEventData); 129 } 130 }); 131 } 132 133 /// <summary> 134 /// 給Slider組件添加onValueChanged事件 135 /// </summary> 136 /// <param name="go"></param> 137 /// <param name="luafunc"></param> 138 public static void AddSliderOnChangeEvent(GameObject go, LuaFunction luafunc) 139 { 140 if (go == null || luafunc == null) return; 141 142 Slider component = go.GetComponent<Slider>(); 143 144 if (component == null) 145 { 146 Debug.LogError(go.name + "找不到Slider組件"); 147 return; 148 } 149 150 go.GetComponent<Slider>().onValueChanged.AddListener( 151 delegate (float val) { 152 luafunc.Call(val); 153 } 154 ); 155 } 156 157 //清除監聽 158 public static void ClearButtonClick(GameObject go) 159 { 160 if (go == null) 161 return; 162 163 Button btn = go.GetComponent<Button>(); 164 if (btn == null) 165 return; 166 167 btn.onClick.RemoveAllListeners(); 168 } 169 170 }在Assets/LuaFrameworks/Scripts/Common下,建立一個EventsSupport.cs腳本,該腳本是一個實現了IPointerDownHandler, IPointerUpHandler等接口的類,用於在Lua中檢測鼠標輸入(鼠標點擊,擡起、按下等功能),配合UIEventEx.cs中的AddPointerDownUpSupport方法使用。其代碼以下:c#
EventsSupport.cs1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using UnityEngine; 5 using UnityEngine.EventSystems; 6 7 /* 其它事件可根據須要在此類中實現 8 IPointerEnterHandler - OnPointerEnter - Called when a pointer enters the object 9 IPointerExitHandler - OnPointerExit - Called when a pointer exits the object 10 IPointerDownHandler - OnPointerDown - Called when a pointer is pressed on the object 11 IPointerUpHandler - OnPointerUp - Called when a pointer is released (called on the original the pressed object) 12 IPointerClickHandler - OnPointerClick - Called when a pointer is pressed and released on the same object 13 IInitializePotentialDragHandler - OnInitializePotentialDrag - Called when a drag target is found, can be used to initialise values 14 IBeginDragHandler - OnBeginDrag - Called on the drag object when dragging is about to begin 15 IDragHandler - OnDrag - Called on the drag object when a drag is happening 16 IEndDragHandler - OnEndDrag - Called on the drag object when a drag finishes 17 IDropHandler - OnDrop - Called on the object where a drag finishes 18 IScrollHandler - OnScroll - Called when a mouse wheel scrolls 19 IUpdateSelectedHandler - OnUpdateSelected - Called on the selected object each tick 20 ISelectHandler - OnSelect - Called when the object becomes the selected object 21 IDeselectHandler - OnDeselect - Called on the selected object becomes deselected 22 IMoveHandler - OnMove - Called when a move event occurs (left, right, up, down, ect) 23 ISubmitHandler - OnSubmit - Called when the submit button is pressed 24 ICancelHandler - OnCancel - Called when the cancel button is pressed 25 */ 26 27 /// <summary> 28 /// unity事件支持(本類用於實現Unity中的各類事件,借給Lua調用) 29 /// </summary> 30 public class EventsSupport : MonoBehaviour, IPointerDownHandler, IPointerUpHandler 31 { 32 Action<PointerEventData> onPointerDownHandler = null; 33 Action<PointerEventData> onPointerUpHandler = null; 34 35 public void InitDownUpHandler (Action<PointerEventData> downHandler, Action<PointerEventData> upHandler) 36 { 37 onPointerDownHandler = downHandler; 38 onPointerUpHandler = upHandler; 39 } 40 41 public void OnPointerDown(PointerEventData pointerEventData) 42 { 43 //Output the name of the GameObject that is being clicked 44 //Debug.Log("[" + name + "] Game Object Click in Progress"); 45 46 if (onPointerDownHandler != null) { 47 onPointerDownHandler(pointerEventData); 48 } 49 } 50 51 //Detect if clicks are no longer registering 52 public void OnPointerUp(PointerEventData pointerEventData) 53 { 54 //Debug.Log("[" + name + "] No longer being clicked"); 55 if (onPointerUpHandler != null) 56 { 57 onPointerUpHandler(pointerEventData); 58 } 59 } 60 }EventsSupport.cs腳本須要掛在待檢測輸入的Game Object上。app
二、使用腳本
這裏仍是以上一篇文章寫的登錄界面爲例,以前咱們是經過LuaBehaviour給Button、Toggle以及InputField添加的監聽函數,如今將相應的用法直接替換掉。框架
替換前:ide
替換後:函數
而後運行,看是否生效。oop
......post
運行結果,報錯了,提示全局變量 UIEventEx爲nil(就是這個變量不存在的意思)動畫
看來這樣想固然的方法是行不通了,咱們不能建立一個C#腳本,而後在Lua中直接使用它。
三、C#類導出
在上一步中,咱們發現沒法直接在Lua中使用建立的C#腳本。經過查閱資料瞭解到,對自定義的c#類,若是想在Lua中使用的話,須要作一個導出操做才行。
ToLua的官方git上也有相關的說明:
跟着說明操做:
1)找到Assets\LuaFramework\Editor\下的CustomSettings.cs腳本;
2)在CustomSettings的60 行左右照例添加一個導出語句"_GT(typeof(UIEventEx)),";
3)點擊Lua/Generate All菜單,等到日誌打印 Generate LuaBinder over !字樣時,代表Generate操做已經完成了。
此時查看Assets\LuaFramework\ToLua\Source\Generate,能找到一個叫UIEventExWrap的cs文件,這個就是UIEventEx的導出類。
4)從新運行Unity,已經再也不報錯了,點擊Button、Toggle、在InputField中輸入字符,功能都和以前使用LuaBehaviour時一致。
若是想在Lua中使用自定義的c#類,須要4個步驟:
1)建立c#腳本;
2)在CustomSetting.cs中添加導出語句;
3)點擊Lua/Generate All菜單;
4)在Lua中以全局變量的形式直接使用;
這裏涉及的轉化過程是這樣的:
1)UIEventEx腳本經過Lua/Generate All菜單生成UIEventWrap腳本;
2)UIEventWrap腳本通過ToLua的做用,最終成爲Lua中的一個全局變量UIEventEx;
在以前的文章中咱們曾直接使用Lua/Generate All菜單而未作解釋,那麼如今你應該明白它的作用是什麼了。
至於ToLua怎麼把一個XxxWrap轉換爲Lua中的全局變量,就不是本文能講得清的了(你能夠本身作弄清楚);
Dotween做爲一款很是優秀的緩動動畫插件,基本上快成爲Unity的標配了。而若是想把全部的UI邏輯所有Lua化,那麼在Lua中使用Dotween就是必須的了。
根據前邊的經驗,Dotween相關的類對於ToLua來講,就是自定義類,想要在Lua中使用,就必須作導出操做。
那麼就有如下步驟:
1)給項目導入一個Dotween插件;
2)導出Dotween相關類;
第二步就是要整理Dotween相關的類,而後一個個寫導出語句,這不是一個簡單的活兒。
不過沒必要擔憂,ToLua已經幫咱們作好了。
打開Assets\LuaFramework\Editor\下的CustomSettings.cs腳本,在70~100行左右,能看到Dotween相關類的導出語句,不過因爲未檢測到USING_DOTWEENING宏定義的緣由,這一段代碼並未生效。
3)使用宏定義USING_DOTWEENING
一個簡單的定義宏的辦法是在腳本頭部加入 #define USING_DOTWEENING語句,以下圖
另一個辦法是在PlayerSettings的Scripting Define Symbols*下添加相應的宏,以下圖:
其中ASYNC_MOD是以前有的,兩個宏之間用分號隔開,輸入USING_DOTWEENING 要回車一次,讓腳本從新編譯。
這裏使用第一種辦法。
定義了宏以後,Dotween相關類的導出語句就生效了,而後要執行一次Lua/Generate All。
4)在Lua中做用Dotween
以登錄界面的登錄按鈕爲例,在LoginPanel.lua腳本中添加以下的Dotween使用方法。
而後運行,能看到動畫已經生效了(移動及循環都沒問題),不過最後的回調沒執行。
看日誌有一個報錯,說的是TweenCallback未註冊。這個就是OnComplete回調未執行的緣由。
TweenCallback是一個委託類型,根據此前了知識,委託類型也須要在CustomSetting中指定位置註冊。
打開CustomSettings腳本,在40~50行左右的位置,添加TweenCallback的導出語句"_DT(typeof(DG.Tweening.TweenCallback)),",以下圖所示:
以後從新執行Lua/Generate All菜單(若是有報錯,可先執行一次Clear再執行Generate All)。
如今將循環次數改成1,從新運行。
能看到動畫中止後,指定的日誌已經輸出。
在Lua中使用用Dotween,就是這樣一個步驟。
有一點要注意的是,在Lua中的代碼提示是很不健全的,特別是在調用用C#腳本的時候。
這裏寫Dotween動畫的代碼就是全靠經驗,若是不熟的話,也能夠先用C#寫一遍,再搬到Lua中改造。
文章的前半部分介紹了Lua中調用c#的方法,那麼相應的如何從c#中調用Lua也有必要了解一下。
c#調用Lua是比較少的一個操做,基本上就在框架(LuaFramework)初始化的時候有用到。這裏不作詳細案例,只講一下了解方式。
方式1:
ToLua的Examples, 03_CallLuaFunction,這個腳本詳細講述了c#調用Lua的過程。
方式2:
LuaFramework的LuaManager類,這個腳本里有詳細的調用Main.Lua的過程。
一個疑問:
在寫Lua的Dotween代碼的時候,使用DOLocalMove、SetLoops和OnComplete都是用冒號:的方式,實際上這三個都是static方法,這有違於上一篇文章中總結的靜態方法用點號,成員方法用冒號的規則。
暫不知道緣由,若是你知道,還請留言指教。
------------------------------------
疑問已解決,答案在1樓,感謝 @ 馬三小夥兒 大佬的解答