Search Unity

How to destroy BANNER-ADs on scene change in unity

Discussion in 'Unity Ads & User Acquisition' 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. public void Dispose()
    26. {
    27. Dispose(true);
    28. GC.SuppressFinalize(this);
    29. }
    30.  
    31. protected virtual void Dispose(bool disposing)
    32. {
    33. if (mDisposed)
    34. return;
    35.  
    36. if (disposing)
    37. {
    38. // Free any other managed objects here.
    39. }
    40. sta_removeBannerObject(mGameObject.name);
    41. mDisposed = true;
    42. }
    43.  
    44. ~BannerAdiOS()
    45. {
    46. Dispose(false);
    47. }
    48.  
    49. public override void PreLoad()
    50. {
    51. sta_preloadBanner(mGameObject.name);
    52. }
    53.  
    54. public override void ShowInPosition(BannerPosition position, string tag)
    55. {
    56. if (tag == null)
    57. {
    58. sta_addBanner(mGameObject.name, (int)position);
    59. return;
    60. }
    61. sta_addBannerWithTag(mGameObject.name, (int)position, tag);
    62. }
    63.  
    64. public override void Hide()
    65. {
    66. sta_hideBanner(mGameObject.name);
    67. }
    68.  
    69. public override bool IsShownInPosition(BannerPosition position)
    70. {
    71. return sta_isShownInPosition(mGameObject.name, (int)position);
    72. }
    73.  
    74. class ListenerComponent : MonoBehaviour
    75. {
    76. public BannerAdiOS Parent { get; set; }
    77.  
    78. void OnDidShowBanner()
    79. {
    80. Parent.OnRaiseBannerShown();
    81. }
    82.  
    83. void OnFailedLoadBanner(string error)
    84. {
    85. Parent.OnRaiseBannerLoadingFailed(error);
    86. }
    87.  
    88. void OnDidClickBanner()
    89. {
    90. Parent.OnRaiseBannerClicked();
    91. }
    92. }
    93.  
    94. [DllImport("__Internal")]
    95. static extern void sta_addBanner(string gameObjectName, int position);
    96.  
    97. [DllImport("__Internal")]
    98. static extern void sta_addBannerWithTag(string gameObjectName, int position, string tag);
    99.  
    100. [DllImport("__Internal")]
    101. static extern void sta_preloadBanner(string gameObjectName);
    102.  
    103. [DllImport("__Internal")]
    104. static extern void sta_hideBanner(string gameObjectName);
    105.  
    106. [DllImport("__Internal")]
    107. static extern bool sta_isShownInPosition(string gameObjectName, int position);
    108.  
    109. [DllImport("__Internal")]
    110. static extern void sta_removeBannerObject(string gameObjectName);
    111.  
    112.  
    113. }
    114. }
    115.  
    116. #endif
     
  2. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519