YouYiSDKAndroid.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #if UNITY_ANDROID
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class YouYiSDKAndroid : SDKBase
  7. {
  8. private AndroidJavaObject m_AJO;
  9. private YouYiListener m_YouYiListener;
  10. public YouYiSDKAndroid()
  11. {
  12. using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  13. {
  14. m_AJO = ajc.GetStatic<AndroidJavaObject>("currentActivity");
  15. }
  16. m_YouYiListener = SDKMgr.Instance.gameObject.AddComponent<YouYiListener>();
  17. SDKName = "SDKHwQuick";
  18. }
  19. public override void Init()
  20. {
  21. base.Init();
  22. if (m_AJO != null)
  23. {
  24. m_AJO.Call("init", "SDKMgr");
  25. }
  26. else
  27. {
  28. m_YouYiListener.OnInitFailed();
  29. }
  30. }
  31. public override void Login()
  32. {
  33. if (m_AJO != null)
  34. {
  35. m_AJO.Call("login");
  36. }
  37. else
  38. {
  39. m_YouYiListener.OnLoginFailed();
  40. }
  41. }
  42. public override void Logout()
  43. {
  44. if (m_AJO != null)
  45. {
  46. m_AJO.Call("switchAccountLogin");
  47. }
  48. }
  49. public override void SwitchAccount()
  50. {
  51. if (m_AJO != null)
  52. {
  53. m_AJO.Call("switchAccountLogin");
  54. }
  55. }
  56. public override void Pay(int goodsId, string goodsName, string goodsDesc, int count, float amount, string cpOrderId, string extrasParams)
  57. {
  58. if (m_AJO != null)
  59. {
  60. GameRoleInfo gameRoleInfo = SDKMgr.Instance.GetGameRoleInfo();
  61. if (gameRoleInfo != null)
  62. m_AJO.Call("pay", cpOrderId, amount.ToString(), goodsId.ToString(), goodsName, gameRoleInfo.roleId.ToString(), gameRoleInfo.roleName, gameRoleInfo.serverId.ToString(), gameRoleInfo.serverName, gameRoleInfo.roleLv, extrasParams);
  63. else
  64. m_YouYiListener.OnPayFailed();
  65. }
  66. else
  67. {
  68. m_YouYiListener.OnPayFailed();
  69. }
  70. }
  71. public override void CreateRole()
  72. {
  73. GameRoleInfo gameRoleInfo = SDKMgr.Instance.GetGameRoleInfo();
  74. if (gameRoleInfo == null || !gameRoleInfo.Valid())
  75. {
  76. Debug.LogError("[YouYiSDK][CreateRole] GameRoleInfo is null or not valid !!!");
  77. return;
  78. }
  79. if (m_AJO != null)
  80. {
  81. m_AJO.Call("createRoleLog", gameRoleInfo.roleId.ToString(), gameRoleInfo.roleName, gameRoleInfo.serverId.ToString(), gameRoleInfo.serverName, gameRoleInfo.roleLv, gameRoleInfo.openServerTime.ToString());
  82. }
  83. }
  84. public override void EnterGame()
  85. {
  86. GameRoleInfo gameRoleInfo = SDKMgr.Instance.GetGameRoleInfo();
  87. if (gameRoleInfo == null || !gameRoleInfo.Valid())
  88. {
  89. Debug.LogError("[YouYiSDK][EnterGame] GameRoleInfo is null or not valid !!!");
  90. return;
  91. }
  92. if (m_AJO != null)
  93. {
  94. m_AJO.Call("loginRoleLog", gameRoleInfo.roleId.ToString(), gameRoleInfo.roleName, gameRoleInfo.serverId.ToString(), gameRoleInfo.serverName, gameRoleInfo.roleLv, gameRoleInfo.openServerTime.ToString());
  95. }
  96. }
  97. public override void UpdateRoleLv()
  98. {
  99. GameRoleInfo gameRoleInfo = SDKMgr.Instance.GetGameRoleInfo();
  100. if (gameRoleInfo == null || !gameRoleInfo.Valid())
  101. {
  102. Debug.LogError("[YouYiSDK][UpdateRoleLv] GameRoleInfo is null or not valid !!!");
  103. return;
  104. }
  105. if (m_AJO != null)
  106. {
  107. m_AJO.Call("levelLog", gameRoleInfo.roleId.ToString(), gameRoleInfo.roleName, gameRoleInfo.serverId.ToString(), gameRoleInfo.serverName, gameRoleInfo.roleLv, gameRoleInfo.openServerTime.ToString());
  108. }
  109. }
  110. public override void ExitGame()
  111. {
  112. }
  113. public override bool Exit()
  114. {
  115. return false;
  116. }
  117. public override bool Quit()
  118. {
  119. return false;
  120. }
  121. public override void CallInitSuccessCB()
  122. {
  123. m_YouYiListener.OnInitSuccess();
  124. }
  125. public override void CallLoginSuccessCB()
  126. {
  127. m_YouYiListener.StartCheckLogin();
  128. }
  129. public override bool CheckHasModul(SDKModulType needCheckModulType)
  130. {
  131. if (needCheckModulType == SDKModulType.EXIT_VIEW)
  132. {
  133. return true;
  134. }
  135. return base.CheckHasModul(needCheckModulType);
  136. }
  137. public override void OpenModul(SDKModulType sDKModulType)
  138. {
  139. if (sDKModulType == SDKModulType.EXIT_VIEW)
  140. {
  141. if (m_AJO != null)
  142. {
  143. m_AJO.Call("exit");
  144. }
  145. return;
  146. }
  147. base.OpenModul(sDKModulType);
  148. }
  149. public override bool IsReportAction() { return true; }
  150. public override void ReportAction(Dictionary<object, object> datas)
  151. {
  152. base.ReportAction(datas);
  153. if (m_AJO != null)
  154. {
  155. using (AndroidJavaObject hashMapAJO = DictionaryToJavaHashMap(datas))
  156. {
  157. m_AJO.Call("reportAction", hashMapAJO);
  158. }
  159. }
  160. }
  161. public override bool HasCanEnterServerJudge() { return true; }
  162. public override void CanEnterServerJudge(string serverId, string serverName)
  163. {
  164. base.CanEnterServerJudge(serverId, serverName);
  165. if (m_AJO != null)
  166. {
  167. m_AJO.Call("canEnterServerJudge", serverId, serverName);
  168. }
  169. }
  170. private AndroidJavaObject DictionaryToJavaHashMap(Dictionary<object, object> datas)
  171. {
  172. AndroidJavaObject hashMapAJO = new AndroidJavaObject("java.util.HashMap");
  173. object[] args = new object[2];
  174. System.IntPtr putMethod = AndroidJNIHelper.GetMethodID(
  175. hashMapAJO.GetRawClass(), "put",
  176. "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
  177. foreach(var data in datas)
  178. {
  179. AndroidJavaObject key = GetAndroidJavaObject(data.Key);
  180. if (key == null) continue;
  181. AndroidJavaObject value = GetAndroidJavaObject(data.Value);
  182. if (value == null) continue;
  183. args[0] = key;
  184. args[1] = value;
  185. AndroidJNI.CallObjectMethod(
  186. hashMapAJO.GetRawObject(),
  187. putMethod,
  188. AndroidJNIHelper.CreateJNIArgArray(args));
  189. key.Dispose();
  190. value.Dispose();
  191. }
  192. return hashMapAJO;
  193. }
  194. private AndroidJavaObject GetAndroidJavaObject(object value)
  195. {
  196. System.Type type = value.GetType();
  197. if (type.IsPrimitive)
  198. {
  199. if (type.Equals(typeof(int)))
  200. {
  201. return new AndroidJavaObject("java.lang.Integer", value);
  202. }
  203. if (type.Equals(typeof(bool)))
  204. {
  205. return new AndroidJavaObject("java.lang.Boolean", value);
  206. }
  207. if (type.Equals(typeof(byte)))
  208. {
  209. return new AndroidJavaObject("java.lang.Byte", value);
  210. }
  211. if (type.Equals(typeof(short)))
  212. {
  213. return new AndroidJavaObject("java.lang.Short", value);
  214. }
  215. if (type.Equals(typeof(long)))
  216. {
  217. return new AndroidJavaObject("java.lang.Long", value);
  218. }
  219. if (type.Equals(typeof(float)))
  220. {
  221. return new AndroidJavaObject("java.lang.Float", value);
  222. }
  223. if (type.Equals(typeof(double)))
  224. {
  225. double dVal = (double)value;
  226. if (dVal > int.MinValue && dVal < int.MaxValue)
  227. {
  228. int intVal = (int)dVal;
  229. if (intVal == dVal)
  230. {
  231. return new AndroidJavaObject("java.lang.Integer", intVal);
  232. }
  233. }
  234. return new AndroidJavaObject("java.lang.Double", value);
  235. }
  236. if (type.Equals(typeof(char)))
  237. {
  238. return new AndroidJavaObject("java.lang.Character", value);
  239. }
  240. }
  241. else
  242. {
  243. if (type.Equals(typeof(string)))
  244. {
  245. return new AndroidJavaObject ("java.lang.String", value);
  246. }
  247. else if (type.Equals(typeof(AndroidJavaObject)))
  248. {
  249. return value as AndroidJavaObject;
  250. }
  251. }
  252. return null;
  253. }
  254. }
  255. #endif