JavaProxy.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using UnityEngine;
  2. using System;
  3. using System.Reflection;
  4. namespace MRStore.Util
  5. {
  6. /// <summary>
  7. /// 通用型接口
  8. /// </summary>
  9. public interface JavaInterface
  10. {
  11. /// <summary>
  12. /// 无参函数
  13. /// </summary>
  14. /// <param name="method"></param>
  15. void Call(string method);
  16. /// <summary>
  17. /// 有参函数
  18. /// </summary>
  19. /// <param name="method"></param>
  20. /// <param name="args"></param>
  21. void Call(string method, string args);
  22. }
  23. /// <summary>
  24. /// 和安卓Java进行交互的方式
  25. /// Java端调用C#代码的方案
  26. /// Java声明接口,由Unity实现相关的代码
  27. /// 重点是,如何设计一个通用的类来实现相关的功能?
  28. ///
  29. /// </summary>
  30. public abstract class JavaProxy<T> : AndroidJavaProxy, JavaInterface where T : JavaProxy<T>
  31. {
  32. /// <summary>
  33. /// com.shadowcreator.mediasouptool.JavaInterface
  34. /// </summary>
  35. /// <param name="packName"></param>
  36. public JavaProxy(string packName) : base(packName) { }
  37. public void Call(string method)
  38. {
  39. MethodInfo methodInfo = typeof(T).GetMethod(method);
  40. if (methodInfo == null) throw new Exception(typeof(T) + method + "method is null");
  41. methodInfo.Invoke(this, null);
  42. }
  43. public void Call(string method, string args)
  44. {
  45. MethodInfo methodInfo = typeof(T).GetMethod(method);
  46. if (methodInfo == null) throw new Exception(typeof(T) + method + "method is null");
  47. methodInfo.Invoke(this, new System.Object[] { args });
  48. }
  49. }
  50. }