Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Updated to Untiy 5.2, having issues with Ads Integration

Discussion in 'Unity Ads & User Acquisition' started by Reymus, Oct 20, 2015.

  1. Reymus

    Reymus

    Joined:
    Apr 27, 2015
    Posts:
    44
    Hi all.

    Upon updating to Unity 5.2, I started getting errors regarding UnityAds. I found out about the UnityAds built in, and removed my Asset Store copy of the UnityAds code. That much, I figured out.

    I then enabled the Ads service in the Unity Services panel, and it gave me GameIDs for both Android and iOS. It started by telling me that my project cannot yet use the new UnityAds Dashboard, but that seems to have stopped. Now, I'm receiving errors though.

    Assets/Scripts/UnityAdsHelper.cs(92,77): error CS0117: `UnityEngine.Advertisements.Advertisement.DebugLevel' does not contain a definition for `NONE'

    Assets/Scripts/UnityAdsHelper.cs(94,99): error CS0117: `UnityEngine.Advertisements.Advertisement.DebugLevel' does not contain a definition for `DEBUG'

    Assets/Scripts/UnityAdsHelper.cs(137,36): error CS0117: `UnityEngine.Advertisements.Advertisement' does not contain a definition for `isReady'

    The above are a few of the errors I'm receiving.

    Here is my code for calling UnityAds:

    Code (CSharp):
    1. // UnityAdsHelper.cs - Written for Unity Ads Asset Store v1.0.4 (SDK 1.3.10)
    2. //  by Nikkolai Davenport <nikkolai@unity3d.com>
    3. //
    4. // Setup Instructions:
    5. // 1. Attach this script to a new game object.
    6. // 2. Enter game IDs into the fields provided.
    7. // 3. Enable Development Build in Build Settings to
    8. //     enable test mode and show SDK debug levels.
    9. //
    10. // Usage Guide:
    11. //  Write a script and call UnityAdsHelper.ShowAd() to show an ad.
    12. //  Customize the HandleShowResults method to perform actions based
    13. //  on whether an ad was succesfully shown or not.
    14. //
    15. // Notes:
    16. //  - Game IDs by platform are required to initialize Unity Ads.
    17. //  - Test game IDs are optional. If not set while in test mode,
    18. //     test game IDs will default to platform game IDs.
    19. //  - The various debug levels and test mode are only used when
    20. //     Development Build is enabled in Build Settings.
    21. //  - Test mode can be disabled while Development Build is set
    22. //     by checking the option to disable it in the inspector.
    23.  
    24. using UnityEngine;
    25. using System.Collections;
    26. #if UNITY_IOS || UNITY_ANDROID
    27. using UnityEngine.Advertisements;
    28. #endif
    29.  
    30. public class UnityAdsHelper : MonoBehaviour
    31. {
    32.     [System.Serializable]
    33.     public struct GameInfo
    34.     {
    35.         [SerializeField]
    36.         private string _gameID;
    37.         [SerializeField]
    38.         private string _testGameID;
    39.        
    40.         public string GetGameID ()
    41.         {
    42.             return Debug.isDebugBuild && !string.IsNullOrEmpty(_testGameID) ? _testGameID : _gameID;
    43.         }
    44.     }
    45.     public GameInfo iOS;
    46.     public GameInfo android;
    47.    
    48.     // Development Build must be enabled in Build Settings
    49.     //  in order to use test mode and to show debug levels.
    50.     public bool disableTestMode;
    51.     public bool showInfoLogs;
    52.     public bool showDebugLogs;
    53.     public bool showWarningLogs = true;
    54.     public bool showErrorLogs = true;
    55.  
    56.  
    57.     private static GameController gameController;
    58.  
    59.  
    60.     public void Start(){
    61.         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    62.         if (gameControllerObject != null)
    63.         {
    64.             gameController = gameControllerObject.GetComponent <GameController>();
    65.         }
    66.         if (gameController == null)
    67.         {
    68.             Debug.Log ("Cannot find 'GameController' script");
    69.         }
    70.     }
    71.  
    72.     protected void Awake()
    73.     {
    74.  
    75.  
    76.  
    77.         #if UNITY_IOS || UNITY_ANDROID
    78.         string gameID = null;
    79.        
    80.         #if UNITY_IOS
    81.         gameID = iOS.GetGameID();
    82.         #elif UNITY_ANDROID
    83.         gameID = android.GetGameID();
    84.         #endif
    85.        
    86.         if (string.IsNullOrEmpty(gameID))
    87.         {
    88.             Debug.LogError("A valid game ID is required to initialize Unity Ads.");
    89.         }
    90.         else
    91.         {
    92.             Advertisement.debugLevel = Advertisement.DebugLevel.NONE;  
    93.             if (showInfoLogs) Advertisement.debugLevel    |= Advertisement.DebugLevel.INFO;
    94.             if (showDebugLogs) Advertisement.debugLevel   |= Advertisement.DebugLevel.DEBUG;
    95.             if (showWarningLogs) Advertisement.debugLevel |= Advertisement.DebugLevel.WARNING;
    96.             if (showErrorLogs) Advertisement.debugLevel   |= Advertisement.DebugLevel.ERROR;
    97.            
    98.             bool enableTestMode = Debug.isDebugBuild && !disableTestMode;
    99.             Debug.Log(string.Format("Initializing Unity Ads for game ID {0} with test mode {1}...",
    100.                                     gameID, enableTestMode ? "enabled" : "disabled"));
    101.            
    102.             Advertisement.Initialize(gameID,enableTestMode);
    103.         }
    104.         #else
    105.         Debug.LogWarning("Unity Ads is not supported on the current build platform.");
    106.         #endif
    107.     }
    108.    
    109.     public static bool isInitialized { get {
    110.             #if UNITY_IOS || UNITY_ANDROID
    111.             return Advertisement.isInitialized;
    112.             #else
    113.             return false;
    114.             #endif
    115.         }}
    116.    
    117.     public static bool isReady (string zone = null)
    118.     {
    119.         #if UNITY_IOS || UNITY_ANDROID
    120.         if (string.IsNullOrEmpty(zone)) zone = null;
    121.         return Advertisement.isReady(zone);
    122.         #else
    123.         return false;
    124.         #endif
    125.     }
    126.  
    127.     public void ShowTestAd()
    128.     {
    129.         ShowAd();
    130.     }
    131.  
    132.     public static bool ShowAd (string zone = "rewardedVideoZone", bool pauseGameDuringAd = true)
    133.     {
    134.         #if UNITY_IOS || UNITY_ANDROID
    135.         if (string.IsNullOrEmpty(zone)) zone = null;
    136.        
    137.         if (!Advertisement.isReady(zone))
    138.         {
    139.             Debug.LogWarning(string.Format("Unable to show ad. The ad placement zone ($0) is not ready.",
    140.                                            zone == null ? "default" : zone));
    141.             return false;
    142.         }
    143.        
    144.         ShowOptions options = new ShowOptions();
    145.         options.pause = pauseGameDuringAd;
    146.         options.resultCallback = HandleShowResult;
    147.        
    148.         Advertisement.Show(zone,options);
    149.        
    150.         return true;
    151.         #else
    152.         Debug.LogError("Failed to show ad. Unity Ads is not supported on the current build platform.");
    153.         return false;
    154.         #endif
    155.     }
    156.    
    157.     #if UNITY_IOS || UNITY_ANDROID
    158.     private static void HandleShowResult (ShowResult result)
    159.     {
    160.         switch (result)
    161.         {
    162.         case ShowResult.Finished:
    163.             Debug.Log("The ad was successfully shown.");
    164.             gameController.SuccessfulAd();
    165.        
    166.             break;
    167.         case ShowResult.Skipped:
    168.             Debug.Log("The ad was skipped before reaching the end.");
    169.             break;
    170.         case ShowResult.Failed:
    171.             Debug.LogError("The ad failed to be shown.");
    172.             break;
    173.         }
    174.     }
    175.  
    176.     #endif
    177. }
     
  2. rasmus-unity

    rasmus-unity

    Unity Technologies

    Joined:
    Aug 15, 2014
    Posts:
    1,312
    unity-nikkolai likes this.
  3. Reymus

    Reymus

    Joined:
    Apr 27, 2015
    Posts:
    44
    Ah yes. I do have a version of the helper script from about 4-6 months ago. I'll attempt to install the new one and go from there
     
  4. Reymus

    Reymus

    Joined:
    Apr 27, 2015
    Posts:
    44
    Alright, so I updated to the new helper script, and went to compile for Android, and got the following error that seems to be related to the UnityAdsHelper install:

     
  5. Reymus

    Reymus

    Joined:
    Apr 27, 2015
    Posts:
    44
    And when trying to compile the iPhone version, xcode gives me the following error:

     
  6. infosekr

    infosekr

    Joined:
    Jul 12, 2013
    Posts:
    46
    Hi Reymus, your Android error seems to indicate that it's found the unity ads classes in two places ("already added: Lcom/applifier/impact/android/ApplifierImpact;"). Possibly you didn't delete the android plugin fully when switching over to the service? I've been able to get Android working ok, but I'm running into the same problem you see for iOS. Have you been able to find a solution for that one yet?
     
  7. jococo

    jococo

    Joined:
    Dec 15, 2012
    Posts:
    232
    Any solutions to this for iOS?