编辑器中的Code Works在应用程序上不起作用

时间:2019-07-08 15:35:14

标签: c# unity3d build

我有在游戏结束时打开插页式广告的代码 该脚本位于上一个场景的游戏对象上。 代码可以在编辑器中正常运行,但是在我的应用中,脚本似乎无法从最后一个场景中找到脚本

我尝试在游戏中添加一些UI和一些if语句来显示发生了什么,看来GameObject.FindGameObjectWithTag(“ AdHandler”)。GetComponent()在应用中返回null,甚至在编辑器中也可以正常工作通常

'''C#菜单广告(最后一个场景中的一个)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
public class MenuAds : MonoBehaviour
{
    private BannerView bannerView;
    public InterstitialAd FullScreenAd;
    public void Start()
    {   
        DontDestroyOnLoad(gameObject);
        #if UNITY_ANDROID
            string appId = "ca-app-pub-3940256099942544~3347511713";
        #elif UNITY_IPHONE
            string appId = "ca-app-pub-3940256099942544~1458002511";
        #else
            string appId = "unexpected_platform";
        #endif

    // Initialize the Google Mobile Ads SDK.
    MobileAds.Initialize(appId);
    this.RequestInterstitial();
    this.RequestBanner();
}
private void RequestInterstitial () {
#if UNITY_ANDROID
    string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
    string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
    string adUnitId = "unexpected_platform";
#endif

   FullScreenAd = new InterstitialAd(adUnitId);
   AdRequest Request = new AdRequest.Builder().Build();
   FullScreenAd.LoadAd(Request);
}
public void ShowFullScreenAd () {
    if(FullScreenAd.IsLoaded()){
        FullScreenAd.Show();
    }
}
private void RequestBanner()
{
    #if UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/6300978111";
    #elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-3940256099942544/2934735716";
    #else
        string adUnitId = "unexpected_platform";
    #endif

    // Create a 320x50 banner at the top of the screen.
    bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);

    // Create an empty ad request.
    AdRequest request = new AdRequest.Builder().Build();

    // Load the banner with the request.
    bannerView.LoadAd(request);
    bannerView.Show();


 }
}
'''

'''C#寻找该脚本的代码

   using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DeathPanelScript : MonoBehaviour
{
    public Text ScoreUI, HighscoreUI, TimeUI, CoinAmountUI,DebugTextUI;
    public Score ScoreScript;
    public int CoinsBeforeBoost, CoinsAfterBoost, AllCoins;
    MenuAds Ads;
void Start()
{
    AllCoins = PlayerPrefs.GetInt("AllCoins",0);
    ScoreUI.text = ScoreScript.ScoreNmbr.ToString();
    HighscoreUI.text = PlayerPrefs.GetInt("HighScore",0).ToString();
    TimeUI.text = GetTime();
    CoinsBeforeBoost = Mathf.FloorToInt(ScoreScript.ScoreNmbr/20);
    CoinsAfterBoost = CoinsBeforeBoost;
    CoinAmountUI.text = CoinsBeforeBoost.ToString();
    Ads = GameObject.FindGameObjectWithTag("AdHandler").GetComponent<MenuAds>();
    Ads.ShowFullScreenAd();
    if(Ads)DebugTextUI.text = ("Ads");
    else DebugTextUI.text = ("No Ads");
}
public void ShowInterstitial () {
    DebugTextUI.text = ("Click");
    Ads = GameObject.FindGameObjectWithTag("AdHandler").GetComponent<MenuAds>();
    if (Ads)
    DebugTextUI.text = ("Ads");
    if(Ads.FullScreenAd.IsLoaded()){
        DebugTextUI.text = ("Ad Loaded");
        Ads.ShowFullScreenAd();
    }
    else
    DebugTextUI.text = ("Ad No Load");
}
public void DoubleCoins (float X){
    //load ad
    //if Watched Ad
    CoinsAfterBoost = CoinsBeforeBoost * 2;
}
string GetTime () {
    float TimeValueRaw = Time.timeSinceLevelLoad;
    int RoundedTimeValue = Mathf.RoundToInt(TimeValueRaw);
    int Minutes,Secondes;
    Secondes = RoundedTimeValue % 60;
    Minutes = Mathf.FloorToInt(RoundedTimeValue/60);
    return (Minutes + " : " + Secondes);
}


public void PlayAgain () {

    PlayerPrefs.SetInt("AllCoins",CoinsAfterBoost+AllCoins);
    Application.LoadLevel("Game");
}
public void BackToMenu () {

    PlayerPrefs.SetInt("AllCoins",CoinsAfterBoost+AllCoins);
    Application.LoadLevel("MainMenu");
    }
}

'''

在编辑器中,当我运行游戏时,我会加载广告 但是当我在手机上运行游戏时,文本卡在了Click上

1 个答案:

答案 0 :(得分:0)

关闭合集并重新打开它,然后在不更改任何代码的情况下构建所有内容的修复程序

ps:在读完该标签后,我才这样做,实际上需要重新启动标签,因此我想如果我的标签尚未注册怎么办?