Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween

在上一篇文章 Unity3D热更新之LuaFramework篇[04]--自定义UI监听方法 中,我对LuaBehaviour脚本进行了扩展,添加了两个新的UI监听方法,也提到最好能单写一个脚本处理此事。本篇文章就来继续这个工作。

 

从Lua中调用C#代码 1、创建UI监听脚本

 打开之前的工程,在Assets/LuaFrameworks/Scripts/Common下,创建一个UIEventEx.cs脚本,将LuaBehaviour.cs中的AddButtonClick以及AddInputFieldEndEditHandler方法迁移过来,并扩展了一些其它方法,代码如下:

Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween

Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween

1 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>目标对象</param> 111 /// <param>按下事件</param> 112 /// <param>抬起事件</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></param> 137 /// <param></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 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxjwx.html