Created
May 26, 2019 05:21
-
-
Save iamgabrielma/834e03ca8592074d784ad96818f5acfa to your computer and use it in GitHub Desktop.
Ad Manager that deals with Unity Ads between gameplays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Advertisements; | |
namespace Ngu | |
{ | |
public class AdManager : MonoBehaviour | |
{ | |
public bool isAdRunning = true; | |
public void PlayAdvertising() | |
{ | |
var options = new ShowOptions { resultCallback = HandleShowResult }; // TODO: (?) | |
if (Advertisement.IsReady("video")) //placement ID parameter, can be others from https://operate.dashboard.unity3d.com/ Overview > Monetization > Placements, pe: rewardedvideos cannot be skipped. | |
{ | |
Advertisement.Show("video", options); | |
Time.timeScale = 0; // Pauses game while the add is running | |
} | |
} | |
private void HandleShowResult(ShowResult result) | |
{ | |
switch (result) | |
{ | |
// TODO: Seems that in TEST MODE, if I click on "close", the ad result is shown. Test this on real to see if is the same case. | |
case ShowResult.Finished: | |
Debug.Log("The ad was successfully shown."); | |
// YOUR CODE TO REWARD THE GAMER | |
break; | |
case ShowResult.Skipped: | |
Debug.Log("The ad was skipped before reaching the end."); | |
break; | |
case ShowResult.Failed: | |
Debug.LogError("The ad failed to be shown."); | |
break; | |
} | |
Time.timeScale = 1; // Restarts game | |
isAdRunning = false; // Game returns to normal after we have an ad result | |
} | |
} | |
} | |
/* Calling the script: This part is added within the GameManager, or when it should be called specifically */ | |
AdManager adManagerScript; | |
adManagerScript = GetComponent<AdManager>(); | |
adManagerScript.PlayAdvertising(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment