Search Unity

How to destroy BANNER-ADs on scene change in unity

Discussion in 'Editor & General Support' started by SAditya, Jun 24, 2019.

  1. SAditya

    SAditya

    Joined:
    May 21, 2019
    Posts:
    19
    Hey guys I integrated Startapp (that provides ads) in my unity game , when I switched the platform to iOs and deployed the game in my iphone (via XCODE) , the Banner-Ads are not getting destroyed on scene change i.e. I want my Banner_ad in only Scene 1 , but the Banner_Add doesn't hide or destroy when I switch to another Scene 2.
    So I want to stop Banner_Ads from being created when I switch to next Scene.
    Below is the BANNER-ADS code , any suggestion is appreciated.
    Thanks !!





    Code (CSharp):
    1. #if UNITY_IOS
    2.  
    3. using UnityEngine;
    4. using System.Runtime.InteropServices;
    5. using System;
    6.  
    7. namespace StartApp
    8. {
    9.     public class BannerAdiOS : BannerAd, IDisposable
    10.     {
    11.         bool mDisposed;
    12.         readonly GameObject mGameObject = new GameObject();
    13.  
    14.         static BannerAdiOS()
    15.         {
    16.             AdSdkiOS.ImplInstance.Setup();
    17.         }
    18.  
    19.         public BannerAdiOS()
    20.         {
    21.             mGameObject.name = mGameObject.GetInstanceID().ToString();
    22.             mGameObject.AddComponent<ListenerComponent>().Parent = this;
    23.            
    24.         }
    25.  
    26.         public void Dispose()
    27.         {
    28.             Dispose(true);
    29.             GC.SuppressFinalize(this);
    30.         }
    31.  
    32.         protected virtual void Dispose(bool disposing)
    33.         {
    34.             if (mDisposed)
    35.                 return;
    36.  
    37.             if (disposing)
    38.             {
    39.                 // Free any other managed objects here.
    40.             }
    41.             sta_removeBannerObject(mGameObject.name);
    42.             mDisposed = true;
    43.         }
    44.  
    45.         ~BannerAdiOS()
    46.         {
    47.             Dispose(false);
    48.         }
    49.  
    50.         public override void PreLoad()
    51.         {
    52.             sta_preloadBanner(mGameObject.name);
    53.         }
    54.  
    55.         public override void ShowInPosition(BannerPosition position, string tag)
    56.         {
    57.             if (tag == null)
    58.             {
    59.                 sta_addBanner(mGameObject.name, (int)position);
    60.                 return;
    61.             }
    62.             sta_addBannerWithTag(mGameObject.name, (int)position, tag);
    63.         }
    64.  
    65.         public override void Hide()
    66.         {
    67.             sta_hideBanner(mGameObject.name);
    68.         }
    69.  
    70.         public override bool IsShownInPosition(BannerPosition position)
    71.         {
    72.             return sta_isShownInPosition(mGameObject.name, (int)position);
    73.         }
    74.  
    75.         class ListenerComponent : MonoBehaviour
    76.         {
    77.             public BannerAdiOS Parent { get; set; }
    78.  
    79.             void OnDidShowBanner()
    80.             {
    81.                 Parent.OnRaiseBannerShown();
    82.             }
    83.  
    84.             void OnFailedLoadBanner(string error)
    85.             {
    86.                 Parent.OnRaiseBannerLoadingFailed(error);
    87.             }
    88.  
    89.             void OnDidClickBanner()
    90.             {
    91.                 Parent.OnRaiseBannerClicked();
    92.             }
    93.         }
    94.  
    95.         [DllImport("__Internal")]
    96.         static extern void sta_addBanner(string gameObjectName, int position);
    97.  
    98.         [DllImport("__Internal")]
    99.         static extern void sta_addBannerWithTag(string gameObjectName, int position, string tag);
    100.  
    101.         [DllImport("__Internal")]
    102.         static extern void sta_preloadBanner(string gameObjectName);
    103.  
    104.         [DllImport("__Internal")]
    105.         static extern void sta_hideBanner(string gameObjectName);
    106.  
    107.         [DllImport("__Internal")]
    108.         static extern bool sta_isShownInPosition(string gameObjectName, int position);
    109.  
    110.         [DllImport("__Internal")]
    111.         static extern void sta_removeBannerObject(string gameObjectName);
    112.  
    113.  
    114.     }
    115. }
    116.  
    117. #endif