VungleAdsManager.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class VungleAdsManager : UnitySingleton<VungleAdsManager> {
  5. private string appID = "";
  6. private string placementID = "";
  7. private bool adInited = false;
  8. private System.Action finishCallback = null;
  9. Dictionary<string, bool> placements = new Dictionary<string, bool>();
  10. public void Init(System.Action finishCallback)
  11. {
  12. #if UNITY_IPHONE
  13. appID = "597a18ac6f7cda15620033e1";
  14. placementID = "DEFAULT19439";
  15. placements = new Dictionary<string, bool>
  16. {
  17. { placementID, false }
  18. };
  19. #elif UNITY_ANDROID
  20. appID = "597a181605724327620031ed";
  21. placementID = "DEFAULT72579";
  22. placements = new Dictionary<string, bool>
  23. {
  24. { placementID, false }
  25. };
  26. #endif
  27. string[] array = new string[placements.Keys.Count];
  28. placements.Keys.CopyTo(array, 0);
  29. Vungle.init(appID, array);
  30. Debug.Log("Vungle Inited. appID is "+appID);
  31. this.finishCallback = finishCallback;
  32. InitializeEventHandlers();
  33. }
  34. public void LoadAd(){
  35. Vungle.loadAd(placementID);
  36. Debug.Log("Vungle load ad ");
  37. }
  38. public bool CheckAd(){
  39. Debug.Log("Vungle isAdvertAvailable is "+Vungle.isAdvertAvailable(placementID));
  40. return Vungle.isAdvertAvailable(placementID);
  41. }
  42. public void PlayAd(){
  43. Vungle.playAd(placementID);
  44. Debug.Log("Vungle play ad");
  45. }
  46. private void InitializeEventHandlers() {
  47. Vungle.onAdStartedEvent += (placementID) => {
  48. Debug.Log ("Ad " + placementID + " is starting! Pause your game animation or sound here.");
  49. };
  50. Vungle.onAdFinishedEvent += (placementID, args) => {
  51. Debug.Log ("Ad finished - placementID " + placementID + ", was call to action clicked:" + args.WasCallToActionClicked + ", is completed view:"
  52. + args.IsCompletedView);
  53. if(args.IsCompletedView && finishCallback != null)
  54. finishCallback();
  55. };
  56. Vungle.adPlayableEvent += (placementID, adPlayable) => {
  57. Debug.Log ("Ad's playable state has been changed! placementID " + placementID + ". Now: " + adPlayable);
  58. };
  59. Vungle.onLogEvent += (log) => {
  60. Debug.Log ("Log: " + log);
  61. };
  62. Vungle.onInitializeEvent += () => {
  63. adInited = true;
  64. Debug.Log ("SDK initialized");
  65. //自动加载广告
  66. LoadAd();
  67. };
  68. }
  69. public bool IsAdInited(){
  70. return adInited;
  71. }
  72. void OnApplicationPause(bool pauseStatus) {
  73. if (pauseStatus) {
  74. Vungle.onPause();
  75. }
  76. else {
  77. Vungle.onResume();
  78. }
  79. }
  80. }