Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.64 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AndroidGoogleAdsExample : MonoBehaviour {
  5.  
  6. public static AndroidGoogleAdsExample instance;
  7.  
  8. void Awake()
  9. {
  10. if(instance == null) {
  11. DontDestroyOnLoad(gameObject);
  12. instance = this;
  13. }
  14. else if(instance != this) {
  15. Destroy (gameObject);
  16. }
  17.  
  18. }
  19.  
  20. //replace with your ids
  21. private const string MY_BANNERS_AD_UNIT_ID = "ca-app-pub-6101605888755494/1824764765";
  22.  
  23. private const string MY_INTERSTISIALS_AD_UNIT_ID = "ca-app-pub-6101605888755494/3301497967";
  24.  
  25. private const string MY_REWARDED_VIDEO_AD_UNIT_ID = "";//"ca-app-pub-6101605888755494/5378283960";
  26.  
  27. private GoogleMobileAdBanner banner1;
  28. private GoogleMobileAdBanner banner2;
  29.  
  30. private bool IsInterstisialsAdReady = false;
  31.  
  32. // public DefaultPreviewButton ShowIntersButton;
  33. //
  34. // public DefaultPreviewButton[] b1CreateButtons;
  35. // public DefaultPreviewButton b1Hide;
  36. // public DefaultPreviewButton b1Show;
  37. // public DefaultPreviewButton b1Refresh;
  38. // public DefaultPreviewButton ChangePost1;
  39. // public DefaultPreviewButton ChangePost2;
  40. // public DefaultPreviewButton b1Destroy;
  41. //
  42. //
  43. // public DefaultPreviewButton[] b2CreateButtons;
  44. // public DefaultPreviewButton b2Hide;
  45. // public DefaultPreviewButton b2Show;
  46. // public DefaultPreviewButton b2Refresh;
  47. // public DefaultPreviewButton b2Destroy;
  48.  
  49.  
  50.  
  51. //--------------------------------------
  52. // INITIALIZE
  53. //--------------------------------------
  54.  
  55.  
  56.  
  57. void Start() {
  58.  
  59. AndroidAdMob.Client.Init(MY_BANNERS_AD_UNIT_ID);
  60.  
  61. //If yoi whant to use Interstisial ad also, you need to set additional ad unin id for Interstisial as well
  62. AndroidAdMob.Client.SetInterstisialsUnitID(MY_INTERSTISIALS_AD_UNIT_ID);
  63. //If yoi whant to use Rewarded Video Ads also, you need to set additional ad unin id for Rewarded Video as well
  64. // AndroidAdMobController.Instance.SetRewardedVideoAdUnitID(MY_REWARDED_VIDEO_AD_UNIT_ID);
  65.  
  66. //Optional, add data for better ad targeting
  67. AndroidAdMob.Client.SetGender(GoogleGender.Male);
  68. // AndroidAdMob.Client.AddKeyword("game");
  69. // AndroidAdMob.Client.SetBirthday(1989, AndroidMonth.MARCH, 18);
  70. // AndroidAdMob.Client.TagForChildDirectedTreatment(false);
  71.  
  72. //Causes a device to receive test ads. The deviceId can be obtained by viewing the logcat output after creating a new ad
  73. //AndroidAdMobController.instance.AddTestDevice("6B9FA8031AEFDC4758B7D8987F77A5A6");
  74.  
  75. AndroidAdMob.Client.OnInterstitialLoaded += OnInterstisialsLoaded;
  76. AndroidAdMob.Client.OnInterstitialOpened += OnInterstisialsOpen;
  77. AndroidAdMob.Client.OnInterstitialClosed += OnInterstisialsClosed;
  78.  
  79. // AndroidAdMobController.Instance.OnRewardedVideoLoaded += HandleOnRewardedVideoLoaded;
  80. // AndroidAdMobController.Instance.OnRewardedVideoAdClosed += HandleOnRewardedVideoAdClosed;
  81.  
  82. //listening for InApp Event
  83. //You will only receive in-app purchase (IAP) ads if you specifically configure an IAP ad campaign in the AdMob front end.
  84. AndroidAdMob.Client.OnAdInAppRequest += OnInAppRequest;
  85.  
  86.  
  87. // Banner
  88. CreateBannerBottomCenter ();
  89. LoadInterstitialAd ();
  90.  
  91. }
  92.  
  93. void HandleOnRewardedVideoAdClosed () { }
  94.  
  95. void HandleOnRewardedVideoLoaded () { }
  96.  
  97. // Start InterstitialAd
  98. public void StartInterstitialAd() {
  99. AndroidAdMob.Client.StartInterstitialAd ();
  100. }
  101.  
  102. // Load Interstitial Ad
  103. public void LoadInterstitialAd() {
  104. AndroidAdMob.Client.LoadInterstitialAd ();
  105. }
  106. // Show Interstitial Ad
  107. public void ShowInterstitialAd() {
  108. AndroidAdMob.Client.ShowInterstitialAd ();
  109. }
  110.  
  111. private void LoadRewardedVideoAd () {
  112. AndroidAdMobController.Instance.LoadRewardedVideo();
  113. }
  114.  
  115. private void ShowRewardedVideoAd () {
  116. AndroidAdMobController.Instance.ShowRewardedVideo();
  117. }
  118.  
  119. private void CreateBannerCustomPos() {
  120. banner1 = AndroidAdMob.Client.CreateAdBanner(300, 100, GADBannerSize.BANNER);
  121. }
  122.  
  123. private void CreateBannerUpperLeft() {
  124. banner1 = AndroidAdMob.Client.CreateAdBanner(TextAnchor.UpperLeft, GADBannerSize.BANNER);
  125. }
  126.  
  127. private void CreateBannerUpperCneter() {
  128. banner1 = AndroidAdMob.Client.CreateAdBanner(TextAnchor.UpperCenter, GADBannerSize.BANNER);
  129. }
  130. // Create Banner Bottom Left
  131. public void CreateBannerBottomLeft() {
  132. banner1 = AndroidAdMob.Client.CreateAdBanner(TextAnchor.LowerLeft, GADBannerSize.BANNER);
  133. }
  134.  
  135. private void CreateBannerBottomCenter() {
  136. banner1 = AndroidAdMob.Client.CreateAdBanner(TextAnchor.LowerCenter, GADBannerSize.BANNER);
  137. }
  138.  
  139. private void CreateBannerBottomRight() {
  140. banner1 = AndroidAdMobController.Instance.CreateAdBanner(TextAnchor.LowerRight, GADBannerSize.BANNER);
  141. }
  142.  
  143. private void B1Hide() {
  144. banner1.Hide();
  145. }
  146.  
  147.  
  148. private void B1Show() {
  149. banner1.Show();
  150. }
  151.  
  152. private void B1Refresh() {
  153. banner1.Refresh();
  154. }
  155.  
  156. private void B1Destrouy() {
  157. AndroidAdMob.Client.DestroyBanner(banner1.id);
  158. banner1 = null;
  159. }
  160.  
  161.  
  162. private void SmartTOP() {
  163. banner2 = AndroidAdMob.Client.CreateAdBanner(TextAnchor.UpperCenter, GADBannerSize.SMART_BANNER);
  164. }
  165.  
  166. private void SmartBottom() {
  167. banner2 = AndroidAdMob.Client.CreateAdBanner(TextAnchor.LowerCenter, GADBannerSize.SMART_BANNER);
  168. }
  169.  
  170.  
  171. private void B2Hide() {
  172. banner2.Hide();
  173. }
  174.  
  175.  
  176. private void B2Show() {
  177. banner2.Show();
  178. }
  179.  
  180. private void B2Refresh() {
  181. banner2.Refresh();
  182. }
  183.  
  184. private void B2Destrouy() {
  185. AndroidAdMob.Client.DestroyBanner(banner2.id);
  186. banner2 = null;
  187. }
  188.  
  189. private void ChnagePostToMiddle() {
  190. banner1.SetBannerPosition(TextAnchor.MiddleCenter);
  191. }
  192.  
  193. private void ChangePostRandom() {
  194. banner1.SetBannerPosition(Random.Range(0, Screen.width), Random.Range(0, Screen.height));
  195. }
  196.  
  197.  
  198.  
  199.  
  200. //--------------------------------------
  201. // PUBLIC METHODS
  202. //--------------------------------------
  203.  
  204.  
  205.  
  206. void FixedUpdate() {
  207. // if(IsInterstisialsAdReady) {
  208. // ShowIntersButton.EnabledButton();
  209. // } else {
  210. // ShowIntersButton.DisabledButton();
  211. // }
  212.  
  213. // if(banner1 != null) {
  214. // foreach(DefaultPreviewButton pb in b1CreateButtons) {
  215. // pb.DisabledButton();
  216. // }
  217. //
  218. // b1Destroy.EnabledButton();
  219. //
  220. // if(banner1.IsLoaded) {
  221. // b1Refresh.EnabledButton();
  222. // ChangePost1.EnabledButton();
  223. // ChangePost2.EnabledButton();
  224. // if(banner1.IsOnScreen) {
  225. // b1Hide.EnabledButton();
  226. // b1Show.DisabledButton();
  227. // } else {
  228. // b1Hide.DisabledButton();
  229. // b1Show.EnabledButton();
  230. // }
  231. // } else {
  232. // b1Refresh.DisabledButton();
  233. // ChangePost1.DisabledButton();
  234. // ChangePost2.DisabledButton();
  235. // b1Hide.DisabledButton();
  236. // b1Show.DisabledButton();
  237. // }
  238. //
  239. //
  240. //
  241. // } else {
  242. // foreach(DefaultPreviewButton pb in b1CreateButtons) {
  243. // pb.EnabledButton();
  244. // }
  245. //
  246. // b1Hide.DisabledButton();
  247. // b1Show.DisabledButton();
  248. // b1Refresh.DisabledButton();
  249. // b1Destroy.DisabledButton();
  250. // }
  251.  
  252.  
  253.  
  254.  
  255.  
  256. // if(banner2 != null) {
  257. // foreach(DefaultPreviewButton pb in b2CreateButtons) {
  258. // pb.DisabledButton();
  259. // }
  260. //
  261. // b2Destroy.EnabledButton();
  262. //
  263. // if(banner2.IsLoaded) {
  264. // b2Refresh.EnabledButton();
  265. // if(banner2.IsOnScreen) {
  266. // b2Hide.EnabledButton();
  267. // b2Show.DisabledButton();
  268. // } else {
  269. // b2Hide.DisabledButton();
  270. // b2Show.EnabledButton();
  271. // }
  272. // } else {
  273. // b2Refresh.DisabledButton();
  274. // b2Hide.DisabledButton();
  275. // b2Show.DisabledButton();
  276. // }
  277. //
  278. //
  279. //
  280. // } else {
  281. // foreach(DefaultPreviewButton pb in b2CreateButtons) {
  282. // pb.EnabledButton();
  283. // }
  284. //
  285. // b2Hide.DisabledButton();
  286. // b2Show.DisabledButton();
  287. // b2Refresh.DisabledButton();
  288. // b2Destroy.DisabledButton();
  289. // }
  290.  
  291.  
  292. }
  293.  
  294. //--------------------------------------
  295. // GET/SET
  296. //--------------------------------------
  297.  
  298. //--------------------------------------
  299. // EVENTS
  300. //--------------------------------------
  301.  
  302. private void OnInterstisialsLoaded() {
  303. IsInterstisialsAdReady = true;
  304. Debug.Log ("Ad Loaded");
  305. }
  306.  
  307. private void OnInterstisialsOpen() {
  308. IsInterstisialsAdReady = false;
  309. Debug.Log ("Ad Opened");
  310. }
  311.  
  312. private void OnInterstisialsClosed() {
  313. Debug.Log ("Ad Closed");
  314. LoadInterstitialAd ();
  315. }
  316.  
  317. private void OnInAppRequest(string productId) {
  318.  
  319. AN_PoupsProxy.showMessage ("In App Request", "In App Request for product Id: " + productId + " received");
  320.  
  321.  
  322. //Then you should perfrom purchase for this product id, using this or another game billing plugin
  323. //Once the purchase is complete, you should call RecordInAppResolution with one of the constants defined in GADInAppResolution:
  324.  
  325. AndroidAdMob.Client.RecordInAppResolution(GADInAppResolution.RESOLUTION_SUCCESS);
  326.  
  327. }
  328.  
  329.  
  330.  
  331. //--------------------------------------
  332. // PRIVATE METHODS
  333. //--------------------------------------
  334.  
  335. //--------------------------------------
  336. // DESTROY
  337. //--------------------------------------
  338.  
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement