Unityでset_timeScale can only be called from the main thread. というエラーが出る件

スポンサーリンク
Android
スポンサーリンク
↑管理人が個人でUnity+Live2Dで作成しているスマホゲームです

Android Studio付属ツールmonitor.batを使ってUnityのAndroid実機デバッグでログを確認していたのですが、どうもAdMobのインタースティシャル広告の開始時および復帰時の時に「E/Unity(22202): set_timeScale can only be called from the main thread.」というエラーが出てしまいます。

Time.timeScaleをいじってゲームを止めたり再開したり処理をOnAdOpeningイベントハンドラやOnAdClosedイベントハンドラで加えていたのですが、これがまずかったのかもしれません。
そのままの意味で考えると、メインスレッドでタイムスケールは弄れやという事らしいので大人しく従う事にします(´・ω・`)

あ、よく見ると「 E/Unity(23828): Find can only be called from the main thread.」「E/Unity(25414): SetActive can only be called from the main thread.」というのも出てますね。Findは使わずにSerializeFieldを使う事にして、イベントハンドラ内ではメインスレッド内のコードのフラグを立てるだけにして、実際の処理はUpdate()の中で行う事にしました。

    public enum AdState {NotShowing,JustStart,NowShowing,JustFinished};
    public AdState nowInterstitial = AdState.NotShowing;

    void Update () {
        if (nowInterstitial == AdState.JustStart)
        {
            Time.timeScale = 0;
            AudioListener.pause = true;
            nowInterstitial = AdState.NowShowing;
        }
        else if (nowInterstitial == AdState.JustFinished)
        {
            Time.timeScale = 1;
            AudioListener.pause = false;
            nowInterstitial = AdState.NotShowing;
            
            //次シーン遷移
            loadingUi.SetActive(true);
            StartCoroutine(LoadScene());
        }
    }

AdMob広告クラスのイベントハンドラ内ではnowInterstitialの値を変えるだけです。これでバグが綺麗に消えました!

1 Star2 Stars3 Stars4 Stars5 Stars (まだ投票されていません)
読み込み中...

コメント

広告ブロッカーを無効にしてください。

タイトルとURLをコピーしました