AnySDKIAP.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System;
  7. namespace anysdk {
  8. public enum PayResultCode
  9. {
  10. kPaySuccess = 0,/**< enum value is callback of succeeding in paying . */
  11. kPayFail,/**< enum value is callback of failing to pay . */
  12. kPayCancel,/**< enum value is callback of canceling to pay . */
  13. kPayNetworkError,/**< enum value is callback of network error . */
  14. kPayProductionInforIncomplete,/**< enum value is callback of incompleting info . */
  15. kPayInitSuccess,/**< enum value is callback of succeeding in initing sdk . */
  16. kPayInitFail,/**< enum value is callback of failing to init sdk . */
  17. kPayNowPaying,/**< enum value is callback of paying now . */
  18. kPayRechargeSuccess,/**< enum value is callback of succeeding in recharging. */
  19. kPayExtension = 30000 /**< enum value is extension code . */
  20. } ;
  21. /** @brief RequestResultCode enum, with inline docs */
  22. public enum RequestResultCode
  23. {
  24. kRequestSuccess = 31000,/**< enum value is callback of succeeding in paying . */
  25. kRequestFail/**< enum value is callback of failing to pay . */
  26. } ;
  27. public class AnySDKIAP
  28. {
  29. private static AnySDKIAP _instance;
  30. public static AnySDKIAP getInstance() {
  31. if( null == _instance ) {
  32. _instance = new AnySDKIAP();
  33. }
  34. return _instance;
  35. }
  36. /**
  37. @brief pay for product
  38. @param info The info of product, must contains key:
  39. @warning Look at the manual of plugins.
  40. */
  41. public void payForProduct(Dictionary<string,string> info,string pluginId = "")
  42. {
  43. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  44. string value = AnySDKUtil.dictionaryToString (info);
  45. AnySDKIAP_nativePayForProduct(value,pluginId);
  46. #else
  47. Debug.Log("This platform does not support!");
  48. #endif
  49. }
  50. /**
  51. @brief get order id
  52. @return the order id
  53. */
  54. public string getOrderId(string pluginId = "")
  55. {
  56. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  57. StringBuilder value = new StringBuilder ();
  58. value.Capacity = AnySDKUtil.MAX_CAPACITY_NUM;
  59. AnySDKIAP_nativeGetOrderId (value,pluginId);
  60. return value.ToString ();
  61. #else
  62. Debug.Log("This platform does not support!");
  63. return "";
  64. #endif
  65. }
  66. /**
  67. @brief Check function the plugin support or not
  68. */
  69. public bool isFunctionSupported (string functionName, string pluginId = "")
  70. {
  71. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  72. return AnySDKIAP_nativeIsFunctionSupported (functionName,pluginId);
  73. #else
  74. Debug.Log("This platform does not support!");
  75. return false;
  76. #endif
  77. }
  78. /**
  79. * set debugmode for plugin
  80. *
  81. */
  82. [Obsolete("This interface is obsolete!",false)]
  83. public void setDebugMode(bool bDebug)
  84. {
  85. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  86. AnySDKIAP_nativeSetDebugMode (bDebug);
  87. #else
  88. Debug.Log("This platform does not support!");
  89. #endif
  90. }
  91. /**
  92. @brief set pListener The callback object for IAP result
  93. @param the MonoBehaviour object
  94. @param the callback of function
  95. */
  96. public void setListener(MonoBehaviour gameObject,string functionName)
  97. {
  98. #if !UNITY_EDITOR && UNITY_ANDROID
  99. AnySDKUtil.registerActionCallback (AnySDKType.IAP, gameObject, functionName);
  100. #elif !UNITY_EDITOR && UNITY_IOS
  101. string gameObjectName = gameObject.gameObject.name;
  102. AnySDKIAP_nativeSetListener(gameObjectName,functionName);
  103. #else
  104. Debug.Log("This platform does not support!");
  105. #endif
  106. }
  107. /**
  108. @brief get plugin ids
  109. @return List<string> the plugin ids
  110. */
  111. public List<string> getPluginId()
  112. {
  113. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  114. StringBuilder value = new StringBuilder ();
  115. value.Capacity = AnySDKUtil.MAX_CAPACITY_NUM;
  116. AnySDKIAP_nativeGetPluginId (value);
  117. List<string> list = AnySDKUtil.StringToList (value.ToString());
  118. return list;
  119. #else
  120. Debug.Log("This platform does not support!");
  121. return null;
  122. #endif
  123. }
  124. /**
  125. @brief change the state of paying
  126. @param the state
  127. */
  128. public void resetPayState()
  129. {
  130. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  131. AnySDKIAP_nativeResetPayState ();
  132. #else
  133. Debug.Log("This platform does not support!");
  134. #endif
  135. }
  136. /**
  137. * Get Plugin version
  138. * @param pluginid
  139. * @return string
  140. */
  141. public string getPluginVersion(string pluginId = "")
  142. {
  143. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  144. StringBuilder version = new StringBuilder();
  145. version.Capacity = AnySDKUtil.MAX_CAPACITY_NUM;
  146. AnySDKIAP_nativeGetPluginVersion (version,pluginId);
  147. return version.ToString();
  148. #else
  149. Debug.Log("This platform does not support!");
  150. return "";
  151. #endif
  152. }
  153. /**
  154. * Get SDK version
  155. *@param pluginid
  156. * @return string
  157. */
  158. public string getSDKVersion(string pluginId = "")
  159. {
  160. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  161. StringBuilder version = new StringBuilder();
  162. version.Capacity = AnySDKUtil.MAX_CAPACITY_NUM;
  163. AnySDKIAP_nativeGetSDKVersion (version,pluginId);
  164. return version.ToString();
  165. #else
  166. Debug.Log("This platform does not support!");
  167. return "";
  168. #endif
  169. }
  170. /**
  171. *@brief methods for reflections
  172. *@param function name
  173. *@param AnySDKParam param
  174. *@param pluginid
  175. *@return void
  176. */
  177. public void callFuncWithParam(string functionName, AnySDKParam param,string pluginId = "")
  178. {
  179. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  180. List<AnySDKParam> list = new List<AnySDKParam> ();
  181. list.Add (param);
  182. AnySDKIAP_nativeCallFuncWithParam(functionName, list.ToArray(),list.Count,pluginId);
  183. #else
  184. Debug.Log("This platform does not support!");
  185. #endif
  186. }
  187. /**
  188. *@brief methods for reflections
  189. *@param function name
  190. *@param List<AnySDKParam> param
  191. *@param pluginid
  192. *@return void
  193. */
  194. public void callFuncWithParam(string functionName, List<AnySDKParam> param = null,string pluginId = "")
  195. {
  196. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  197. if (param == null)
  198. {
  199. AnySDKIAP_nativeCallFuncWithParam (functionName, null, 0,pluginId);
  200. } else {
  201. AnySDKIAP_nativeCallFuncWithParam (functionName, param.ToArray (), param.Count,pluginId);
  202. }
  203. #else
  204. Debug.Log("This platform does not support!");
  205. #endif
  206. }
  207. /**
  208. *@brief methods for reflections
  209. *@param function name
  210. *@param AnySDKParam param
  211. *@param pluginid
  212. *@return int
  213. */
  214. public int callIntFuncWithParam(string functionName, AnySDKParam param,string pluginId = "")
  215. {
  216. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  217. List<AnySDKParam> list = new List<AnySDKParam> ();
  218. list.Add (param);
  219. return AnySDKIAP_nativeCallIntFuncWithParam(functionName, list.ToArray(),list.Count,pluginId);
  220. #else
  221. Debug.Log("This platform does not support!");
  222. return -1;
  223. #endif
  224. }
  225. /**
  226. *@brief methods for reflections
  227. *@param function name
  228. *@param List<AnySDKParam> param
  229. *@param pluginid
  230. *@return int
  231. */
  232. public int callIntFuncWithParam(string functionName, List<AnySDKParam> param = null,string pluginId = "")
  233. {
  234. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  235. if (param == null)
  236. {
  237. return AnySDKIAP_nativeCallIntFuncWithParam (functionName, null, 0,pluginId);
  238. } else {
  239. return AnySDKIAP_nativeCallIntFuncWithParam (functionName, param.ToArray (), param.Count,pluginId);
  240. }
  241. #else
  242. Debug.Log("This platform does not support!");
  243. return -1;
  244. #endif
  245. }
  246. /**
  247. *@brief methods for reflections
  248. *@param function name
  249. *@param AnySDKParam param
  250. *@param pluginid
  251. *@return float
  252. */
  253. public float callFloatFuncWithParam(string functionName, AnySDKParam param,string pluginId = "")
  254. {
  255. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  256. List<AnySDKParam> list = new List<AnySDKParam> ();
  257. list.Add (param);
  258. return AnySDKIAP_nativeCallFloatFuncWithParam(functionName, list.ToArray(),list.Count,pluginId);
  259. #else
  260. Debug.Log("This platform does not support!");
  261. return 0;
  262. #endif
  263. }
  264. /**
  265. *@brief methods for reflections
  266. *@param function name
  267. *@param List<AnySDKParam> param
  268. *@param pluginid
  269. *@return float
  270. */
  271. public float callFloatFuncWithParam(string functionName, List<AnySDKParam> param = null,string pluginId = "")
  272. {
  273. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  274. if (param == null)
  275. {
  276. return AnySDKIAP_nativeCallFloatFuncWithParam (functionName, null, 0,pluginId);
  277. } else {
  278. return AnySDKIAP_nativeCallFloatFuncWithParam (functionName, param.ToArray (), param.Count,pluginId);
  279. }
  280. #else
  281. Debug.Log("This platform does not support!");
  282. return 0;
  283. #endif
  284. }
  285. /**
  286. *@brief methods for reflections
  287. *@param function name
  288. *@param AnySDKParam param
  289. *@param pluginid
  290. *@return bool
  291. */
  292. public bool callBoolFuncWithParam(string functionName, AnySDKParam param,string pluginId = "")
  293. {
  294. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  295. List<AnySDKParam> list = new List<AnySDKParam> ();
  296. list.Add (param);
  297. return AnySDKIAP_nativeCallBoolFuncWithParam(functionName, list.ToArray(),list.Count,pluginId);
  298. #else
  299. Debug.Log("This platform does not support!");
  300. return false;
  301. #endif
  302. }
  303. /**
  304. *@brief methods for reflections
  305. *@param function name
  306. *@param List<AnySDKParam> param
  307. *@param pluginid
  308. *@return bool
  309. */
  310. public bool callBoolFuncWithParam(string functionName, List<AnySDKParam> param = null,string pluginId = "")
  311. {
  312. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  313. if (param == null)
  314. {
  315. return AnySDKIAP_nativeCallBoolFuncWithParam (functionName, null, 0,pluginId);
  316. } else {
  317. return AnySDKIAP_nativeCallBoolFuncWithParam (functionName, param.ToArray (), param.Count,pluginId);
  318. }
  319. #else
  320. Debug.Log("This platform does not support!");
  321. return false;
  322. #endif
  323. }
  324. /**
  325. *@brief methods for reflections
  326. *@param function name
  327. *@param AnySDKParam param
  328. *@param pluginid
  329. *@return string
  330. */
  331. public string callStringFuncWithParam(string functionName, AnySDKParam param,string pluginId = "")
  332. {
  333. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  334. List<AnySDKParam> list = new List<AnySDKParam> ();
  335. list.Add (param);
  336. StringBuilder value = new StringBuilder();
  337. value.Capacity = AnySDKUtil.MAX_CAPACITY_NUM;
  338. AnySDKIAP_nativeCallStringFuncWithParam(functionName, list.ToArray(),list.Count,value,pluginId);
  339. return value.ToString ();
  340. #else
  341. Debug.Log("This platform does not support!");
  342. return "";
  343. #endif
  344. }
  345. /**
  346. *@brief methods for reflections
  347. *@param function name
  348. *@param List<AnySDKParam> param
  349. *@param pluginid
  350. *@return string
  351. */
  352. public string callStringFuncWithParam(string functionName, List<AnySDKParam> param = null,string pluginId = "")
  353. {
  354. #if !UNITY_EDITOR &&( UNITY_ANDROID || UNITY_IOS)
  355. StringBuilder value = new StringBuilder();
  356. value.Capacity = AnySDKUtil.MAX_CAPACITY_NUM;
  357. if (param == null)
  358. {
  359. AnySDKIAP_nativeCallStringFuncWithParam (functionName, null, 0,value,pluginId);
  360. } else {
  361. AnySDKIAP_nativeCallStringFuncWithParam (functionName, param.ToArray (), param.Count,value,pluginId);
  362. }
  363. return value.ToString ();
  364. #else
  365. Debug.Log("This platform does not support!");
  366. return "";
  367. #endif
  368. }
  369. [DllImport(AnySDKUtil.ANYSDK_PLATFORM,CallingConvention=CallingConvention.Cdecl)]
  370. private static extern void AnySDKIAP_RegisterExternalCallDelegate(IntPtr functionPointer);
  371. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  372. private static extern void AnySDKIAP_nativeSetListener(string gameName, string functionName);
  373. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  374. private static extern void AnySDKIAP_nativePayForProduct(string info,string pluginId);
  375. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  376. private static extern void AnySDKIAP_nativeGetOrderId(StringBuilder orderId,string pluginId);
  377. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  378. private static extern void AnySDKIAP_nativeResetPayState();
  379. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  380. private static extern bool AnySDKIAP_nativeIsFunctionSupported(string functionName,string pluginId);
  381. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  382. private static extern void AnySDKIAP_nativeSetDebugMode(bool bDebug);
  383. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  384. private static extern void AnySDKIAP_nativeGetPluginId(StringBuilder pluginID);
  385. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  386. private static extern void AnySDKIAP_nativeGetPluginVersion(StringBuilder version,string pluginId);
  387. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  388. private static extern void AnySDKIAP_nativeGetSDKVersion(StringBuilder version,string pluginId);
  389. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  390. private static extern void AnySDKIAP_nativeCallFuncWithParam(string functionName, AnySDKParam[] param,int count,string pluginId);
  391. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  392. private static extern int AnySDKIAP_nativeCallIntFuncWithParam(string functionName, AnySDKParam[] param,int count,string pluginId);
  393. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  394. private static extern float AnySDKIAP_nativeCallFloatFuncWithParam(string functionName, AnySDKParam[] param,int count,string pluginId);
  395. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  396. private static extern bool AnySDKIAP_nativeCallBoolFuncWithParam(string functionName, AnySDKParam[] param,int count,string pluginId);
  397. [DllImport(AnySDKUtil.ANYSDK_PLATFORM)]
  398. private static extern void AnySDKIAP_nativeCallStringFuncWithParam(string functionName, AnySDKParam[] param,int count,StringBuilder value,string pluginId);
  399. }
  400. }