ATNativeBannerAdWrapper.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using AOT;
  6. using AnyThinkAds.ThirdParty.LitJson;
  7. using AnyThinkAds.iOS;
  8. using AnyThinkAds.Api;
  9. #pragma warning disable 0109
  10. public class ATNativeBannerAdWrapper : ATAdWrapper {
  11. static private Dictionary<string, ATNativeBannerAdClient> clients;
  12. static private string CMessageReceiverClass = "ATNativeBannerAdWrapper";
  13. static public new void InvokeCallback(JsonData jsonData) {
  14. Debug.Log("Unity: ATNativeBannerAdWrapper::InvokeCallback()");
  15. string extraJson = "";
  16. string callback = (string)jsonData["callback"];
  17. Dictionary<string, object> msgDict = JsonMapper.ToObject<Dictionary<string, object>>(jsonData["msg"].ToJson());
  18. JsonData msgJsonData = jsonData["msg"];
  19. IDictionary idic = (System.Collections.IDictionary)msgJsonData;
  20. if (idic.Contains("extra")) {
  21. JsonData extraJsonDate = msgJsonData["extra"];
  22. if (extraJsonDate != null) {
  23. extraJson = msgJsonData["extra"].ToJson();
  24. }
  25. }
  26. if (callback.Equals("OnNativeBannerAdLoaded")) {
  27. OnNativeBannerAdLoaded((string)msgDict["placement_id"]);
  28. } else if (callback.Equals("OnNativeBannerAdLoadingFailure")) {
  29. Dictionary<string, object> errorDict = new Dictionary<string, object>();
  30. Dictionary<string, object> errorMsg = JsonMapper.ToObject<Dictionary<string, object>>(msgJsonData["error"].ToJson());
  31. if (errorMsg.ContainsKey("code")) { errorDict.Add("code", errorMsg["code"]); }
  32. if (errorMsg.ContainsKey("reason")) { errorDict.Add("message", errorMsg["reason"]); }
  33. OnNativeBannerAdLoadingFailure((string)msgDict["placement_id"], errorDict);
  34. } else if (callback.Equals("OnNaitveBannerAdShow")) {
  35. OnNaitveBannerAdShow((string)msgDict["placement_id"], extraJson);
  36. } else if (callback.Equals("OnNativeBannerAdClick")) {
  37. OnNativeBannerAdClick((string)msgDict["placement_id"], extraJson);
  38. } else if (callback.Equals("OnNativeBannerAdAutorefresh")) {
  39. OnNativeBannerAdAutorefresh((string)msgDict["placement_id"], extraJson);
  40. } else if (callback.Equals("OnNativeBannerAdAutorefreshFailed")) {
  41. Dictionary<string, object> errorDict = new Dictionary<string, object>();
  42. Dictionary<string, object> errorMsg = JsonMapper.ToObject<Dictionary<string, object>>(msgJsonData["error"].ToJson());
  43. if (errorMsg.ContainsKey("code")) { errorDict.Add("code", errorMsg["code"]); }
  44. if (errorMsg.ContainsKey("reason")) { errorDict.Add("message", errorMsg["reason"]); }
  45. OnNativeBannerAdAutorefreshFailed((string)msgDict["placement_id"], errorDict);
  46. } else if (callback.Equals("OnNativeBannerAdCloseButtonClicked")) {
  47. OnNativeBannerAdCloseButtonClicked((string)msgDict["placement_id"]);
  48. } else if (callback.Equals("PauseAudio")) {
  49. Debug.Log("c# : callback, PauseAudio");
  50. PauseAudio();
  51. } else if (callback.Equals("ResumeAudio")) {
  52. Debug.Log("c# : callback, ResumeAudio");
  53. ResumeAudio();
  54. }
  55. }
  56. static public void PauseAudio() {
  57. Debug.Log("ATNativeBannerAdWrapper::PauseAudio()");
  58. }
  59. static public void ResumeAudio() {
  60. Debug.Log("ATNativeBannerAdWrapper::ResumeAudio()");
  61. }
  62. static public void setClientForPlacementID(string placementID, ATNativeBannerAdClient client) {
  63. Debug.Log("ATNativeBannerAdWrapper::setClientForPlacementID()");
  64. if (clients == null) clients = new Dictionary<string, ATNativeBannerAdClient>();
  65. if (clients.ContainsKey(placementID)) clients.Remove(placementID);
  66. clients.Add(placementID, client);
  67. }
  68. static public void loadAd(string placementID, string customData) {
  69. Debug.Log("ATNativeBannerAdWrapper::loadAd()");
  70. ATUnityCBridge.SendMessageToC(CMessageReceiverClass, "loadNativeBannerAdWithPlacementID:customDataJSONString:callback:", new object[]{placementID, customData != null ? customData : ""}, true);
  71. }
  72. static public bool adReady(string placementID) {
  73. Debug.Log("ATNativeBannerAdWrapper::adReady()");
  74. return ATUnityCBridge.SendMessageToC(CMessageReceiverClass, "isNativeBannerAdReadyForPlacementID:", new object[]{placementID});
  75. }
  76. static public void showAd(string placementID, ATRect rect, Dictionary<string, string> pairs) {
  77. Debug.Log("ATNativeBannerAdWrapper::showAd()");
  78. Dictionary<string, object> rectDict = new Dictionary<string, object>{ {"x", rect.x}, {"y", rect.y}, {"width", rect.width}, {"height", rect.height} };
  79. ATUnityCBridge.SendMessageToC(CMessageReceiverClass, "showNativeBannerAdWithPlacementID:rect:extra:", new object[]{placementID, JsonMapper.ToJson(rectDict), JsonMapper.ToJson(pairs != null ? pairs : new Dictionary<string, string>())}, false);
  80. }
  81. static public void removeAd(string placementID) {
  82. Debug.Log("ATNativeBannerAdWrapper::removeAd()");
  83. ATUnityCBridge.SendMessageToC(CMessageReceiverClass, "removeNativeBannerAdWithPlacementID:", new object[]{placementID}, false);
  84. }
  85. //Callbacks
  86. static private void OnNativeBannerAdLoaded(string placementID) {
  87. Debug.Log("Unity: ATNativeBannerAdWrapper::OnNativeBannerAdLoaded(" + placementID + ")");
  88. if (clients[placementID] != null) clients[placementID].onAdLoaded(placementID);
  89. }
  90. static private void OnNativeBannerAdLoadingFailure(string placementID, Dictionary<string, object> errorDict) {
  91. Debug.Log("Unity: ATNativeBannerAdWrapper::OnNativeBannerAdLoadingFailure(" + placementID + ")");
  92. if (clients[placementID] != null) clients[placementID].onAdLoadFail(placementID, (string)errorDict["code"], (string)errorDict["message"]);
  93. }
  94. static private void OnNaitveBannerAdShow(string placementID, string callbackJson) {
  95. Debug.Log("Unity: ATNativeBannerAdWrapper::OnNaitveBannerAdShow(" + placementID + ")");
  96. if (clients[placementID] != null) clients[placementID].onAdImpressed(placementID, callbackJson);
  97. }
  98. static private void OnNativeBannerAdClick(string placementID, string callbackJson) {
  99. Debug.Log("Unity: ATNativeBannerAdWrapper::OnNativeBannerAdClick(" + placementID + ")");
  100. if (clients[placementID] != null) clients[placementID].onAdClicked(placementID, callbackJson);
  101. }
  102. static private void OnNativeBannerAdAutorefresh(string placementID, string callbackJson) {
  103. Debug.Log("Unity: ATNativeBannerAdWrapper::OnNativeBannerAdAutorefresh(" + placementID + ")");
  104. if (clients[placementID] != null) clients[placementID].onAdAutoRefresh(placementID, callbackJson);
  105. }
  106. static private void OnNativeBannerAdAutorefreshFailed(string placementID, Dictionary<string, object> errorDict) {
  107. Debug.Log("Unity: ATNativeBannerAdWrapper::OnNativeBannerAdAutorefreshFailed(" + placementID + ")");
  108. if (clients[placementID] != null) clients[placementID].onAdAutoRefreshFailure(placementID, (string)errorDict["code"], (string)errorDict["message"]);
  109. }
  110. static private void OnNativeBannerAdCloseButtonClicked(string placementID) {
  111. Debug.Log("Unity: ATNativeBannerAdWrapper::OnNativeBannerAdCloseButtonClicked(" + placementID + ")");
  112. if (clients[placementID] != null) clients[placementID].onAdCloseButtonClicked(placementID);
  113. }
  114. }