Advertisement
Guest User

Untitled

a guest
May 27th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.53 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using GoogleMobileAds;
  5. using GoogleMobileAds.Api;
  6. using System;
  7. using UnityEngine.UI;
  8. using UnityEngine.Advertisements;
  9. using UnityEngine.Purchasing;
  10. using Tapdaq;
  11.  
  12. public class MyAdManager : MonoBehaviour {
  13.  
  14. public static MyAdManager instance;
  15.  
  16. public bool initAdmob;
  17. [SerializeField]
  18. bool testing;
  19. [SerializeField]
  20. string androidAppId;
  21. [SerializeField]
  22. string IosAppId;
  23. [SerializeField]
  24. string bannerAdId;
  25. [SerializeField]
  26. string iosBannerAdId;
  27. [SerializeField]
  28. string interstitialAdId;
  29. [SerializeField]
  30. string iosInterstitialAdId;
  31. [SerializeField]
  32. string rewardedSpin;
  33. [SerializeField]
  34. string iosRewardedSpin;
  35. [SerializeField]
  36. int rewardedAdRewardInt;
  37. [SerializeField]
  38. Animator doubleCoinAnim;
  39. public int tripleCoinRewardAmount;
  40. [SerializeField]
  41. RectTransform bannerSim;
  42.  
  43. private BannerView bannerView;
  44. private InterstitialAd interstitial;
  45. private RewardBasedVideoAd rewardBasedVideo;
  46.  
  47. private void Awake()
  48. {
  49. if (instance == null)
  50. {
  51. instance = this;
  52. }
  53. else
  54. {
  55. Debug.Log("There's Already an ad Manager, Destroy This one at " + gameObject.name);
  56. Destroy(this);
  57. }
  58.  
  59. #if UNITY_IOS
  60. //Advertisement.Initialize("2650279");
  61. #elif UNITY_ANDROID
  62. //if(initAdmob)
  63. //{
  64. //Advertisement.Initialize("2650277");
  65. //}
  66. #endif
  67. }
  68.  
  69. public void Start()
  70. {
  71. //InitAppID();
  72. //GameHandler.OnGameStart += ShowBanner;
  73. ////GameHandler.OnGameEnd += ShowInterstitial;
  74. //GameHandler.OnGameEnd += HideBanner;
  75.  
  76.  
  77. AdManager.Init();
  78. //Debug.Log("TapDaq Called");
  79. //AdManager.InitWithConsent (TDStatus.TRUE, TDStatus.FALSE, TDStatus.UNKNOWN);//IsUserSubjectToGDPR, UserConsent, isAgeRestrictedUser
  80. }
  81.  
  82.  
  83. private void InitAppID()
  84. {
  85. string appId = "ca-app-pub-2629468278215471~2139414196";
  86. #if UNITY_ANDROID
  87. if(!testing)
  88. {
  89. appId = androidAppId;
  90. }
  91. #elif UNITY_IPHONE
  92. if(!testing)
  93. {
  94. appId = androidAppId;
  95. }
  96. #else
  97. string appId = "unexpected_platform";
  98. #endif
  99.  
  100. // Initialize the Google Mobile Ads SDK.
  101. MobileAds.Initialize(appId);
  102.  
  103. //Req Banner
  104. this.RequestBanner();
  105. HideBanner();
  106. RequestInterstitial();
  107.  
  108. //Req Rewarded Video
  109. rewardBasedVideo = RewardBasedVideoAd.Instance;
  110. RequestRewardedVideo();
  111. rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
  112. }
  113.  
  114. private void RequestBanner()
  115. {
  116. string adUnitId = "ca-app-pub-2629468278215471/5385429561";
  117. #if UNITY_ANDROID
  118. if (!testing)
  119. {
  120. adUnitId = bannerAdId;
  121. }
  122. #elif UNITY_IPHONE
  123. if(!testing)
  124. {
  125. adUnitId = iosBannerAdId;
  126. }
  127. #else
  128. adUnitId = "unexpected_platform";
  129. #endif
  130. // Create a 320x50 banner at the top of the screen.
  131. Vector2 adsizeVector = new Vector2(Screen.width, Screen.height / 21);
  132. AdSize adSize = new AdSize((int)adsizeVector.x, (int)adsizeVector.y);
  133. bannerSim.sizeDelta = adsizeVector;
  134. bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
  135.  
  136. // Create an empty ad request.
  137. AdRequest request = new AdRequest.Builder().Build();
  138. // Load the banner with the request.
  139. bannerView.LoadAd(request);
  140. }
  141.  
  142. private void RequestInterstitial()
  143. {
  144. string adUnitId = "ca-app-pub-3940256099942544/1033173712";
  145. #if UNITY_ANDROID
  146. if(!testing)
  147. {
  148. adUnitId = interstitialAdId;
  149. }
  150. #elif UNITY_IPHONE
  151. if(!testing)
  152. {
  153. adUnitId = iosInterstitialAdId;
  154. }
  155. #else
  156. adUnitId = "unexpected_platform";
  157. #endif
  158.  
  159. // Initialize an InterstitialAd.
  160. interstitial = new InterstitialAd(adUnitId);
  161. // Create an empty ad request.
  162. AdRequest request = new AdRequest.Builder().Build();
  163. // Load the interstitial with the request.
  164. interstitial.LoadAd(request);
  165. }
  166.  
  167. private void RequestRewardedVideo()
  168. {
  169. string adUnitId = "ca-app-pub-3940256099942544/5224354917";
  170. #if UNITY_ANDROID
  171. if(!testing)
  172. {
  173. adUnitId = rewardedSpin;
  174. }
  175. #elif UNITY_IPHONE
  176. if(!testing)
  177. {
  178. adUnitId = iosRewardedSpin;
  179. }
  180. #else
  181. string adUnitId = "unexpected_platform";
  182. #endif
  183.  
  184. // Create an empty ad request.
  185. AdRequest request = new AdRequest.Builder().Build();
  186. // Load the rewarded video ad with the request.
  187. this.rewardBasedVideo.LoadAd(request, adUnitId);
  188. //IOS not firing Rewarded Properly Handler
  189. #if UNITY_IOS
  190. rewardBasedVideo.OnAdRewarded += IOSReward();
  191. #endif
  192. }
  193.  
  194. private EventHandler<Reward> IOSReward()
  195. {
  196. HandleRewards();
  197. return null;
  198. }
  199.  
  200. public void HandleRewardBasedVideoRewarded(object sender, Reward args)
  201. {
  202. //AudioManager.instance.MuteMusic();
  203. string type = args.Type;
  204. double amount = args.Amount;
  205. MonoBehaviour.print(
  206. "HandleRewardBasedVideoRewarded event received for "
  207. + amount.ToString() + " " + type);
  208. //GameSlyce.LuckyWheel.instance.Spins = 1;
  209. HandleRewards();
  210. }
  211.  
  212. private void HandleRewards()
  213. {
  214. //Convert this to switch later
  215. Dictionary<string, string> gameEvent = new Dictionary<string, string>();
  216. switch (rewardedAdRewardInt)
  217. {
  218. case 1:
  219. // admob set 1 == 60 Gold
  220. PlayerInfo.instance.AddCurrency(20, true);
  221. gameEvent = new Dictionary<string, string>();
  222. gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
  223. GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Spin Coins", gameEvent);
  224. break;
  225. case 2:
  226. //Triple it
  227. PlayerInfo.instance.AddCurrency(tripleCoinRewardAmount);
  228. GameHandler.instance.SpawnCoinsNoValue(GameHandler.instance.UIAnim.gameObject, PlayerInfo.instance.gameplayCurrencyObj.gameObject);
  229. gameEvent = new Dictionary<string, string>();
  230. gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
  231. GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Double Coins", gameEvent);
  232. break;
  233. case 3:
  234. //Free Hint Coins
  235. int goldReward = UnityEngine.Random.Range(60, 80);
  236. PlayerInfo.instance.AddCurrency(goldReward);
  237.  
  238. GameHandler.instance.freeHintWindow.GetComponent<Animator>().SetBool("Active", true);
  239. GameHandler.instance.freeHintGoldAmount.text = goldReward.ToString();
  240.  
  241. GameHandler.instance.SpawnCoinsNoValue(GameHandler.instance.freeHintWindow.gameObject, PlayerInfo.instance.gameplayCurrencyObj.gameObject);
  242. GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Free Hint = Coins", new Dictionary<string, string>());
  243. break;
  244. case 4:
  245. //Gift Hint Use Reward
  246. if (GameHandler.instance.inGame)
  247. {
  248. GameHandler.instance.UseHint();
  249. gameEvent = new Dictionary<string, string>();
  250. gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
  251. GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Free Hint Used", gameEvent);
  252. }
  253. break;
  254. case 5:
  255. InGameStoreManager.instance.RewardWatchedAd();
  256. gameEvent = new Dictionary<string, string>();
  257. gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
  258. GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded,Player Store Hour Decrease", gameEvent);
  259. break;
  260. default:
  261. Debug.LogError("Reward Num Not Implemented.");
  262. break;
  263. }
  264. //if (rewardedAdRewardInt == 1)
  265. //{
  266. // // admob set 1 == 60 Gold
  267. // PlayerInfo.instance.AddCurrency(20, true);
  268. // Dictionary<string, string> gameEvent = new Dictionary<string, string>();
  269. // gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
  270. // GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Spin Coins", gameEvent);
  271. //}
  272. //else if (rewardedAdRewardInt == 2)
  273. //{
  274. // //Admob Set Double Coins
  275. // PlayerInfo.instance.AddCurrency(tripleCoinRewardAmount);
  276. // GameHandler.instance.SpawnCoinsNoValue(GameHandler.instance.UIAnim.gameObject, PlayerInfo.instance.gameplayCurrencyObj.gameObject);
  277. // Dictionary<string, string> gameEvent = new Dictionary<string, string>();
  278. // gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
  279. // GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Double Coins", gameEvent);
  280. //}
  281. //else if (rewardedAdRewardInt == 3)
  282. //{
  283. // //Gift Triple Reward
  284. // int goldReward = UnityEngine.Random.Range(30, 50);
  285. // PlayerInfo.instance.AddCurrency(goldReward);
  286.  
  287. // GameHandler.instance.freeHintWindow.GetComponent<Animator>().SetBool("Active",true);
  288. // GameHandler.instance.freeHintGoldAmount.text = goldReward.ToString();
  289.  
  290. // GameHandler.instance.SpawnCoinsNoValue(GameHandler.instance.freeHintWindow.gameObject, PlayerInfo.instance.gameplayCurrencyObj.gameObject);
  291. // GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Free Hint = Coins", new Dictionary<string, string>());
  292. //}
  293. //else if (rewardedAdRewardInt == 4)
  294. //{
  295. // //Gift Hint Use Reward
  296. // if (GameHandler.instance.inGame)
  297. // {
  298. // GameHandler.instance.UseHint();
  299. // Dictionary<string, string> gameEvent = new Dictionary<string, string>();
  300. // gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
  301. // GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Free Hint Used", gameEvent);
  302. // }
  303. //}
  304. }
  305.  
  306. // public void ShowInterstitial()
  307. // {
  308. // if (PlayerInfo.instance.playerData.showAds && GameHandler.instance.currentLvlNum % 2 == 0 && GameHandler.instance.currentLvlNum > 7)
  309. // {
  310. //#if UNITY_IOS
  311. // ShowUnityInterstitialVideo();
  312. //#else
  313. // if (interstitial.IsLoaded())
  314. // {
  315. // interstitial.Show();
  316. // RequestInterstitial();
  317. // }
  318. // else
  319. // {
  320. // //ShowUnityInterstitialVideo();
  321. // }
  322. //#endif
  323. // Dictionary<string, string> gameEvent = new Dictionary<string, string>();
  324. // gameEvent.Add("Interstitial Shown :", GameHandler.instance.currentLvlNum.ToString());
  325. // GameHandler.instance.SendAnalyticsEvent("Ad : Interstitial", gameEvent);
  326. // }
  327. // //if (PlayerInfo.instance.playerData.showAds && GameHandler.instance.currentLvlNum > 4)
  328. // //{
  329. // // ShowUnityInterstitialVideo();
  330. // //}
  331. // //StartCoroutine(TurnMusicOnTouch());
  332. // }
  333.  
  334. //public void HideBanner()
  335. //{
  336. // //bannerSim.gameObject.SetActive(false);
  337. // bannerView.Hide();
  338. //}
  339.  
  340. //public void ShowBanner()
  341. //{
  342. // //bannerSim.gameObject.SetActive(true);
  343. // if(PlayerInfo.instance.playerData.showAds)
  344. // {
  345. // bannerView.Show();
  346. // }
  347. //}
  348.  
  349. public void ShowDoubleCoins()
  350. {
  351. //The Amount is set by the Level Complete Function
  352. //Disabled, now shown in Level Complete Screen
  353. doubleCoinAnim.GetComponentInChildren<Button>().interactable = true;
  354. doubleCoinAnim.SetTrigger("Appear");
  355. Invoke("HideDoubleCoins", 7f);
  356. }
  357.  
  358. public void HideDoubleCoins()
  359. {
  360. doubleCoinAnim.SetTrigger("Appear");
  361. doubleCoinAnim.GetComponentInChildren<Button>().interactable = false;
  362. }
  363.  
  364. #region UnityAds
  365.  
  366. //void ShowUnityRewardedVideo()
  367. //{
  368. // ShowOptions options = new ShowOptions();
  369. // options.resultCallback = HandleRewardedResult;
  370.  
  371. // Advertisement.Show("rewardedVideo", options);
  372. //}
  373.  
  374. //void HandleRewardedResult(ShowResult result)
  375. //{
  376. // if (result == ShowResult.Finished)
  377. // {
  378. // Debug.Log("Video completed - Offer a reward to the player");
  379. // // Reward your player here.
  380. // HandleRewards();
  381.  
  382. // }
  383. // else if (result == ShowResult.Skipped)
  384. // {
  385. // Debug.LogWarning("Video was skipped - Do NOT reward the player");
  386.  
  387. // }
  388. // else if (result == ShowResult.Failed)
  389. // {
  390. // Debug.LogError("Video failed to show");
  391. // }
  392. //}
  393.  
  394. //void ShowUnityInterstitialVideo()
  395. //{
  396. // ShowOptions options = new ShowOptions();
  397. // options.resultCallback = HandleInterstitialResult;
  398.  
  399. // Advertisement.Show("video", options);
  400. //}
  401.  
  402. //void HandleInterstitialResult(ShowResult result)
  403. //{
  404. // if (result == ShowResult.Finished)
  405. // {
  406. // Debug.Log("Video completed - Offer a reward to the player");
  407. // // Reward your player here.
  408.  
  409. // }
  410. // else if (result == ShowResult.Skipped)
  411. // {
  412. // Debug.LogWarning("Video was skipped - Do NOT reward the player");
  413.  
  414. // }
  415. // else if (result == ShowResult.Failed)
  416. // {
  417. // Debug.LogError("Video failed to show");
  418. // }
  419. //}
  420.  
  421. #endregion
  422.  
  423. private void OnEnable()
  424. {
  425. TDCallbacks.TapdaqConfigLoaded += OnTapdaqConfigLoaded;
  426. TDCallbacks.TapdaqConfigFailedToLoad += OnTapdaqConfigFailToLoad;
  427. TDCallbacks.RewardVideoValidated += OnRewardVideoValidated;
  428. TDCallbacks.AdAvailable += OnAdAvailable;
  429. TDCallbacks.AdNotAvailable += OnAdNotAvailable;
  430. TDCallbacks.AdClicked += OnAdClicked;
  431. }
  432.  
  433. private void OnDisable()
  434. {
  435. TDCallbacks.TapdaqConfigLoaded -= OnTapdaqConfigLoaded;
  436. TDCallbacks.TapdaqConfigFailedToLoad -= OnTapdaqConfigFailToLoad;
  437. TDCallbacks.RewardVideoValidated -= OnRewardVideoValidated;
  438. TDCallbacks.AdAvailable -= OnAdAvailable;
  439. TDCallbacks.AdNotAvailable -= OnAdNotAvailable;
  440. TDCallbacks.AdClicked -= OnAdClicked;
  441. }
  442.  
  443. private void OnTapdaqConfigLoaded()
  444. {
  445. //Tapdaq Ready to use
  446. Debug.Log("Loading Ads Called");
  447. AdManager.LoadRewardedVideo("game_over");
  448. AdManager.LoadVideo("main_menu");
  449. AdManager.RequestBanner(TDMBannerSize.TDMBannerStandard);
  450. }
  451.  
  452. private void OnTapdaqConfigFailToLoad(TDAdError error)
  453. {
  454. //Tapdaq failed to initialise
  455. Debug.LogError("TapDaq Failed To Load");
  456. Debug.LogError("TapDaq : " + error.message);
  457. foreach (KeyValuePair<string, Tapdaq.TDAdError> attachStat in error.subErrors)
  458. {
  459. //Now you can access the key and value both separately from this attachStat as:
  460. Debug.LogError("Key : " + attachStat.Key);
  461. Debug.LogError("Value : " + attachStat.Value);
  462. }
  463. }
  464. void OnApplicationPause(bool pauseStatus)
  465. {
  466. AdManager.OnApplicationPause(pauseStatus);
  467. }
  468.  
  469. private void OnAdAvailable(TDAdEvent e)
  470. {
  471. // Ad has loaded, can now be displayed.
  472. // This method will also be called when a banner is refreshed every 30 seconds even if the banner is already in view. Calling show again is safe, alternatively TDAdEvent message property will note either "LOADED" or "REFRESH"
  473. Debug.Log("Calling OnAdAvailable");
  474. if (e.adType == "BANNER")
  475. {
  476. AdManager.ShowBanner(TDBannerPosition.Bottom);
  477. }
  478. if (e.adType == "REWARD_AD" && e.tag == "game_over")
  479. {
  480. // Add code here to display or enable your Rewarded Video button.
  481. }
  482. }
  483.  
  484. public void OnAdNotAvailable(TDAdEvent adEvent)
  485. {
  486. // Ad has failed to load/refresh
  487. }
  488.  
  489. public void OnAdClicked(TDAdEvent adEvent)
  490. {
  491. // Ad has been clicked
  492. }
  493.  
  494. public void ShowInterstitial()
  495. {
  496. if (AdManager.IsVideoReady("main_menu"))
  497. {
  498. AdManager.ShowVideo("main_menu");
  499. }
  500. }
  501.  
  502. public void ShowBanner()
  503. {
  504. // Ad has loaded, can now be displayed.
  505. // This method will also be called when a banner is refreshed every 30 seconds even if the banner is already in view. Calling show again is safe, alternatively TDAdEvent message property will note either "LOADED" or "REFRESH"
  506. if (AdManager.IsBannerReady())
  507. {
  508. AdManager.ShowBanner(TDBannerPosition.Bottom);
  509. }
  510. }
  511.  
  512. public void HideBanner()
  513. {
  514. AdManager.HideBanner();
  515. }
  516.  
  517. public void ShowRewardedAd(int _rewardId)
  518. {
  519. rewardedAdRewardInt = _rewardId;
  520.  
  521. if (AdManager.IsRewardedVideoReady("game_over"))
  522. {
  523. AdManager.ShowRewardVideo("game_over");
  524. }
  525. }
  526.  
  527. private void OnRewardVideoValidated(TDVideoReward videoReward)
  528. {
  529. Debug.Log("I got " + videoReward.RewardAmount + " of " + videoReward.RewardName
  530. + " tag=" + videoReward.Tag + " IsRewardValid " + videoReward.RewardValid + " CustomJson: " + videoReward.RewardJson);
  531.  
  532. if (videoReward.RewardValid)
  533. {
  534. //Give Reward
  535. HandleRewards();
  536. Dictionary<string, string> gameEvent = new Dictionary<string, string>();
  537. gameEvent.Add("RewardedAd Shown :", GameHandler.instance.currentLvlNum.ToString());
  538. GameHandler.instance.SendAnalyticsEvent("Ad : Rewarded Total", gameEvent);
  539. }
  540. else
  541. {
  542. //Reward is invalid, video may not have completed or an ad network may have refused to the provide reward
  543. }
  544. }
  545. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement