如何对 rewaredvideo 广告实施不同的奖励
How to implement different reward for rewaredvideo ads
我的游戏里有幸运轮,可以在看视频广告的时候玩。这个轮子可以提供 4 种奖励。(4 颗钻石、8 颗钻石、累积奖金、奖金级别)现在我不知道如何完成这项工作。沿着这个,我的每个级别都有双硬币按钮,它也会在观看广告时触发。
这是我的 admanager 脚本,它链接到我调用 ShowRewardedVideo() 的双硬币按钮
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using admob;
using UnityEngine.SceneManagement;
public class AdManager : MonoBehaviour
{
private static AdManager _instance;
public static AdManager Instance
{
get
{
if (_instance == null)
{
_instance = GameObject.FindObjectOfType<AdManager>();
}
return _instance;
}
}
public Admob ad;
string appID = "";
string bannerID = "";
string interstitialID = "";
string videoID = "";
string nativeBannerID = "";
void Awake()
{
DontDestroyOnLoad(gameObject);
//Replace with your IDs here. These are test ids for you to check if the ads are implemented properly or not
#if UNITY_IOS
appID="ca-app-pub-6533984286287209~3970651947";
bannerID="ca-app-pub-3940256099942544/6300978111";
interstitialID="ca-app-pub-3940256099942544/1033173712";
videoID="ca-app-pub-3940256099942544/5224354917";
nativeBannerID = "ca-app-pub-3940256099942544/2247696110";
#elif UNITY_ANDROID
appID = "ca-app-pub-1147416347412616~8130084612";
bannerID = "ca-app-pub-3940256099942544/6300978111";
interstitialID = "ca-app-pub-3940256099942544/1033173712";
videoID = "ca-app-pub-3940256099942544/5224354917";
nativeBannerID = "ca-app-pub-3940256099942544/2247696110";
#endif
AdProperties adProperties = new AdProperties();
adProperties.isTesting=true;
adProperties.isAppMuted=false;
adProperties.isUnderAgeOfConsent=false;
adProperties.appVolume=100;
ad = Admob.Instance();
ad.bannerEventHandler += onBannerEvent;
ad.interstitialEventHandler += onInterstitialEvent;
ad.rewardedVideoEventHandler += onRewardedVideoEvent;
// ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
ad.initSDK(appID,adProperties);//reqired,adProperties can been null
ad.loadInterstitial(interstitialID);
InvokeRepeating("CacheVideoAd", 1, 5);
}
void CacheVideoAd()
{
if (!ad.isRewardedVideoReady())
{
ad.loadRewardedVideo(videoID);
ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
}
}
IEnumerator LoadGameScene()
{
yield return new WaitForSeconds(2);
SceneManager.LoadSceneAsync("MainMenu");
}
public bool IsAdReady()
{
#if UNITY_EDITOR
return false;
#endif
return ad.isRewardedVideoReady();
ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
}
public void ShowInterstitial()
{
//To give a call back to this use the code below
//AdManager.Instance.ShowInterstitial();
#if UNITY_ANDROID
print("touch inst button -------------");
try
{
if (ad.isInterstitialReady())
{
ad.showInterstitial();
}
else
{
ad.loadInterstitial(interstitialID);
}
}
catch (System.Exception e)
{
print(e);
}
#endif
}
public void ShowBanner()
{
//To give a call back to this use the code below
//AdManager.Instance.ShowBanner();
Admob.Instance().showBannerRelative(bannerID, AdSize.SMART_BANNER, AdPosition.BOTTOM_CENTER);
}
public void DestroyBanner()
{
//To give a call back to this use the code below
//AdManager.Instance.DestroyBanner();
Admob.Instance().removeBanner();
}
public void ShowRewardedVideo()
{
//To give a call back to this use the code below
//AdManager.Instance.ShowRewardedVideo();
Debug.Log("touch video button -------------");
if (ad.isRewardedVideoReady())
{
ad.showRewardedVideo();
}
else
{
ad.loadRewardedVideo(videoID);
ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
}
}
void onInterstitialEvent(string eventName, string msg)
{
Debug.Log("handler onAdmobEvent---" + eventName + " " + msg);
if (eventName == AdmobEvent.onAdLoaded)
{
// Admob.Instance().showInterstitial();
}
}
void onBannerEvent(string eventName, string msg)
{
Debug.Log("handler onAdmobBannerEvent---" + eventName + " " + msg);
}
void onRewardedVideoEvent(string eventName, string msg)
{
Debug.Log("handler onRewardedVideoEvent---" + eventName + " rewarded: " + msg);
if(eventName == AdmobEvent.onRewarded)
{
//Add reward condition here
SaveManager.Instance.DiamondReward();
ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
Debug.Log("diamond reward which is (4 Diamonds)");
}
}
}
这是附加到微调轮的 SpinManager 脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.CrossPlatformInput;
public class SpiningManager : MonoBehaviour {
int randVal;
private float timeInterval;
private bool isCoroutine;
private int finalAngle;
public Text winText;
public int section;
float totalAngle;
public string[] PrizeName;
// Use this for initialization
private MenuScene g;
private void Start () {
isCoroutine = true;
totalAngle = 360 / section;
g = GameObject.FindObjectOfType<MenuScene>();
}
// Update is called once per frame
private void Update () {
float xRotate = CrossPlatformInputManager.GetAxis("Horizontal");
if (xRotate == 1 && isCoroutine) {
StartCoroutine (Spin ());
}
}
private IEnumerator Spin(){
isCoroutine = false;
randVal = Random.Range (80, 150);
timeInterval = Time.deltaTime*2;
for (int i = 0; i < randVal; i++) {
transform.Rotate (0, 0, (totalAngle/2)); //Start Rotate
//To slow Down Wheel
if (i > Mathf.RoundToInt (randVal * .2f))//0.2f
timeInterval = 0.5f*Time.deltaTime;
if (i > Mathf.RoundToInt (randVal * .5f))//0.2f
timeInterval = 1f*Time.deltaTime;
if (i > Mathf.RoundToInt (randVal * .7f))//0.2f
timeInterval = 1.5f*Time.deltaTime;
if (i > Mathf.RoundToInt (randVal * .8f))//0.2f
timeInterval = 2f*Time.deltaTime;
if (i > Mathf.RoundToInt (randVal * .9f))
timeInterval = 2.5f*Time.deltaTime;
yield return new WaitForSeconds (timeInterval);
}
if (Mathf.RoundToInt (transform.eulerAngles.z) % totalAngle != 0) //when the indicator stop between 2 numbers,it will add aditional step
transform.Rotate (0, 0, totalAngle/2);
finalAngle = Mathf.RoundToInt (transform.eulerAngles.z);//round off euler angle of wheel value
print (finalAngle);
//Prize check
for (int i = 0; i < section; i++) {
if (finalAngle == i * totalAngle)
{
winText.text = PrizeName[i];
if(winText.text == "BONUS LEVEL")
{
Debug.Log("Play Bonus Level");
}
else if(winText.text == "DIAMONDS X8")
{
Debug.Log("Give DIAMONDS X8");
}
else if (winText.text == "DIAMONDS X4")
{
Debug.Log("Give DIAMONDS X4");
}
else if (winText.text == "JACKPOT")
{
Debug.Log("Give JACKPOT");
}
}
}
isCoroutine = true;
}
}
SpiningManager.cs:
public enum RewardType
{PLAYBONUSLEVEL=0,
DIAMONDSX8,
DIAMONDSX4=2,
JACKPOT=3};
public static RewardType currentRewardType;
for (int i = 0; i < section; i++)
{
if (finalAngle == i * totalAngle)
{
winText.text = PrizeName[i];
if(winText.text == "BONUS LEVEL")
{
currentRewardType= RewardType.PLAYBONUSLEVEL;
Debug.Log("Play Bonus Level");
}
else if(winText.text == "DIAMONDS X8")
{
currentRewardType= RewardType.DIAMONDSX8;
Debug.Log("Give DIAMONDS X8");
}
else if (winText.text == "DIAMONDS X4")
{
currentRewardType= RewardType.DIAMONDSX4;
Debug.Log("Give DIAMONDS X4");
}
else if (winText.text == "JACKPOT")
{
currentRewardType= RewardType.JACKPOT;
Debug.Log("Give JACKPOT");
}
}
}
AdManager.cs:
void onRewardedVideoEvent(string eventName, string msg)
{
Debug.Log("handler onRewardedVideoEvent---" + eventName + " rewarded: " + msg);
if(eventName == AdmobEvent.onRewarded)
{
Switch(SpiningManager.currentRewardType)
{
case SpiningManager.RewardType.PLAYBONUSLEVEL:
//Reward for Bonus
break;
case SpiningManager.RewardType.DIAMONDSX8:
//Reward for DIAMONDSX8
break;
case SpiningManager.RewardType.DIAMONDSX4:
//Reward for DIAMONDSX4
SaveManager.Instance.DiamondReward();
break;
case SpiningManager.RewardType.JACKPOT:
//Reward for JACKPOT
break;
}
//Add reward condition here
ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
}
}
我通过为每个奖励设置 playerprefs 密钥并检查 playerprefs 密钥是否可用于在 onrewarded() 函数中获得奖励来解决这个问题,并在声明后删除该密钥。
我的游戏里有幸运轮,可以在看视频广告的时候玩。这个轮子可以提供 4 种奖励。(4 颗钻石、8 颗钻石、累积奖金、奖金级别)现在我不知道如何完成这项工作。沿着这个,我的每个级别都有双硬币按钮,它也会在观看广告时触发。
这是我的 admanager 脚本,它链接到我调用 ShowRewardedVideo() 的双硬币按钮
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using admob;
using UnityEngine.SceneManagement;
public class AdManager : MonoBehaviour
{
private static AdManager _instance;
public static AdManager Instance
{
get
{
if (_instance == null)
{
_instance = GameObject.FindObjectOfType<AdManager>();
}
return _instance;
}
}
public Admob ad;
string appID = "";
string bannerID = "";
string interstitialID = "";
string videoID = "";
string nativeBannerID = "";
void Awake()
{
DontDestroyOnLoad(gameObject);
//Replace with your IDs here. These are test ids for you to check if the ads are implemented properly or not
#if UNITY_IOS
appID="ca-app-pub-6533984286287209~3970651947";
bannerID="ca-app-pub-3940256099942544/6300978111";
interstitialID="ca-app-pub-3940256099942544/1033173712";
videoID="ca-app-pub-3940256099942544/5224354917";
nativeBannerID = "ca-app-pub-3940256099942544/2247696110";
#elif UNITY_ANDROID
appID = "ca-app-pub-1147416347412616~8130084612";
bannerID = "ca-app-pub-3940256099942544/6300978111";
interstitialID = "ca-app-pub-3940256099942544/1033173712";
videoID = "ca-app-pub-3940256099942544/5224354917";
nativeBannerID = "ca-app-pub-3940256099942544/2247696110";
#endif
AdProperties adProperties = new AdProperties();
adProperties.isTesting=true;
adProperties.isAppMuted=false;
adProperties.isUnderAgeOfConsent=false;
adProperties.appVolume=100;
ad = Admob.Instance();
ad.bannerEventHandler += onBannerEvent;
ad.interstitialEventHandler += onInterstitialEvent;
ad.rewardedVideoEventHandler += onRewardedVideoEvent;
// ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
ad.initSDK(appID,adProperties);//reqired,adProperties can been null
ad.loadInterstitial(interstitialID);
InvokeRepeating("CacheVideoAd", 1, 5);
}
void CacheVideoAd()
{
if (!ad.isRewardedVideoReady())
{
ad.loadRewardedVideo(videoID);
ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
}
}
IEnumerator LoadGameScene()
{
yield return new WaitForSeconds(2);
SceneManager.LoadSceneAsync("MainMenu");
}
public bool IsAdReady()
{
#if UNITY_EDITOR
return false;
#endif
return ad.isRewardedVideoReady();
ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
}
public void ShowInterstitial()
{
//To give a call back to this use the code below
//AdManager.Instance.ShowInterstitial();
#if UNITY_ANDROID
print("touch inst button -------------");
try
{
if (ad.isInterstitialReady())
{
ad.showInterstitial();
}
else
{
ad.loadInterstitial(interstitialID);
}
}
catch (System.Exception e)
{
print(e);
}
#endif
}
public void ShowBanner()
{
//To give a call back to this use the code below
//AdManager.Instance.ShowBanner();
Admob.Instance().showBannerRelative(bannerID, AdSize.SMART_BANNER, AdPosition.BOTTOM_CENTER);
}
public void DestroyBanner()
{
//To give a call back to this use the code below
//AdManager.Instance.DestroyBanner();
Admob.Instance().removeBanner();
}
public void ShowRewardedVideo()
{
//To give a call back to this use the code below
//AdManager.Instance.ShowRewardedVideo();
Debug.Log("touch video button -------------");
if (ad.isRewardedVideoReady())
{
ad.showRewardedVideo();
}
else
{
ad.loadRewardedVideo(videoID);
ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
}
}
void onInterstitialEvent(string eventName, string msg)
{
Debug.Log("handler onAdmobEvent---" + eventName + " " + msg);
if (eventName == AdmobEvent.onAdLoaded)
{
// Admob.Instance().showInterstitial();
}
}
void onBannerEvent(string eventName, string msg)
{
Debug.Log("handler onAdmobBannerEvent---" + eventName + " " + msg);
}
void onRewardedVideoEvent(string eventName, string msg)
{
Debug.Log("handler onRewardedVideoEvent---" + eventName + " rewarded: " + msg);
if(eventName == AdmobEvent.onRewarded)
{
//Add reward condition here
SaveManager.Instance.DiamondReward();
ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
Debug.Log("diamond reward which is (4 Diamonds)");
}
}
}
这是附加到微调轮的 SpinManager 脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.CrossPlatformInput;
public class SpiningManager : MonoBehaviour {
int randVal;
private float timeInterval;
private bool isCoroutine;
private int finalAngle;
public Text winText;
public int section;
float totalAngle;
public string[] PrizeName;
// Use this for initialization
private MenuScene g;
private void Start () {
isCoroutine = true;
totalAngle = 360 / section;
g = GameObject.FindObjectOfType<MenuScene>();
}
// Update is called once per frame
private void Update () {
float xRotate = CrossPlatformInputManager.GetAxis("Horizontal");
if (xRotate == 1 && isCoroutine) {
StartCoroutine (Spin ());
}
}
private IEnumerator Spin(){
isCoroutine = false;
randVal = Random.Range (80, 150);
timeInterval = Time.deltaTime*2;
for (int i = 0; i < randVal; i++) {
transform.Rotate (0, 0, (totalAngle/2)); //Start Rotate
//To slow Down Wheel
if (i > Mathf.RoundToInt (randVal * .2f))//0.2f
timeInterval = 0.5f*Time.deltaTime;
if (i > Mathf.RoundToInt (randVal * .5f))//0.2f
timeInterval = 1f*Time.deltaTime;
if (i > Mathf.RoundToInt (randVal * .7f))//0.2f
timeInterval = 1.5f*Time.deltaTime;
if (i > Mathf.RoundToInt (randVal * .8f))//0.2f
timeInterval = 2f*Time.deltaTime;
if (i > Mathf.RoundToInt (randVal * .9f))
timeInterval = 2.5f*Time.deltaTime;
yield return new WaitForSeconds (timeInterval);
}
if (Mathf.RoundToInt (transform.eulerAngles.z) % totalAngle != 0) //when the indicator stop between 2 numbers,it will add aditional step
transform.Rotate (0, 0, totalAngle/2);
finalAngle = Mathf.RoundToInt (transform.eulerAngles.z);//round off euler angle of wheel value
print (finalAngle);
//Prize check
for (int i = 0; i < section; i++) {
if (finalAngle == i * totalAngle)
{
winText.text = PrizeName[i];
if(winText.text == "BONUS LEVEL")
{
Debug.Log("Play Bonus Level");
}
else if(winText.text == "DIAMONDS X8")
{
Debug.Log("Give DIAMONDS X8");
}
else if (winText.text == "DIAMONDS X4")
{
Debug.Log("Give DIAMONDS X4");
}
else if (winText.text == "JACKPOT")
{
Debug.Log("Give JACKPOT");
}
}
}
isCoroutine = true;
}
}
SpiningManager.cs:
public enum RewardType
{PLAYBONUSLEVEL=0,
DIAMONDSX8,
DIAMONDSX4=2,
JACKPOT=3};
public static RewardType currentRewardType;
for (int i = 0; i < section; i++)
{
if (finalAngle == i * totalAngle)
{
winText.text = PrizeName[i];
if(winText.text == "BONUS LEVEL")
{
currentRewardType= RewardType.PLAYBONUSLEVEL;
Debug.Log("Play Bonus Level");
}
else if(winText.text == "DIAMONDS X8")
{
currentRewardType= RewardType.DIAMONDSX8;
Debug.Log("Give DIAMONDS X8");
}
else if (winText.text == "DIAMONDS X4")
{
currentRewardType= RewardType.DIAMONDSX4;
Debug.Log("Give DIAMONDS X4");
}
else if (winText.text == "JACKPOT")
{
currentRewardType= RewardType.JACKPOT;
Debug.Log("Give JACKPOT");
}
}
}
AdManager.cs:
void onRewardedVideoEvent(string eventName, string msg)
{
Debug.Log("handler onRewardedVideoEvent---" + eventName + " rewarded: " + msg);
if(eventName == AdmobEvent.onRewarded)
{
Switch(SpiningManager.currentRewardType)
{
case SpiningManager.RewardType.PLAYBONUSLEVEL:
//Reward for Bonus
break;
case SpiningManager.RewardType.DIAMONDSX8:
//Reward for DIAMONDSX8
break;
case SpiningManager.RewardType.DIAMONDSX4:
//Reward for DIAMONDSX4
SaveManager.Instance.DiamondReward();
break;
case SpiningManager.RewardType.JACKPOT:
//Reward for JACKPOT
break;
}
//Add reward condition here
ad.rewardedVideoEventHandler -= onRewardedVideoEvent;
}
}
我通过为每个奖励设置 playerprefs 密钥并检查 playerprefs 密钥是否可用于在 onrewarded() 函数中获得奖励来解决这个问题,并在声明后删除该密钥。