xLua——C#扩展方法与xLua泛型

作者:追风剑情 发布于:2017-7-19 21:20 分类:Lua

以下代码来自xLua demo


  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using XLua;
  6.  
  7. [LuaCallCSharp]
  8. public class Foo1Parent
  9. {
  10. }
  11.  
  12. [LuaCallCSharp]
  13. public class Foo2Parent
  14. {
  15. }
  16.  
  17. [LuaCallCSharp]
  18. public class Foo1Child : Foo1Parent
  19. {
  20. }
  21.  
  22. [LuaCallCSharp]
  23. public class Foo2Child : Foo2Parent
  24. {
  25. }
  26.  
  27. [LuaCallCSharp]
  28. public class Foo
  29. {
  30. #region Supported methods
  31.  
  32. public void Test1<T>(T a) where T : Foo1Parent
  33. {
  34. Debug.Log(string.Format("Test1<{0}>", typeof (T)));
  35. }
  36.  
  37. public T1 Test2<T1, T2>(T1 a, T2 b, GameObject c) where T1 : Foo1Parent where T2 : Foo2Parent
  38. {
  39. Debug.Log(string.Format("Test2<{0},{1}>", typeof (T1), typeof (T2)), c);
  40. return a;
  41. }
  42.  
  43. #endregion
  44.  
  45. #region Unsupported methods
  46.  
  47. /// <summary>
  48. /// 不支持生成lua的泛型方法(没有泛型约束)
  49. /// </summary>
  50. public void UnsupportedMethod1<T>(T a)
  51. {
  52. Debug.Log("UnsupportedMethod1");
  53. }
  54.  
  55. /// <summary>
  56. /// 不支持生成lua的泛型方法(缺少带约束的泛型参数)
  57. /// </summary>
  58. public void UnsupportedMethod2<T>() where T : Foo1Parent
  59. {
  60. Debug.Log(string.Format("UnsupportedMethod2<{0}>",typeof(T)));
  61. }
  62.  
  63. /// <summary>
  64. /// 不支持生成lua的泛型方法(泛型约束必须为class)
  65. /// </summary>
  66. public void UnsupportedMethod3<T>(T a) where T : IDisposable
  67. {
  68. Debug.Log(string.Format("UnsupportedMethod3<{0}>", typeof(T)));
  69. }
  70.  
  71. #endregion
  72. }
  73.  
  74. [LuaCallCSharp]
  75. public static class FooExtension
  76. {
  77. public static void PlainExtension(this Foo1Parent a)
  78. {
  79. Debug.Log("PlainExtension");
  80. }
  81.  
  82. public static T Extension1<T>(this T a) where T : Foo1Parent
  83. {
  84. Debug.Log(string.Format("Extension1<{0}>", typeof (T)));
  85. return a;
  86. }
  87.  
  88. public static T Extension2<T>(this T a, GameObject b) where T : Foo1Parent
  89. {
  90. Debug.Log(string.Format("Extension2<{0}>", typeof (T)), b);
  91. return a;
  92. }
  93.  
  94. public static void Extension2<T1, T2>(this T1 a, T2 b) where T1 : Foo1Parent where T2 : Foo2Parent
  95. {
  96. Debug.Log(string.Format("Extension2<{0},{1}>", typeof (T1), typeof (T2)));
  97. }
  98.  
  99. public static T UnsupportedExtension<T>(this GameObject obj) where T : Component
  100. {
  101. return obj.GetComponent<T>();
  102. }
  103. }

  1. using UnityEngine;
  2. using XLua;
  3.  
  4. public class GenericMethodExample : MonoBehaviour
  5. {
  6. private const string script = @"
  7. local foo1 = CS.Foo1Child()
  8. local foo2 = CS.Foo2Child()
  9.  
  10. local obj = CS.UnityEngine.GameObject()
  11. foo1:PlainExtension()
  12. foo1:Extension1()
  13. foo1:Extension2(obj) -- overload1
  14. foo1:Extension2(foo2) -- overload2
  15. local foo = CS.Foo()
  16. foo:Test1(foo1)
  17. foo:Test2(foo1,foo2,obj)
  18. ";
  19. private LuaEnv env;
  20.  
  21. private void Start()
  22. {
  23. env = new LuaEnv();
  24. env.DoString(script);
  25. }
  26.  
  27. private void Update()
  28. {
  29. if (env != null)
  30. env.Tick();
  31. }
  32.  
  33. private void OnDestroy()
  34. {
  35. env.Dispose();
  36. }
  37. }


运行测试

111111.png

标签: xLua

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号