Search Unity

Question The type or namespace name 'IUnityAdsListener' could not be found (are you missing a using directive

Discussion in 'Unity DevOps General Discussion' started by egybeckham100, Jun 1, 2023.

  1. egybeckham100

    egybeckham100

    Joined:
    Jan 10, 2023
    Posts:
    2
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.Advertisements;
    5.  
    6. // Example script showing how to invoke the Google Mobile Ads Unity plugin.
    7. public class Adcontrol : MonoBehaviour, IUnityAdsListener
    8. {
    9.     public bool forcoin;
    10.     public static Adcontrol instace;
    11.     public string GameId = "3988497";
    12.     private bool testmode = false;
    13.     public string videoAd = "video";
    14.     public string banner = "banner";
    15.     public string reward = "rewardedVideo";
    16.     private void Awake()
    17.     {
    18.         instace = this;
    19.     }
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.  
    24.         Advertisement.Initialize(GameId, testmode);
    25.  
    26.         Advertisement.AddListener(this);
    27.         showBanner();
    28.  
    29.  
    30.     }
    31.  
    32.  
    33.     public void showBanner()
    34.     {
    35.  
    36.         StartCoroutine(ShowBannerWhenInitialized());
    37.         Advertisement.Banner.SetPosition(BannerPosition.TOP_CENTER);
    38.     }
    39.  
    40.     public void showAd()
    41.     {
    42.         if (!Advertisement.IsReady(videoAd)) { return; }
    43.         Advertisement.Show();
    44.     }
    45.  
    46.  
    47.     IEnumerator ShowBannerWhenInitialized()
    48.     {
    49.         while (!Advertisement.isInitialized)
    50.         {
    51.             yield return new WaitForSeconds(0.5f);
    52.         }
    53.         Advertisement.Banner.Show(banner);
    54.     }
    55.  
    56.     public void hideBanner()
    57.     {
    58.         Advertisement.Banner.Hide();
    59.     }
    60.  
    61.  
    62.     public void ShowRewardedVideo()
    63.     {
    64.         // Check if UnityAds ready before calling Show method:
    65.         if (Advertisement.IsReady(reward))
    66.         {
    67.             Advertisement.Show(reward);
    68.         }
    69.         else
    70.         {
    71.             Debug.Log("Rewarded video is not ready at the moment! Please try again later!");
    72.         }
    73.     }
    74.  
    75.     // Implement IUnityAdsListener interface methods:
    76.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    77.     {
    78.         // Define conditional logic for each ad completion status:
    79.         if (showResult == ShowResult.Finished)
    80.         {
    81.             AdManager.instance.give50points();
    82.             AdManager.instance.forcoin = false;
    83.            
    84.             // Reward the user for watching the ad to completion.
    85.             //  GameManager.instance.rewardUser();
    86.         }
    87.         else if (showResult == ShowResult.Skipped)
    88.         {
    89.             AdManager.instance.forcoin = false;
    90.             // Do not reward the user for skipping the ad.
    91.  
    92.         }
    93.         else if (showResult == ShowResult.Failed)
    94.         {
    95.             AdManager.instance.forcoin = false;
    96.             Debug.LogWarning("The ad did not finish due to an error.");
    97.         }
    98.     }
    99.  
    100.     public void OnUnityAdsReady(string placementId)
    101.     {
    102.         // If the ready Placement is rewarded, show the ad:
    103.         if (placementId == reward)
    104.         {
    105.             // Optional actions to take when the placement becomes ready(For example, enable the rewarded ads button)
    106.         }
    107.     }
    108.  
    109.     public void OnUnityAdsDidError(string message)
    110.     {
    111.         // Log the error.
    112.     }
    113.  
    114.     public void OnUnityAdsDidStart(string placementId)
    115.     {
    116.         // Optional actions to take when the end-users triggers an ad.
    117.     }
    118.  
    119.     // When the object that subscribes to ad events is destroyed, remove the listener:
    120.     public void OnDestroy()
    121.     {
    122.         Advertisement.RemoveListener(this);
    123.     }
    124. }