LuaCallCs.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Tencent is pleased to support the open source community by making xLua available.
  3. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  4. * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  5. * http://opensource.org/licenses/MIT
  6. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  7. */
  8. using UnityEngine;
  9. using System.Collections;
  10. using System;
  11. using XLua;
  12. using System.Collections.Generic;
  13. namespace Tutorial
  14. {
  15. [LuaCallCSharp]
  16. public class BaseClass
  17. {
  18. public static void BSFunc()
  19. {
  20. Debug.Log("Derived Static Func, BSF = " + BSF);
  21. }
  22. public static int BSF = 1;
  23. public void BMFunc()
  24. {
  25. Debug.Log("Derived Member Func, BMF = " + BMF);
  26. }
  27. public int BMF { get; set; }
  28. }
  29. public struct Param1
  30. {
  31. public int x;
  32. public string y;
  33. }
  34. [LuaCallCSharp]
  35. public enum TestEnum
  36. {
  37. E1,
  38. E2
  39. }
  40. [LuaCallCSharp]
  41. public class DerivedClass : BaseClass
  42. {
  43. [LuaCallCSharp]
  44. public enum TestEnumInner
  45. {
  46. E3,
  47. E4
  48. }
  49. public void DMFunc()
  50. {
  51. Debug.Log("Derived Member Func, DMF = " + DMF);
  52. }
  53. public int DMF { get; set; }
  54. public double ComplexFunc(Param1 p1, ref int p2, out string p3, Action luafunc, out Action csfunc)
  55. {
  56. Debug.Log("P1 = {x=" + p1.x + ",y=" + p1.y + "},p2 = " + p2);
  57. luafunc();
  58. p2 = p2 * p1.x;
  59. p3 = "hello " + p1.y;
  60. csfunc = () =>
  61. {
  62. Debug.Log("csharp callback invoked!");
  63. };
  64. return 1.23;
  65. }
  66. public void TestFunc(int i)
  67. {
  68. Debug.Log("TestFunc(int i)");
  69. }
  70. public void TestFunc(string i)
  71. {
  72. Debug.Log("TestFunc(string i)");
  73. }
  74. public static DerivedClass operator +(DerivedClass a, DerivedClass b)
  75. {
  76. DerivedClass ret = new DerivedClass();
  77. ret.DMF = a.DMF + b.DMF;
  78. return ret;
  79. }
  80. public void DefaultValueFunc(int a = 100, string b = "cccc", string c = null)
  81. {
  82. UnityEngine.Debug.Log("DefaultValueFunc: a=" + a + ",b=" + b + ",c=" + c);
  83. }
  84. public void VariableParamsFunc(int a, params string[] strs)
  85. {
  86. UnityEngine.Debug.Log("VariableParamsFunc: a =" + a);
  87. foreach (var str in strs)
  88. {
  89. UnityEngine.Debug.Log("str:" + str);
  90. }
  91. }
  92. public TestEnum EnumTestFunc(TestEnum e)
  93. {
  94. Debug.Log("EnumTestFunc: e=" + e);
  95. return TestEnum.E2;
  96. }
  97. public Action<string> TestDelegate = (param) =>
  98. {
  99. Debug.Log("TestDelegate in c#:" + param);
  100. };
  101. public event Action TestEvent;
  102. public void CallEvent()
  103. {
  104. TestEvent();
  105. }
  106. public ulong TestLong(long n)
  107. {
  108. return (ulong)(n + 1);
  109. }
  110. class InnerCalc : ICalc
  111. {
  112. public int add(int a, int b)
  113. {
  114. return a + b;
  115. }
  116. public int id = 100;
  117. }
  118. public ICalc GetCalc()
  119. {
  120. return new InnerCalc();
  121. }
  122. public void GenericMethod<T>()
  123. {
  124. Debug.Log("GenericMethod<" + typeof(T) + ">");
  125. }
  126. }
  127. [LuaCallCSharp]
  128. public interface ICalc
  129. {
  130. int add(int a, int b);
  131. }
  132. [LuaCallCSharp]
  133. public static class DerivedClassExtensions
  134. {
  135. public static int GetSomeData(this DerivedClass obj)
  136. {
  137. Debug.Log("GetSomeData ret = " + obj.DMF);
  138. return obj.DMF;
  139. }
  140. public static int GetSomeBaseData(this BaseClass obj)
  141. {
  142. Debug.Log("GetSomeBaseData ret = " + obj.BMF);
  143. return obj.BMF;
  144. }
  145. public static void GenericMethodOfString(this DerivedClass obj)
  146. {
  147. obj.GenericMethod<string>();
  148. }
  149. }
  150. }
  151. public class LuaCallCs : MonoBehaviour
  152. {
  153. LuaEnv luaenv = null;
  154. string script = @"
  155. function demo()
  156. --new C#对象
  157. local newGameObj = CS.UnityEngine.GameObject()
  158. local newGameObj2 = CS.UnityEngine.GameObject('helloworld')
  159. print(newGameObj, newGameObj2)
  160. --访问静态属性,方法
  161. local GameObject = CS.UnityEngine.GameObject
  162. print('UnityEngine.Time.deltaTime:', CS.UnityEngine.Time.deltaTime) --读静态属性
  163. CS.UnityEngine.Time.timeScale = 0.5 --写静态属性
  164. print('helloworld', GameObject.Find('helloworld')) --静态方法调用
  165. --访问成员属性,方法
  166. local DerivedClass = CS.Tutorial.DerivedClass
  167. local testobj = DerivedClass()
  168. testobj.DMF = 1024--设置成员属性
  169. print(testobj.DMF)--读取成员属性
  170. testobj:DMFunc()--成员方法
  171. --基类属性,方法
  172. print(DerivedClass.BSF)--读基类静态属性
  173. DerivedClass.BSF = 2048--写基类静态属性
  174. DerivedClass.BSFunc();--基类静态方法
  175. print(testobj.BMF)--读基类成员属性
  176. testobj.BMF = 4096--写基类成员属性
  177. testobj:BMFunc()--基类方法调用
  178. --复杂方法调用
  179. local ret, p2, p3, csfunc = testobj:ComplexFunc({x=3, y = 'john'}, 100, function()
  180. print('i am lua callback')
  181. end)
  182. print('ComplexFunc ret:', ret, p2, p3, csfunc)
  183. csfunc()
  184. --重载方法调用
  185. testobj:TestFunc(100)
  186. testobj:TestFunc('hello')
  187. --操作符
  188. local testobj2 = DerivedClass()
  189. testobj2.DMF = 2048
  190. print('(testobj + testobj2).DMF = ', (testobj + testobj2).DMF)
  191. --默认值
  192. testobj:DefaultValueFunc(1)
  193. testobj:DefaultValueFunc(3, 'hello', 'john')
  194. --可变参数
  195. testobj:VariableParamsFunc(5, 'hello', 'john')
  196. --Extension methods
  197. print(testobj:GetSomeData())
  198. print(testobj:GetSomeBaseData()) --访问基类的Extension methods
  199. testobj:GenericMethodOfString() --通过Extension methods实现访问泛化方法
  200. --枚举类型
  201. local e = testobj:EnumTestFunc(CS.Tutorial.TestEnum.E1)
  202. print(e, e == CS.Tutorial.TestEnum.E2)
  203. print(CS.Tutorial.TestEnum.__CastFrom(1), CS.Tutorial.TestEnum.__CastFrom('E1'))
  204. print(CS.Tutorial.DerivedClass.TestEnumInner.E3)
  205. assert(CS.Tutorial.BaseClass.TestEnumInner == nil)
  206. --Delegate
  207. testobj.TestDelegate('hello') --直接调用
  208. local function lua_delegate(str)
  209. print('TestDelegate in lua:', str)
  210. end
  211. testobj.TestDelegate = lua_delegate + testobj.TestDelegate --combine,这里演示的是C#delegate作为右值,左值也支持
  212. testobj.TestDelegate('hello')
  213. testobj.TestDelegate = testobj.TestDelegate - lua_delegate --remove
  214. testobj.TestDelegate('hello')
  215. --事件
  216. local function lua_event_callback1() print('lua_event_callback1') end
  217. local function lua_event_callback2() print('lua_event_callback2') end
  218. testobj:TestEvent('+', lua_event_callback1)
  219. testobj:CallEvent()
  220. testobj:TestEvent('+', lua_event_callback2)
  221. testobj:CallEvent()
  222. testobj:TestEvent('-', lua_event_callback1)
  223. testobj:CallEvent()
  224. testobj:TestEvent('-', lua_event_callback2)
  225. --64位支持
  226. local l = testobj:TestLong(11)
  227. print(type(l), l, l + 100, 10000 + l)
  228. --typeof
  229. newGameObj:AddComponent(typeof(CS.UnityEngine.ParticleSystem))
  230. --cast
  231. local calc = testobj:GetCalc()
  232. print('assess instance of InnerCalc via reflection', calc:add(1, 2))
  233. assert(calc.id == 100)
  234. cast(calc, typeof(CS.Tutorial.ICalc))
  235. print('cast to interface ICalc', calc:add(1, 2))
  236. assert(calc.id == nil)
  237. end
  238. demo()
  239. --协程下使用
  240. local co = coroutine.create(function()
  241. print('------------------------------------------------------')
  242. demo()
  243. end)
  244. assert(coroutine.resume(co))
  245. ";
  246. // Use this for initialization
  247. void Start()
  248. {
  249. luaenv = new LuaEnv();
  250. luaenv.DoString(script);
  251. }
  252. // Update is called once per frame
  253. void Update()
  254. {
  255. if (luaenv != null)
  256. {
  257. luaenv.Tick();
  258. }
  259. }
  260. void OnDestroy()
  261. {
  262. luaenv.Dispose();
  263. }
  264. }