AndroidInteractive.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System;
  4. using UnityEngine;
  5. /// <summary>
  6. /// 安卓原生交互类
  7. /// </summary>
  8. public class AndroidInteractive : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 单键
  12. /// </summary>
  13. private static AndroidInteractive pInit = null;
  14. public static Action<string> LoginSuccessCall = null;
  15. public static Action<string> LoginFailCall = null;
  16. public static Action<string> LogoutCall = null;
  17. public static Action<string> PaySuccessCall = null;
  18. public static Action<string> PayFailCall = null;
  19. /// <summary>
  20. /// 当前版本
  21. /// </summary>
  22. public static string curVersion = "";
  23. /// <summary>
  24. /// 当前年龄
  25. /// </summary>
  26. public static int curAge = -1;
  27. /// <summary>
  28. /// 获取单键
  29. /// </summary>
  30. /// <returns>单键实例</returns>
  31. public static AndroidInteractive Instance()
  32. {
  33. return pInit;
  34. }
  35. /// <summary>
  36. /// 初始化
  37. /// </summary>
  38. private void Awake()
  39. {
  40. // 初始化
  41. pInit = this;
  42. }
  43. /// <summary>
  44. /// SDK登录成功回调
  45. /// </summary>
  46. /// <param name="uid"></param>
  47. private void SDKLoginSuccess(string uid)
  48. {
  49. Debug.Log("SDK 登录成功!");
  50. if (LoginSuccessCall != null)
  51. {
  52. LoginSuccessCall(uid);
  53. }
  54. }
  55. /// <summary>
  56. /// SDK登录失败回调
  57. /// </summary>
  58. /// <param name="uid"></param>
  59. private void SDKLoginFail(string msg)
  60. {
  61. Debug.Log("SDK 登录失败!");
  62. if (LoginFailCall != null)
  63. {
  64. LoginFailCall(msg);
  65. }
  66. }
  67. /// <summary>
  68. /// SDK登出回调
  69. /// </summary>
  70. /// <param name="msg"></param>
  71. private void SDKLogout(string msg)
  72. {
  73. Debug.Log("SDK 登出成功!");
  74. if (LogoutCall != null)
  75. {
  76. LogoutCall(msg);
  77. }
  78. }
  79. /// <summary>
  80. /// SDK支付成功回调
  81. /// </summary>
  82. /// <param name="oderID"></param>
  83. private void SDKPaySuccess(string oderID)
  84. {
  85. if (PaySuccessCall != null)
  86. {
  87. PaySuccessCall(oderID);
  88. }
  89. }
  90. /// <summary>
  91. /// SDK支付失败回调
  92. /// </summary>
  93. /// <param name="oderID"></param>
  94. private void SDKPayFail(string msg)
  95. {
  96. if (PayFailCall != null)
  97. {
  98. PayFailCall(msg);
  99. }
  100. }
  101. /// <summary>
  102. /// 查询年龄段
  103. /// </summary>
  104. public int GetUserAge()
  105. {
  106. if(curAge != -1)
  107. {
  108. return curAge;
  109. }
  110. try
  111. {
  112. using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  113. {
  114. using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
  115. {
  116. //// 获取平台名字
  117. //string PlatformName = ajo.Call<string>("GetPlatformName");
  118. // 获取当前版本
  119. string strAge = ajo.Call<string>("GetUserAge");
  120. Debug.Log("获取当前年龄__________________________" + strAge);
  121. curAge = int.Parse(strAge);
  122. UserProxy.Instance.player.ageRange = curAge;
  123. return curAge;
  124. }
  125. }
  126. }
  127. catch (Exception exc)
  128. {
  129. curAge = 99;
  130. return curAge;
  131. }
  132. }
  133. // 获取Version Name
  134. public String GetVersionName()
  135. {
  136. if (curVersion != "")
  137. {
  138. return curVersion;
  139. }
  140. try
  141. {
  142. using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  143. {
  144. using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
  145. {
  146. //// 获取平台名字
  147. //string PlatformName = ajo.Call<string>("GetPlatformName");
  148. // 获取当前版本
  149. curVersion = ajo.Call<string>("GetVersionName");
  150. Debug.Log("获取当前版本__________________________" + curVersion);
  151. return curVersion;
  152. }
  153. }
  154. }
  155. catch (Exception exc)
  156. {
  157. curVersion = "unity";
  158. return curVersion;
  159. }
  160. }
  161. /// <summary>
  162. /// 调用Java层函数
  163. /// </summary>
  164. public static bool CallJavaFunction(string className)
  165. {
  166. if (Application.platform == RuntimePlatform.Android)
  167. {
  168. try
  169. {
  170. using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  171. {
  172. using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
  173. {
  174. ajo.Call(className);
  175. return true;
  176. }
  177. }
  178. }
  179. catch (Exception exc)
  180. {
  181. Debug.LogError("调用《" + className + "》异常: " + exc);
  182. }
  183. }
  184. return false;
  185. }
  186. /// <summary>
  187. /// 调用Java层函数
  188. /// </summary>
  189. public static bool CallJavaFunction(string funName, string Parameter1)
  190. {
  191. if (Application.platform == RuntimePlatform.Android)
  192. {
  193. try
  194. {
  195. using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  196. {
  197. using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
  198. {
  199. ajo.Call(funName, Parameter1);
  200. return true;
  201. }
  202. }
  203. }
  204. catch (Exception exc)
  205. {
  206. Debug.LogError("调用《" + funName + "》异常: " + exc);
  207. }
  208. }
  209. return false;
  210. }
  211. /// <summary>
  212. /// 调用Java层函数
  213. /// </summary>
  214. public static bool CallJavaFunction(string funName, string Parameter1, string Parameter2)
  215. {
  216. if (Application.platform == RuntimePlatform.Android)
  217. {
  218. try
  219. {
  220. using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  221. {
  222. using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
  223. {
  224. ajo.Call(funName, Parameter1, Parameter2);
  225. return true;
  226. }
  227. }
  228. }
  229. catch (Exception exc)
  230. {
  231. Debug.LogError("调用《" + funName + "》异常: " + exc);
  232. }
  233. }
  234. return false;
  235. }
  236. /// <summary>
  237. /// 调用Java层函数
  238. /// </summary>
  239. public static bool CallJavaFunction(string funName, string Parameter1, string Parameter2, string Parameter3)
  240. {
  241. if (Application.platform == RuntimePlatform.Android)
  242. {
  243. try
  244. {
  245. using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  246. {
  247. using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
  248. {
  249. ajo.Call(funName, Parameter1, Parameter2, Parameter3);
  250. return true;
  251. }
  252. }
  253. }
  254. catch (Exception exc)
  255. {
  256. Debug.LogError("调用《" + funName + "》异常: " + exc);
  257. }
  258. }
  259. return false;
  260. }
  261. /// <summary>
  262. /// 调用Java层函数
  263. /// </summary>
  264. public static bool CallJavaFunction(string funName, string Parameter1, string Parameter2, string Parameter3, string Parameter4)
  265. {
  266. if (Application.platform == RuntimePlatform.Android)
  267. {
  268. try
  269. {
  270. using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  271. {
  272. using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
  273. {
  274. ajo.Call(funName, Parameter1, Parameter2, Parameter3, Parameter4);
  275. return true;
  276. }
  277. }
  278. }
  279. catch (Exception exc)
  280. {
  281. Debug.LogError("调用《" + funName + "》异常: " + exc);
  282. }
  283. }
  284. return false;
  285. }
  286. }