Created
August 23, 2020 23:25
-
-
Save arkilis/5d73dd5ebb5cd0ef2423b9ea81ea4ff8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
using UnityEngine.UI; | |
public class RewardedVideoButton : MonoBehaviour, IUnityAdsListener { | |
#if UNITY_IOS | |
private string gameId = "3685328"; | |
#elif UNITY_ANDROID | |
private string gameId = "3685329"; | |
#endif | |
Button myButton; | |
public string myPlacementId = "rewardedVideo"; | |
void Start () { | |
myButton = GetComponent <Button> (); | |
// Set interactivity to be dependent on the Placement’s status: | |
myButton.interactable = Advertisement.IsReady (myPlacementId); | |
// Map the ShowRewardedVideo function to the button’s click listener: | |
if (myButton) myButton.onClick.AddListener (ShowRewardedVideo); | |
// Initialize the Ads listener and service: | |
Advertisement.AddListener (this); | |
Advertisement.Initialize (gameId, true); | |
} | |
// Implement a function for showing a rewarded video ad: | |
void ShowRewardedVideo () { | |
Advertisement.Show (myPlacementId); | |
} | |
// Implement IUnityAdsListener interface methods: | |
public void OnUnityAdsReady (string placementId) { | |
// If the ready Placement is rewarded, activate the button: | |
if (placementId == myPlacementId) { | |
myButton.interactable = true; | |
} | |
} | |
public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) { | |
// Define conditional logic for each ad completion status: | |
if (showResult == ShowResult.Finished) { | |
// Reward the user for watching the ad to completion. | |
Debug.Log("Player gain extra life"); | |
} else if (showResult == ShowResult.Skipped) { | |
// Do not reward the user for skipping the ad. | |
} else if (showResult == ShowResult.Failed) { | |
Debug.LogWarning ("The ad did not finish due to an error."); | |
} | |
} | |
public void OnUnityAdsDidError (string message) { | |
// Log the error. | |
} | |
public void OnUnityAdsDidStart (string placementId) { | |
// Optional actions to take when the end-users triggers an ad. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment