如何在unity3d中实现亚马逊广告

How to implement Amazon ads in unity3d

我想在 unity3d 游戏中向 android 和 ios 添加亚马逊移动广告

并在这段代码中遇到这个问题

public void createIad(){
    CreateInterstitialAd ();
}

Ad CreateInterstitialAd(){

IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;
AdInterstitial = mobileAds.CreateInterstitialAd();
string adType = AdInterstitial.AdType.ToString();
long identifier = AdInterstitial.Identifier;
    /*
    LoadingStarted LSObject = mobileAds.LoadInterstitialAd();
    bool loadingStarted = LSObject.BooleanValue;
    */
    return AdInterstitial;
}

在 developer.amazon.com

上注册开发者帐户

注册后转到主仪表板页面并单击“应用程序和服务”选项卡 并创建一个新应用

填写所有必要信息并复制您的应用程序密钥

通过此link下载亚马逊移动广告sdk并导入广告插件

https://developer.amazon.com/public/resources/development-tools/sdk-thank-you?product=apps_games_services_unity

然后在 Unity 中添加此代码以初始化您的广告代码

"after placing below code in your script don't forget to paste you app key inside this script app key parameter"

using UnityEngine;

使用System.Collections; 使用 com.amazon.mas.cpt.ads;

public class AdTest : MonoBehaviour {

    public string androidKey;

    public string iosKey;

    private IAmazonMobileAds mobileAds;

    private static AdTest instance2;

    public static AdTest Instance

    {
        get { return instance2; }
    }
    void Awake() {

        DontDestroyOnLoad (transform.gameObject);
        // If no Player ever existed, we are it.
        if (instance2 == null)
            instance2 = this;
        // If one already exist, it's because it came from another level.
        else if (instance2 != this) {
            Destroy (gameObject);
            return;
        }


        //CloseFloatingAd ();
        //DisplayInterstitial ();
    }


    // Use this for initialization
    void Start () {
        SetAppKey ();

        //Delete this function before releasing your app
        EnableTesting ();
        //

        //DisplayInterstitial ();

    }

    // Update is called once per frame
    void Update () {

    }


    public void SetAppKey(){

        // Create a reference to the mobile ads instance

        mobileAds = AmazonMobileAdsImpl.Instance;



        // Create new key

        ApplicationKey key = new ApplicationKey ();

        //zum Testen
        //key.StringValue = androidKey;

        // Set key based on OS

        #if UNITY_ANDROID

        key.StringValue = androidKey;

        #elif UNITY_IPHONE

        key.StringValue = iosKey;

        #endif



        // Pass in the key

        mobileAds.SetApplicationKey (key);

    }

    public void EnableTesting(){

        //Create should enable instance

        ShouldEnable enable = new ShouldEnable ();

        enable.BooleanValue = true;

        mobileAds.EnableTesting (enable);

        mobileAds.EnableLogging (enable);   

    }



    Ad AdObject;
    /*
    public Ad CreateFloatingBannerAd(Placement input){

        IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;
        Placement placement = new Placement ();
        placement.Dock = Dock.BOTTOM;

        placement.HorizontalAlign = HorizontalAlign.CENTER;

        placement.AdFit = AdFit.FIT_AD_SIZE;

        Ad response = mobileAds.CreateFloatingBannerAd (placement);

        string adType = response.AdType.ToString ();
        long identifier = response.Identifier;

    }*/


    public void CloseFloatingAd(){

        if (AdTest.Instance.AdObject != null) {
            IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;
            mobileAds.CloseFloatingBannerAd (AdObject);
            CreateFloatingBannerAd ();

        }
    }

    LoadingStarted LoadInterstitialAd(){
        IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;
        LoadingStarted response = mobileAds.LoadInterstitialAd ();
        bool loadingStarted = response.BooleanValue;
        return response;
    }

    public void createBanner(){
        CreateFloatingBannerAd ();
    }


    Ad CreateFloatingBannerAd(){

        // Configure placement for the ad

        Placement placement = new Placement ();

        //placement.Dock = Dock.TOP;
        placement.Dock = Dock.BOTTOM;

        placement.HorizontalAlign = HorizontalAlign.CENTER;

        placement.AdFit = AdFit.FIT_AD_SIZE;

        AdObject = mobileAds.CreateFloatingBannerAd(placement);
        return AdObject;
    }

    public void DisplayFloatingAd(){

        // Configure placement for the ad

        Placement placement = new Placement ();

        //placement.Dock = Dock.TOP;
        placement.Dock = Dock.BOTTOM;

        placement.HorizontalAlign = HorizontalAlign.CENTER;

        placement.AdFit = AdFit.FIT_AD_SIZE;



        // This method returns an Ad object, which you must save and keep track of

        AdObject = mobileAds.CreateFloatingBannerAd(placement);



        // This method returns a LoadingStarted object

        LoadingStarted newResponse = mobileAds.LoadAndShowFloatingBannerAd(AdObject);

    }

    Ad AdInterstitial;
    AdShown AdSObject;

    public void createIad(){
        CreateInterstitialAd ();
    }

    Ad CreateInterstitialAd(){

        Debug.Log ("CreateInterstitialAd()+++++++");
    IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;

    AdInterstitial = mobileAds.CreateInterstitialAd();

    string adType = AdInterstitial.AdType.ToString();
    long identifier = AdInterstitial.Identifier;
        /*
        LoadingStarted LSObject = mobileAds.LoadInterstitialAd();
        bool loadingStarted = LSObject.BooleanValue;
        */
        return AdInterstitial;
    }



    public void DisplayInterstitial(){

        CreateInterstitialAd ();

        IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;

        LoadingStarted LSObject = mobileAds.LoadInterstitialAd();
        bool loadingStarted = LSObject.BooleanValue;


            AdSObject = mobileAds.ShowInterstitialAd ();
            //bool adSwohn = AdSObject.BooleanValue;


    }


}

您也可以从此脚本调用一个实例来加载广告并显示它们 如下图

public void DisplayBanner () {
    AdTest.Instance.DisplayFloatingAd ();
}
public void CreateBanner () {
    AdTest.Instance.createBanner();
}
public void DisplayInterstitial () {
    AdTest.Instance.DisplayInterstitial();
}
public void CreateInterstitial () {
    AdTest.Instance.createIad();
}

我的问题无关紧要,因为我写这篇教程是因为每个人都需要关于这个主题的帮助

希望对您有所帮助