我一直在尝试为第一款游戏编写一些代码,这样,如果用户没有观看(来自admob的)完整的奖励视频广告,并且他更早关闭了该视频,则他不会要求获得实际的奖励额外的生活。
我试图做的第一件事是移动游戏对象(关联的“广告脚本”)从菜单场景的游戏场景,因为它更容易有从游戏场景的功能的访问,我想..
看一些后教程,以下是“奖励视频广告”的代码:
public class ads : MonoBehaviour
{
private RewardedAd rewardedAd;
private void RequestReward()
{
string adUnitId = "ca-app-pub-3940256099942544/5224354917";
this.rewardedAd = new RewardedAd(adUnitId);
this.rewardedAd.OnAdLoaded += this.HandleOnRewardedAdLoaded;
this.rewardedAd.OnUserEarnedReward += this.HandleOnRewarded;
this.rewardedAd.OnAdClosed += this.HandleOnRewardedAdClosed;
AdRequest request = new AdRequest.Builder().Build();
this.rewardedAd.LoadAd(request);
}
public void ShowRewardVideo()
{
if (this.rewardedAd.IsLoaded())
{
this.rewardedAd.Show();
}
else
{
Debug.Log("Reward video is not ready yet");
}
}
public void HandleOnRewardedAdLoaded(object sender, EventArgs args)
{
ShowRewardVideo();
}
public void HandleOnRewarded(object sender, EventArgs args)
{
// SO, If I understand properly, if I want players not to receive the extra life<br>
before the reward video is finished, I have to write here the code for receiving the extra life
instead of writing it in the GameManager script. (script that is down below)
}
public void HandleOnRewardedAdClosed(object sender, EventArgs args)
{
this.rewardedAd.OnAdLoaded -= this.HandleOnRewardedAdLoaded;
this.rewardedAd.OnUserEarnedReward -= this.HandleOnRewarded;
this.rewardedAd.OnAdClosed -= this.HandleOnRewardedAdClosed;
}
在这里,我有GameScript的两个函数,这些函数可以让我展示广告:第一个,当按下游戏中的按钮“观看额外的生命”时调用
public void ShowReward()
{
birdParent.SetActive(false);
ads.instance.ShowRewardVideo();
gameOverCanvas.SetActive(false);
score.SetActive(false);
->rewardPanel.SetActive(true);
Time.timeScale = 0;
}
单击该按钮后,视频开始播放,如您在功能中所看到的,带有奖励的面板被激活(无论您是观看完整视频还是关闭完整视频)。然后,在完成视频播放后,您会回到游戏中,在面板上显示诸如“感谢您观看视频,这是您的额外生命。请按OK”。当按下“确定”按钮时,将调用此功能
public void ContinueAfterReward()
{
score.SetActive(true);
rewardPanel.SetActive(false);
Time.timeScale = 1;
Start();
GameHasStarted();
bird.Start();
bird.Update();
score.SetActive(true);
}
问题是这两个函数位于“ GameManager”脚本(位于GameManager对象中)中,但我希望它们以某种方式集成到“ ads”脚本(位于标准GameObject对象中)中的脚本中,因此我可以在该句柄工具中插入或调用函数,以防止用户在获得奖励时跳过视频。我试图在广告脚本中声明一个“ public GameManager gameManager”(因此之后我应该能够调用广告脚本中的函数),但是它根本不起作用,我从标题中得到了错误。
我不知道您是否了解我想做的事,但是如果您有任何想法请写答复,因为我在这件事上苦苦挣扎了几个小时,只是想不通..或者您是否知道其他方式
因此,基本上,在GameObject中,我有脚本“ ads”,在其中,我想从GameManager对象的“ GameManager”脚本中调用某些函数。即使我在GM脚本中声明了公共静态GameManager实例,也无法在ads脚本中调用GameManager.instance.ShowReward()或public GameManager GameManager。我该怎么做?
有趣的是,如您在上一张照片中所看到的,我有一个与鸟脚本相关联的鸟游戏对象,并且在其中可以调用gamemanager脚本..并不是因为大写字母,我尝试了两个gameManager和GameManager
在这种情况下,所有迹象都表明Unity试图在ads
之前强制编译GameManager
。
进入统一的编辑和移动ads
的出GoogleMobileAds
目录,并到不同的Assets
目录,因为它可能是GoogleMobileAds
具体用于第一编译/月初。
在极少数情况下无法正常工作的情况下,请与放入ads
同一目录GameManager
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句