GenericMethod

GenericMethodExample.cs

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

Foo.cs

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

?