YouYiSDKAndroid.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. int itemp = Mathf.FloorToInt(amount);
  67. string pice = "";
  68. if ((itemp + 0.005f) > amount)
  69. {
  70. pice = itemp.ToString();
  71. }
  72. else
  73. {
  74. pice = amount.ToString("F2");
  75. }
  76. if (cfgs.ContainsKey(pice))
  77. {
  78. SDKMgr.ProductInfo productInfo = cfgs[pice];
  79. 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);
  80. }
  81. else
  82. m_YouYiListener.OnPayFailed();
  83. }
  84. else
  85. m_YouYiListener.OnPayFailed();
  86. }
  87. else
  88. {
  89. m_YouYiListener.OnPayFailed();
  90. }
  91. }
  92. public override void CreateRole()
  93. {
  94. GameRoleInfo gameRoleInfo = SDKMgr.Instance.GetGameRoleInfo();
  95. if (gameRoleInfo == null || !gameRoleInfo.Valid())
  96. {
  97. Debug.LogError("[YouYiSDK][CreateRole] GameRoleInfo is null or not valid !!!");
  98. return;
  99. }
  100. if (m_AJO != null)
  101. {
  102. m_AJO.Call("createRoleLog", gameRoleInfo.roleId.ToString(), gameRoleInfo.roleName, gameRoleInfo.serverId.ToString(), gameRoleInfo.serverName, gameRoleInfo.roleLv, gameRoleInfo.openServerTime.ToString());
  103. }
  104. }
  105. public override void EnterGame()
  106. {
  107. GameRoleInfo gameRoleInfo = SDKMgr.Instance.GetGameRoleInfo();
  108. if (gameRoleInfo == null || !gameRoleInfo.Valid())
  109. {
  110. Debug.LogError("[YouYiSDK][EnterGame] GameRoleInfo is null or not valid !!!");
  111. return;
  112. }
  113. if (m_AJO != null)
  114. {
  115. m_AJO.Call("loginRoleLog", gameRoleInfo.roleId.ToString(), gameRoleInfo.roleName, gameRoleInfo.serverId.ToString(), gameRoleInfo.serverName, gameRoleInfo.roleLv, gameRoleInfo.openServerTime.ToString());
  116. }
  117. }
  118. public override void UpdateRoleLv()
  119. {
  120. GameRoleInfo gameRoleInfo = SDKMgr.Instance.GetGameRoleInfo();
  121. if (gameRoleInfo == null || !gameRoleInfo.Valid())
  122. {
  123. Debug.LogError("[YouYiSDK][UpdateRoleLv] GameRoleInfo is null or not valid !!!");
  124. return;
  125. }
  126. if (m_AJO != null)
  127. {
  128. m_AJO.Call("levelLog", gameRoleInfo.roleId.ToString(), gameRoleInfo.roleName, gameRoleInfo.serverId.ToString(), gameRoleInfo.serverName, gameRoleInfo.roleLv, gameRoleInfo.openServerTime.ToString());
  129. }
  130. }
  131. public override void ExitGame()
  132. {
  133. }
  134. public override bool Exit()
  135. {
  136. return false;
  137. }
  138. public override bool Quit()
  139. {
  140. return false;
  141. }
  142. public override void CallInitSuccessCB()
  143. {
  144. m_YouYiListener.OnInitSuccess();
  145. }
  146. public override void CallLoginSuccessCB()
  147. {
  148. m_YouYiListener.StartCheckLogin();
  149. }
  150. public override bool CheckHasModul(SDKModulType needCheckModulType)
  151. {
  152. if (needCheckModulType == SDKModulType.EXIT_VIEW)
  153. {
  154. return true;
  155. }
  156. return base.CheckHasModul(needCheckModulType);
  157. }
  158. public override void OpenModul(SDKModulType sDKModulType)
  159. {
  160. if (sDKModulType == SDKModulType.EXIT_VIEW)
  161. {
  162. if (m_AJO != null)
  163. {
  164. m_AJO.Call("exit");
  165. }
  166. return;
  167. }
  168. base.OpenModul(sDKModulType);
  169. }
  170. public override bool IsReportAction() { return true; }
  171. public override void ReportAction(Dictionary<object, object> datas)
  172. {
  173. base.ReportAction(datas);
  174. if (m_AJO != null)
  175. {
  176. using (AndroidJavaObject hashMapAJO = DictionaryToJavaHashMap(datas))
  177. {
  178. m_AJO.Call("reportAction", hashMapAJO);
  179. }
  180. }
  181. }
  182. public override bool HasCanEnterServerJudge() { return true; }
  183. public override void CanEnterServerJudge(string serverId, string serverName)
  184. {
  185. base.CanEnterServerJudge(serverId, serverName);
  186. if (m_AJO != null)
  187. {
  188. m_AJO.Call("canEnterServerJudge", serverId, serverName);
  189. }
  190. }
  191. private AndroidJavaObject DictionaryToJavaHashMap(Dictionary<object, object> datas)
  192. {
  193. AndroidJavaObject hashMapAJO = new AndroidJavaObject("java.util.HashMap");
  194. object[] args = new object[2];
  195. System.IntPtr putMethod = AndroidJNIHelper.GetMethodID(
  196. hashMapAJO.GetRawClass(), "put",
  197. "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
  198. foreach (var data in datas)
  199. {
  200. AndroidJavaObject key = GetAndroidJavaObject(data.Key);
  201. if (key == null) continue;
  202. AndroidJavaObject value = GetAndroidJavaObject(data.Value);
  203. if (value == null) continue;
  204. args[0] = key;
  205. args[1] = value;
  206. AndroidJNI.CallObjectMethod(
  207. hashMapAJO.GetRawObject(),
  208. putMethod,
  209. AndroidJNIHelper.CreateJNIArgArray(args));
  210. key.Dispose();
  211. value.Dispose();
  212. }
  213. return hashMapAJO;
  214. }
  215. private AndroidJavaObject GetAndroidJavaObject(object value)
  216. {
  217. System.Type type = value.GetType();
  218. if (type.IsPrimitive)
  219. {
  220. if (type.Equals(typeof(int)))
  221. {
  222. return new AndroidJavaObject("java.lang.Integer", value);
  223. }
  224. if (type.Equals(typeof(bool)))
  225. {
  226. return new AndroidJavaObject("java.lang.Boolean", value);
  227. }
  228. if (type.Equals(typeof(byte)))
  229. {
  230. return new AndroidJavaObject("java.lang.Byte", value);
  231. }
  232. if (type.Equals(typeof(short)))
  233. {
  234. return new AndroidJavaObject("java.lang.Short", value);
  235. }
  236. if (type.Equals(typeof(long)))
  237. {
  238. return new AndroidJavaObject("java.lang.Long", value);
  239. }
  240. if (type.Equals(typeof(float)))
  241. {
  242. return new AndroidJavaObject("java.lang.Float", value);
  243. }
  244. if (type.Equals(typeof(double)))
  245. {
  246. double dVal = (double)value;
  247. if (dVal > int.MinValue && dVal < int.MaxValue)
  248. {
  249. int intVal = (int)dVal;
  250. if (intVal == dVal)
  251. {
  252. return new AndroidJavaObject("java.lang.Integer", intVal);
  253. }
  254. }
  255. return new AndroidJavaObject("java.lang.Double", value);
  256. }
  257. if (type.Equals(typeof(char)))
  258. {
  259. return new AndroidJavaObject("java.lang.Character", value);
  260. }
  261. }
  262. else
  263. {
  264. if (type.Equals(typeof(string)))
  265. {
  266. return new AndroidJavaObject("java.lang.String", value);
  267. }
  268. else if (type.Equals(typeof(AndroidJavaObject)))
  269. {
  270. return value as AndroidJavaObject;
  271. }
  272. }
  273. return null;
  274. }
  275. }
  276. }
  277. #endif