设置Chartboost插播广告出现的时间

本文关键字:时间 Chartboost 设置 | 更新日期: 2023-09-27 18:08:17

我需要设置我的Chartboost插播广告会在我想要的时候出现,因为它只是随机弹出(即使是在玩家玩游戏的时候)。我试着在UNITY3D c#中做到这一点。

我的CBScript.cs中的代码,基本上包含了从Chartboost加载广告所需的所有信息。我将这个脚本添加到我的层级视图中的空GameObject中。

CBScript.cs:

    using UnityEngine;
    using System.Collections;
    using System;
    using Chartboost;

    public class CBScript : MonoBehaviour {
        #if UNITY_ANDROID || UNITY_IPHONE
        public void Update() {
            #if UNITY_ANDROID
            // Handle the Android back button (only if impressions are set to not use activities)
            if (Input.GetKeyUp(KeyCode.Escape)) {
                // Check if Chartboost wants to respond to it
                if (CBBinding.onBackPressed()) {
                    // If so, return and ignore it
                    return;
                } else {
                    // Otherwise, handle it ourselves -- let's close the app
                    Application.Quit();
                }
            }
            #endif
        }
        void OnEnable() {
            // Initialize the Chartboost plugin
            #if UNITY_ANDROID
            // Replace these with your own Android app ID and signature from the Chartboost web portal
            CBBinding.init("ID", "Signature");
            #elif UNITY_IPHONE
            // Replace these with your own iOS app ID and signature from the Chartboost web porta
l
        CBBinding.init("ID", "Signature");
        #endif
    }
    void OnApplicationPause(bool paused) {
        #if UNITY_ANDROID
        // Manage Chartboost plugin lifecycle
        CBBinding.pause(paused);
        #endif
    }
    void OnDisable() {
        // Shut down the Chartboost plugin
        #if UNITY_ANDROID
        CBBinding.destroy();
        #endif
    }
    // UNITY_ANDROID || UNITY_IPHONE
    #endif
}

现在我将向你们展示,我是如何将这些代码实现到我的一个Game函数中。我真的希望我的《Chartboost》插播广告能够在玩家输掉游戏后出现。这是我的GUI脚本的片段,ShowEnd函数:

//Shows the end menu after a crash
    public void ShowEnd()
    {
        //Save the mission and activate the finish menu
        MissionManager.Instance.Save();
        EnableDisable(finishMenu, true);
        //Get the current coin and distance data
        int currentDist = (int)LevelGenerator.Instance.distance;
        int currentCoins = LevelManager.Instance.Coins();
        //Apply the data to the finish menu
        finishTexts[0].text = currentDist + "M";
        finishTexts[1].text = currentCoins.ToString();
        //If the current distance is greater than the best distance
        if (currentDist > SaveManager.GetBestDistance())
            //Set the current distance as the best distance
            SaveManager.SetBestDistance(currentDist);
        //Add the collected coins to the account
        SaveManager.SetCoins(SaveManager.GetCoins() + currentCoins);
        //Show the finish menu
        StartCoroutine(FadeScreen(0.4f, 0.7f));
        StartCoroutine(MoveMenu(finishMenu.transform, 0, -14.8f, 0.55f, false));
        OnGUI ();
    }

这是OnGUI();函数,它捕获并实际显示了插播广告:

void OnGUI(){
        #if UNITY_ANDROID
        // Disable user input for GUI when impressions are visible
        // This is only necessary on Android if we have disabled impression activities
        //   by having called CBBinding.init(ID, SIG, false), as that allows touch
        //   events to leak through Chartboost impressions
        GUI.enabled = !CBBinding.isImpressionVisible();
        #endif
        GUI.matrix = Matrix4x4.Scale(new Vector3(2, 2, 2));
        CBBinding.cacheInterstitial("Default");
        CBBinding.showInterstitial("Default");
    }

就像我说的,CB插播广告随机出现。我希望我的广告只在游戏结束后立即显示。我知道我现在做的每件事都是错的,但我不知道如何补救。谢谢你,谢谢你。

设置Chartboost插播广告出现的时间

我建议你应该在游戏启动时缓存插页广告,可能是在你的一个GameObjects的OnEnable函数中。我建议你在附加GUI的游戏对象的OnEnable中这样做。

void OnEnable() {
    CBBinding.cacheInterstitial("Default");
}

在OnGUI()函数中,你应该首先检查插页,然后调用showInterstitial()

void OnGUI(){
        #if UNITY_ANDROID
        // Disable user input for GUI when impressions are visible
        // This is only necessary on Android if we have disabled impression activities
        //   by having called CBBinding.init(ID, SIG, false), as that allows touch
        //   events to leak through Chartboost impressions
        GUI.enabled = !CBBinding.isImpressionVisible();
        #endif
        GUI.matrix = Matrix4x4.Scale(new Vector3(2, 2, 2));
        if (CBBinding.hasInterstitial("Default"))
            CBBinding.showInterstitial("Default");
    }