YouYiSDKAndroid.cs 8.5 KB

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