ATNativeAdClient.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using AnyThinkAds.Common;
  6. using AnyThinkAds.Api;
  7. namespace AnyThinkAds.Android
  8. {
  9. public class ATNativeAdClient : AndroidJavaProxy, IATNativeAdClient
  10. {
  11. public event EventHandler<ATAdEventArgs> onAdLoadEvent;
  12. public event EventHandler<ATAdErrorEventArgs> onAdLoadFailureEvent;
  13. public event EventHandler<ATAdEventArgs> onAdImpressEvent;
  14. public event EventHandler<ATAdEventArgs> onAdClickEvent;
  15. public event EventHandler<ATAdEventArgs> onAdVideoStartEvent;
  16. public event EventHandler<ATAdEventArgs> onAdVideoEndEvent;
  17. public event EventHandler<ATAdProgressEventArgs> onAdVideoProgressEvent;
  18. public event EventHandler<ATAdEventArgs> onAdCloseEvent;
  19. public event EventHandler<ATAdEventArgs> onAdSourceAttemptEvent;
  20. public event EventHandler<ATAdEventArgs> onAdSourceFilledEvent;
  21. public event EventHandler<ATAdErrorEventArgs> onAdSourceLoadFailureEvent;
  22. public event EventHandler<ATAdEventArgs> onAdSourceBiddingAttemptEvent;
  23. public event EventHandler<ATAdEventArgs> onAdSourceBiddingFilledEvent;
  24. public event EventHandler<ATAdErrorEventArgs> onAdSourceBiddingFailureEvent;
  25. private Dictionary<string, AndroidJavaObject> nativeAdHelperMap = new Dictionary<string, AndroidJavaObject>();
  26. private ATNativeAdListener mlistener;
  27. public ATNativeAdClient(): base("com.anythink.unitybridge.nativead.NativeListener")
  28. {
  29. }
  30. public void loadNativeAd(string placementId, string mapJson)
  31. {
  32. Debug.Log ("loadNativeAd....jsonmap:"+mapJson);
  33. if(!nativeAdHelperMap.ContainsKey(placementId)){
  34. AndroidJavaObject nativeHelper = new AndroidJavaObject(
  35. "com.anythink.unitybridge.nativead.NativeHelper", this);
  36. nativeHelper.Call("initNative", placementId);
  37. nativeAdHelperMap.Add(placementId, nativeHelper);
  38. }
  39. try{
  40. if (nativeAdHelperMap.ContainsKey(placementId)) {
  41. nativeAdHelperMap[placementId].Call ("loadNative",mapJson);
  42. }
  43. }catch(System.Exception e){
  44. System.Console.WriteLine("Exception caught: {0}", e);
  45. Debug.Log ("ATNativeAdClient : error."+e.Message);
  46. }
  47. }
  48. public bool hasAdReady(string placementId)
  49. {
  50. bool isready = false;
  51. Debug.Log ("hasAdReady....");
  52. try{
  53. if (nativeAdHelperMap.ContainsKey(placementId)) {
  54. isready = nativeAdHelperMap[placementId].Call<bool> ("isAdReady");
  55. }
  56. }catch(System.Exception e){
  57. System.Console.WriteLine("Exception caught: {0}", e);
  58. Debug.Log ("ATNativeAdClient : error."+e.Message);
  59. }
  60. return isready;
  61. }
  62. public void entryScenarioWithPlacementID(string placementId, string scenarioID){
  63. Debug.Log("ATNativeAdClient : entryScenarioWithPlacementID....");
  64. try
  65. {
  66. if (nativeAdHelperMap.ContainsKey(placementId))
  67. {
  68. nativeAdHelperMap[placementId].Call("entryAdScenario", scenarioID);
  69. }
  70. }
  71. catch (System.Exception e)
  72. {
  73. System.Console.WriteLine("Exception caught: {0}", e);
  74. Debug.Log("ATNativeAdClient entryScenarioWithPlacementID: error." + e.Message);
  75. }
  76. }
  77. public string checkAdStatus(string placementId)
  78. {
  79. string adStatusJsonString = "";
  80. Debug.Log("ATNativeAdClient : checkAdStatus....");
  81. try
  82. {
  83. if (nativeAdHelperMap.ContainsKey(placementId))
  84. {
  85. adStatusJsonString = nativeAdHelperMap[placementId].Call<string>("checkAdStatus");
  86. }
  87. }
  88. catch (System.Exception e)
  89. {
  90. System.Console.WriteLine("Exception caught: {0}", e);
  91. Debug.Log("ATNativeAdClient : error." + e.Message);
  92. }
  93. return adStatusJsonString;
  94. }
  95. public string getValidAdCaches(string placementId)
  96. {
  97. string validAdCachesString = "";
  98. Debug.Log("ATNativeAdClient : getValidAdCaches....");
  99. try
  100. {
  101. if (nativeAdHelperMap.ContainsKey(placementId))
  102. {
  103. validAdCachesString = nativeAdHelperMap[placementId].Call<string>("getValidAdCaches");
  104. }
  105. }
  106. catch (System.Exception e)
  107. {
  108. System.Console.WriteLine("Exception caught: {0}", e);
  109. Debug.Log("ATNativeAdClient : error." + e.Message);
  110. }
  111. return validAdCachesString;
  112. }
  113. public void setListener(ATNativeAdListener listener)
  114. {
  115. mlistener = listener;
  116. }
  117. public void renderAdToScene(string placementId, ATNativeAdView anyThinkNativeAdView, string mapJson)
  118. {
  119. string showconfig = anyThinkNativeAdView.toJSON ();
  120. //暂未实现 show
  121. Debug.Log ("renderAdToScene....showconfig >>>:"+showconfig);
  122. try{
  123. if (nativeAdHelperMap.ContainsKey(placementId)) {
  124. nativeAdHelperMap[placementId].Call ("show",showconfig, mapJson);
  125. }
  126. }catch(System.Exception e){
  127. Debug.Log ("ATNativeAdClient : error."+e.Message);
  128. System.Console.WriteLine("Exception caught: {0}", e);
  129. }
  130. }
  131. public void cleanAdView(string placementId, ATNativeAdView anyThinkNativeAdView)
  132. {
  133. //
  134. Debug.Log ("cleanAdView.... ");
  135. try{
  136. if (nativeAdHelperMap.ContainsKey(placementId)) {
  137. nativeAdHelperMap[placementId].Call ("cleanView");
  138. }
  139. }catch(System.Exception e){
  140. System.Console.WriteLine("Exception caught: {0}", e);
  141. Debug.Log ("ATNativeAdClient : error."+e.Message);
  142. }
  143. }
  144. public void onApplicationForces(string placementId, ATNativeAdView anyThinkNativeAdView)
  145. {
  146. Debug.Log ("onApplicationForces.... ");
  147. try{
  148. if (nativeAdHelperMap.ContainsKey(placementId)) {
  149. nativeAdHelperMap[placementId].Call ("onResume");
  150. }
  151. }catch(System.Exception e){
  152. System.Console.WriteLine("Exception caught: {0}", e);
  153. Debug.Log ("ATNativeAdClient : error."+e.Message);
  154. }
  155. }
  156. public void onApplicationPasue(string placementId, ATNativeAdView anyThinkNativeAdView)
  157. {
  158. Debug.Log ("onApplicationPasue.... ");
  159. try{
  160. if (nativeAdHelperMap.ContainsKey(placementId)) {
  161. nativeAdHelperMap[placementId].Call ("onPause");
  162. }
  163. }catch(System.Exception e){
  164. System.Console.WriteLine("Exception caught: {0}", e);
  165. Debug.Log ("ATNativeAdClient : error."+e.Message);
  166. }
  167. }
  168. public void cleanCache(string placementId)
  169. {
  170. Debug.Log ("cleanCache....");
  171. try{
  172. if (nativeAdHelperMap.ContainsKey(placementId)) {
  173. nativeAdHelperMap[placementId].Call ("clean");
  174. }
  175. }catch(System.Exception e){
  176. System.Console.WriteLine("Exception caught: {0}", e);
  177. Debug.Log ("ATNativeAdClient : error."+e.Message);
  178. }
  179. }
  180. /**
  181. * 广告展示回调
  182. *
  183. * @param view
  184. */
  185. public void onAdImpressed(string placementId, string callbackJson)
  186. {
  187. Debug.Log("onAdImpressed...unity3d.");
  188. onAdImpressEvent?.Invoke(this, new ATAdEventArgs(placementId,callbackJson));
  189. }
  190. /**
  191. * 广告点击回调
  192. *
  193. * @param view
  194. */
  195. public void onAdClicked(string placementId, string callbackJson)
  196. {
  197. Debug.Log("onAdClicked...unity3d.");
  198. onAdClickEvent?.Invoke(this, new ATAdEventArgs(placementId,callbackJson));
  199. }
  200. /**
  201. * 广告视频开始回调
  202. *
  203. * @param view
  204. */
  205. public void onAdVideoStart(string placementId)
  206. {
  207. Debug.Log("onAdVideoStart...unity3d.");
  208. onAdVideoStartEvent?.Invoke(this, new ATAdEventArgs(placementId));
  209. }
  210. /**
  211. * 广告视频结束回调
  212. *
  213. * @param view
  214. */
  215. public void onAdVideoEnd(string placementId)
  216. {
  217. Debug.Log("onAdVideoEnd...unity3d.");
  218. onAdVideoEndEvent?.Invoke(this, new ATAdEventArgs(placementId,""));
  219. }
  220. /**
  221. * 广告视频进度回调
  222. *
  223. * @param view
  224. */
  225. public void onAdVideoProgress(string placementId,int progress)
  226. {
  227. Debug.Log("onAdVideoProgress...progress[" + progress + "]");
  228. onAdVideoProgressEvent?.Invoke(this, new ATAdProgressEventArgs(placementId,"",progress));
  229. }
  230. /**
  231. * 广告视频进度回调
  232. *
  233. * @param view
  234. */
  235. public void onAdCloseButtonClicked(string placementId, string callbackJson)
  236. {
  237. Debug.Log("onAdCloseButtonClicked...unity3d");
  238. onAdCloseEvent?.Invoke(this, new ATAdEventArgs(placementId,callbackJson));
  239. }
  240. /**
  241. * 广告加载成功
  242. */
  243. public void onNativeAdLoaded(string placementId)
  244. {
  245. Debug.Log("onNativeAdLoaded...unity3d.");
  246. onAdLoadEvent?.Invoke(this, new ATAdEventArgs(placementId,""));
  247. }
  248. /**
  249. * 广告加载失败
  250. */
  251. public void onNativeAdLoadFail(string placementId,string code, string msg)
  252. {
  253. Debug.Log("onNativeAdLoadFail...unity3d. code:" + code + " msg:" + msg);
  254. onAdLoadFailureEvent?.Invoke(this, new ATAdErrorEventArgs(placementId,code,msg));
  255. }
  256. // Adsource Listener
  257. public void onAdSourceBiddingAttempt(string placementId, string callbackJson)
  258. {
  259. Debug.Log("onAdSourceBiddingAttempt...unity3d." + placementId + "," + callbackJson);
  260. onAdSourceBiddingAttemptEvent?.Invoke(this, new ATAdEventArgs(placementId,callbackJson));
  261. }
  262. public void onAdSourceBiddingFilled(string placementId, string callbackJson)
  263. {
  264. Debug.Log("onAdSourceBiddingFilled...unity3d." + placementId + "," + callbackJson);
  265. onAdSourceBiddingFilledEvent?.Invoke(this, new ATAdEventArgs(placementId,callbackJson));
  266. }
  267. public void onAdSourceBiddingFail(string placementId, string callbackJson, string code, string error)
  268. {
  269. Debug.Log("onAdSourceBiddingFail...unity3d." + placementId + "," + code + "," + error + "," + callbackJson);
  270. onAdSourceBiddingFailureEvent?.Invoke(this, new ATAdErrorEventArgs(placementId,callbackJson,code,error));
  271. }
  272. public void onAdSourceAttempt(string placementId, string callbackJson)
  273. {
  274. Debug.Log("onAdSourceAttempt...unity3d." + placementId + "," + callbackJson);
  275. onAdSourceAttemptEvent?.Invoke(this, new ATAdEventArgs(placementId,callbackJson));
  276. }
  277. public void onAdSourceLoadFilled(string placementId, string callbackJson)
  278. {
  279. Debug.Log("onAdSourceLoadFilled...unity3d." + placementId + "," + callbackJson);
  280. onAdSourceFilledEvent?.Invoke(this, new ATAdEventArgs(placementId,callbackJson));
  281. }
  282. public void onAdSourceLoadFail(string placementId, string callbackJson, string code, string error)
  283. {
  284. Debug.Log("onAdSourceLoadFail...unity3d." + placementId + "," + code + "," + error + "," + callbackJson);
  285. onAdSourceLoadFailureEvent?.Invoke(this, new ATAdErrorEventArgs(placementId,callbackJson,code,error));
  286. }
  287. }
  288. }