YouYiSDKAndroid.cs 8.8 KB

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