【问题标题】:Unity Main thread issue about ads关于广告的 Unity 主线程问题
【发布时间】:2019-09-03 22:36:05
【问题描述】:

我几乎在 Unity 上完成了我的游戏。我只是在使用 Admob 来完成它。问题在于奖励视频。我有测试广告并正在处理它们,但我的奖励功能无法正常工作。我正在尝试在 Android Studio 上对其进行调试,有关过程的主要错误是:

UnityException: get_transform 只能从主线程调用。 加载场景时,构造函数和字段初始化程序将从加载线程中执行。 不要在构造函数或字段初始化程序中使用此函数,而是将初始化代码移至 Awake 或 Start 函数。 在(包装器托管到本机)UnityEngine.GameObject.get_transform(UnityEngine.GameObject) 在 C:\Users\s.gireniz\Desktop\bridgeball\BridgeBall\Assets\ContinueScript.cs:39 中的 ContinueScript.ContinueGame () [0x00018]

  at AdmobManager.GiveReward () [0x0000c] in C:\Users\s.gireniz\Desktop\bridgeball\BridgeBall\Assets\AdmobManager.cs:272

  at AdmobManager.HandleRewardBasedVideoRewarded (System.Object sender, GoogleMobileAds.Api.Reward args) [0x00037] in

C:\Users\s.gireniz\Desktop\bridgeball\BridgeBall\Assets\AdmobManager.cs:263

  at GoogleMobileAds.Api.RewardBasedVideoAd.<.ctor>b__4_6 (System.Object sender, GoogleMobileAds.Api.Reward args) [0x0000f] in

C:\Users\s.gireniz\Desktop\bridgeball\BridgeBall\Assets\G

我得到这个错误,就像我将分享的奖励脚本的每一步一样。我正在尝试修复它,但是对于每一行代码,都会出现主线程问题。这是我离开的步骤的错误,它出现在“get_transform”中。我怎样才能清楚地解决这个问题而不是随机做呢?这个错误的原因是什么?你们能帮帮我吗?

这是我下面的奖励脚本。

public void WatchTheAd()
{
    Debug.Log("Displaying Ad!!!!!");
    AdmobManager.instance.ShowRewarded();
}

public void ContinueGame()
{
    Debug.Log("Position Fixed !!!!!");
    //Target.transform.position = new Vector3(0, 1.5f, -200f);
    Player.GetComponent<Renderer>().enabled = true;

    for (int i = 0; i < Obstacles.Length; i++)
    {
        if (Vector3.Distance(Obstacles[i].transform.position, playerPos) < 
        200f)
        {
            Destroy(Obstacles[i]);
        }
    }

    Debug.Log("Is Dead False !!!!!");

    FindObjectOfType<PlayerCollision>().isDead = false;
    Destroy(ReplayButton);
    Destroy(WatchAd);
    Debug.Log("Buttons Destroyed !!!!!");

    GameObject Canvas = GameObject.FindGameObjectWithTag("Canvas") as GameObject;
    GameObject ContinueButton = Instantiate(AfterAdButton, new Vector3(0, 0, 1), Quaternion.identity)as GameObject;

    ContinueButton.transform.SetParent(Canvas.transform, false);
    Debug.Log("Button Instantiated !!!!!");
    Time.timeScale = 0f;
}

void Update()
{
    playerPos = Player.transform.position;
    WatchAd = GameObject.FindGameObjectWithTag("WatchAd");
    ReplayButton = GameObject.FindGameObjectWithTag("ReplayButton");
}

这是我正在尝试处理的 AdmobManager 的一些行。

public void ShowRewarded()
{
    if (rewarded.IsLoaded())
    {
        Debug.Log("Ad Displayed!!!!!");
        rewarded.Show();
    }
    else
    {
        //FB.LogAppEvent("AdmobRewardedFailToLoad");
        GiveReward();
        RequestRewardBasedVideo();
    } 
}

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
    string type = args.Type;
    double amount = args.Amount;
    Debug.Log(
        "HandleRewardBasedVideoRewarded event received for "
        + amount.ToString() + " " + type);


    // FB.LogAppEvent("AdmobRewardedView");
    Debug.Log("Reward Function Called!!!!!");
    GiveReward();

    this.RequestRewardBasedVideo();
}

private void GiveReward()
{
    Debug.Log("Reward Given!!!!!");
    ContinueScript.instance.ContinueGame();
}

【问题讨论】:

  • 现在我认为那里不足以算作minimal reproducible example 最大的提示似乎是 continuescript.instance.continuegame.. 暗示它可能已被卸载到不同的线程..
  • 哪个是你的ContinueScript,哪个是行39

标签: c# visual-studio android-studio unity3d


【解决方案1】:

尝试使用 IEnumerator

   public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
Debug.Log(
    "HandleRewardBasedVideoRewarded event received for "
    + amount.ToString() + " " + type);

  StartCoroutine(AfunctionName());

}


IEnumerator AfunctionName()
{
 yield return new WaitForSecondsRealtime(0.1f);

 // FB.LogAppEvent("AdmobRewardedView");
    Debug.Log("Reward Function Called!!!!!");
    GiveReward();

    this.RequestRewardBasedVideo();
}

试一试。我有同样的问题,这解决了我的问题:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多