UnityToJava.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using static TestFile;
  6. public class UnityToJava
  7. {
  8. private static AndroidJavaClass _yiYanClass;
  9. private static AndroidJavaClass _jumpAPPClass;
  10. private static AndroidJavaClass _SpeekClass;
  11. private static AndroidJavaClass _DuerBaseClass;
  12. private static AndroidJavaObject mCurrentActivity = null;
  13. private static AndroidJavaClass mUnityPlayerClass = null;
  14. private static AndroidJavaObject mContext = null;
  15. public static AndroidJavaObject CurrentActivity
  16. {
  17. get
  18. {
  19. if (Application.platform == RuntimePlatform.Android)
  20. {
  21. if (mCurrentActivity == null && UnityPlayerClass != null)
  22. {
  23. mCurrentActivity = UnityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
  24. }
  25. }
  26. return mCurrentActivity;
  27. }
  28. }
  29. public static AndroidJavaClass UnityPlayerClass
  30. {
  31. get
  32. {
  33. if (Application.platform == RuntimePlatform.Android)
  34. {
  35. if (mUnityPlayerClass == null)
  36. {
  37. mUnityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  38. }
  39. }
  40. return mUnityPlayerClass;
  41. }
  42. }
  43. public static AndroidJavaObject Context
  44. {
  45. get
  46. {
  47. if (Application.platform == RuntimePlatform.Android)
  48. {
  49. if (mContext == null)
  50. {
  51. mContext = CurrentActivityFunctionCall<AndroidJavaObject>("getApplicationContext");
  52. }
  53. }
  54. return mContext;
  55. }
  56. }
  57. public static T CurrentActivityFunctionCall<T>(string callName, params object[] args)
  58. {
  59. if (CurrentActivity != null)
  60. {
  61. try
  62. {
  63. return CurrentActivity.Call<T>(callName, args);
  64. }
  65. catch (Exception e)
  66. {
  67. Debug.LogError(e);
  68. }
  69. }
  70. return default(T);
  71. }
  72. private static AndroidJavaClass jumpAPPClass
  73. {
  74. get
  75. {
  76. if (_jumpAPPClass == null)
  77. {
  78. _jumpAPPClass = new AndroidJavaClass("com.tools.AppLock.AppLock");
  79. }
  80. return _jumpAPPClass;
  81. }
  82. }
  83. public static AndroidJavaClass yiYanClass
  84. {
  85. get
  86. {
  87. if (_yiYanClass == null)
  88. {
  89. _yiYanClass = new AndroidJavaClass("com.xiaodu.dueros.magictool.demo.YiyanDemoActivity");
  90. }
  91. return _yiYanClass;
  92. }
  93. }
  94. private static AndroidJavaClass SpeekClass
  95. {
  96. get
  97. {
  98. if (_SpeekClass == null)
  99. {
  100. _SpeekClass = new AndroidJavaClass("com.example.speekaar.SpeekManager");
  101. }
  102. return _SpeekClass;
  103. }
  104. }
  105. public static void startSpeekText(string str)
  106. {
  107. Debug.Log("startSpeekText==>");
  108. SpeekClass.CallStatic("startSpeekText",str);
  109. }
  110. public static void startWebSocketSpeek()
  111. {
  112. Debug.Log("startWebSocketSpeek==>");
  113. SpeekClass.CallStatic("startWebSocketSpeek");
  114. }
  115. public static void stopSpeek()
  116. {
  117. Debug.Log("stopSpeek==>");
  118. SpeekClass.CallStatic("stopSpeek");
  119. }
  120. public static void initSpeek(YiYanBack fh)
  121. {
  122. yiYanClass.CallStatic("init", Context);
  123. yiYanClass.CallStatic("SetUnityHandler", fh);
  124. }
  125. public static void SpeekOffString(string str)
  126. {
  127. yiYanClass.Call("SpeekOffString", str);
  128. }
  129. }