unity-game-engineadsamazon-advertising-api

How to implement Amazon ads in unity3d


i want to add amazon mobile ads to android and ios in unity3d game

and facing this problem in this code

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;
}

Solution

  • Register for a developer account on developer.amazon.com

    after registration go to main dashboard page and click on Apps & Services tab and create a new App

    Fill all the necessary info and copy your Application Key enter image description here

    download Amazon mobile ads sdk through this link and import ads plugin

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

    Then in Unity add this code to initialize your code for ads

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

    using UnityEngine;
    

    using System.Collections; using 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;
    
    
        }
    
    
    }
    

    you can also call an instance from this script to load ads and to display them like below

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

    My question is just not matter because i wrote this tutorial because everyone need help on this topic

    I Hope This Might Help