首页 > 解决方案 > 找不到类型或命名空间名称“GameManager”

问题描述

几天来,我一直在尝试为我的第一个游戏编写一些代码,这样如果用户没有观看完整的奖励视频广告(来自 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 的函数,可以让我显示广告:第一个,在按下游戏内按钮“Watch for extra life”时调用

public void ShowReward()
{
        birdParent.SetActive(false);
        ads.instance.ShowRewardVideo();
        gameOverCanvas.SetActive(false);
        score.SetActive(false);
      ->rewardPanel.SetActive(true);
        Time.timeScale = 0;
}

单击该按钮并开始播放视频后,正如您在功能中看到的那样,带有奖励的面板被激活(无论您观看了完整的视频还是关闭了它)。然后,在完成视频后,您会回到游戏,其中有一个面板,上面写着“感谢您观看视频,这是您的额外生活。按 OK”。当按下按钮 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 对象中)中的那个脚本中,所以我可以插入或调用该手柄工具中的功能,以防止用户在想要获得奖励时跳过视频。我试图在广告脚本中声明一个“公共 GameManager gameManager”(因此之后我应该能够调用广告脚本中的函数)但它根本不起作用,我从标题中得到错误。

我不知道你是否明白我想做什么,但如果你有任何想法,请写一个回复,因为我在这件事上苦苦挣扎了几个小时,只是想不通..或者如果你知道任何其他方式来做到这一点..

编辑: 在此处输入图像描述

因此,基本上,在 GameObject 中,我有脚本“广告”,在其中我想从 GameManager 对象的“GameManager”脚本中调用一些函数。即使我在 GM 脚本中声明了公共静态 GameManager 实例,我也无法在广告脚本中调用 GameManager.instance.ShowReward() 或公共 GameManager GameManager。我应该怎么做?

在此处输入图像描述

在此处输入图像描述

有趣的是,正如你在最后一张照片中看到的那样,我有一个鸟游戏对象,它关联了一个鸟脚本,在其中我可以调用 gamemanager 脚本。这不是因为大写字母,我尝试了两个 gameManager和游戏管理器

在此处输入图像描述

标签: c#unity3dadmob

解决方案


在这种情况下,所有迹象都指向 Unity 试图强制ads编译 before GameManager

进入 Unity 编辑器并移出ads目录GoogleMobileAds并进入不同的Assets目录,因为它可能GoogleMobileAds专门配置为先/早编译。

万一不起作用,请将ads其放在与GameManager.


推荐阅读