YouYiSDKAndroid.cs 9.6 KB

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