0

不知何故,我需要设置我的 Chartboost 插页式广告会在我想要的时候显示,因为它只是随机弹出(即使在游戏本身,当玩家正在玩时)。我正在尝试在 C# 的 UNITY3D 中执行此操作。

我的 CBScript.cs 中有代码,它基本上包含从 Chartboost 加载我的广告所需的所有信息。我将此脚本添加到层次结构视图中的空游戏对象中。

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
}

现在我将向你们展示我是如何将这段代码实现到我的一个游戏函数中的。我真的希望我的 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 插页式广告是随机出现的。我希望我的广告仅在游戏结束屏幕后立即显示。我知道我现在做的一切都错了,但我不知道如何解决它。谢谢并欣赏。

4

1 回答 1

2

我建议您应该在游戏启动时缓存插页式广告,可能在您的游戏对象之一的 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");
    }
于 2014-06-16T18:09:50.913 回答