Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question UnityAds stopped initializing after adding code about the Google Family policy update

Discussion in 'Unity Ads & User Acquisition' started by ab2700kd, Mar 22, 2022.

  1. ab2700kd

    ab2700kd

    Joined:
    May 12, 2016
    Posts:
    22
    Unity: 2021.2.5f1
    Unity Ads (Advertisement): 3.7.5 (August 02, 2021)


    UnityAds stopped initializing after adding code about the Google Family policy update
    (added in code OnInitializationComplete())
    Code (CSharp):
    1. MetaData metaData = new MetaData("privacy");
    2. metaData.Set("mode", "mixed"); // This is a mixed audience game.
    3. Advertisement.SetMetaData(metaData);
    4.  
    AdsInitializer.cs (Initializing of Unity Ads)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Advertisements;
    5.  
    6. public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
    7. {
    8.     [SerializeField] private string _androidGameId = "39XXXXX";
    9.     [SerializeField] private string _iOSGameId = "39XXXXX";
    10.     private string _gameId;
    11.     //
    12.     private static GameObject playerInstance;
    13.  
    14.     void Awake()
    15.     {
    16.         //
    17.             DontDestroyOnLoad(this);
    18.             if (playerInstance == null)
    19.             {
    20.                 playerInstance = gameObject;
    21.             }
    22.             else
    23.             {
    24.                 Destroy(gameObject);
    25.             }    
    26.         //
    27.         InitializeAds();
    28.     }
    29.  
    30.     public void InitializeAds()
    31.     {
    32.         _gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
    33.             ? _iOSGameId
    34.             : _androidGameId;
    35.  
    36.         //   Advertisement.Initialize(_gameId, _testMode);
    37.         Advertisement.Initialize(_gameId, false);
    38.      
    39.     }
    40.  
    41.     public void OnInitializationComplete()
    42.     {
    43.         Debug.Log("Unity Ads initialization complete.");
    44.         //
    45. #if UNITY_ANDROID
    46.         //If your game is directed at mixed audiences, use this code to inform Unity Ads when users should not receive personalized ads
    47.         //for Android
    48.         MetaData metaData = new MetaData("privacy");
    49.         metaData.Set("mode", "mixed"); // This is a mixed audience game.
    50.         Advertisement.SetMetaData(metaData);
    51. #endif
    52.  
    53.     }
    54.  
    55.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    56.     {
    57.         Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    58.     }
    59. }
    60.  

    On Project Settings/Ads/Test mode
    image002.jpg

    How to solve this troubles with Unity Ads?
     
    Last edited: Mar 22, 2022
  2. Hofmann

    Hofmann

    Joined:
    Sep 30, 2015
    Posts:
    25
    Hi,

    It works for me like this:

    Code (CSharp):
    1.  [SerializeField] string UnityAdsID;
    2.     [SerializeField] string UnityAdsID_IOS;
    3.     [SerializeField] bool _testMode = false;
    4.     [SerializeField] bool _enablePerPlacementMode = false;
    5.  
    6.     private string _gameId;
    7.  
    8.  
    9. void Awake()
    10.     {  
    11.        InitUnityAds();
    12.     }
    13.  
    14. private void InitUnityAds()
    15.     {
    16.         _gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
    17.             ? UnityAdsID_IOS
    18.             : UnityAdsID;
    19.  
    20.         if (Advertisement.isInitialized == false)
    21.         {
    22.             Advertisement.Initialize(_gameId, _testMode, _enablePerPlacementMode, this);
    23.         }
    24.     }
    25.  
    26. public void OnInitializationComplete()
    27.     {
    28.         Debug.Log("Unity Ads initialization complete.");
    29. #if UNITY_ANDROID
    30.         //If your game is directed at mixed audiences, use this code to inform Unity Ads when users should not receive personalized ads
    31.         //for Android
    32.         MetaData metaData = new MetaData("privacy");
    33.         metaData.Set("mode", "mixed"); // This is a mixed audience game.
    34.         Advertisement.SetMetaData(metaData);
    35. #endif
    36.     }
    37.  
    38.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    39.     {
    40.         Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    41.     }
     
  3. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    139

    Hello!
    Small question.

    Why didn't you add "nonbehavioral"?
    MetaData userMetaData = new MetaData("user");
    userMetaData.Set("nonbehavioral", "true");
    Advertisement.SetMetaData(userMetaData);

    And in another thread I read that the metadata needs to be added before the initialization.

    Thanks in advance
     
  4. ab2700kd

    ab2700kd

    Joined:
    May 12, 2016
    Posts:
    22
    In next step, I add the metadata prior to initialization....

    Code (CSharp):
    1. void Awake()
    2.     {          
    3.      #if UNITY_ANDROID
    4.         //If your game is directed at mixed audiences, use this code to inform Unity Ads when users should not receive personalized ads
    5.         //for Android
    6.         MetaData metaData = new MetaData("privacy");
    7.         metaData.Set("mode", "mixed"); // This is a mixed audience game.
    8.         Advertisement.SetMetaData(metaData);
    9.      #endif
    10.  
    11.         //
    12.         InitializeAds();
    13.     }  
    And Unity Ads is not initialized ...
    adb2.png
     
    denis_bogdanov likes this.
  5. Hofmann

    Hofmann

    Joined:
    Sep 30, 2015
    Posts:
    25
    Try copy / paste my code works for you?
     
  6. Hofmann

    Hofmann

    Joined:
    Sep 30, 2015
    Posts:
    25
    I'm also not entirely sure how to do it, but initialization works for me.
    https://forum.unity.com/threads/complicity-with-googles-new-family-child-policy-for-ads.1254915/
     
    denis_bogdanov likes this.
  7. ab2700kd

    ab2700kd

    Joined:
    May 12, 2016
    Posts:
    22
    I set in code

    Code (CSharp):
    1. void Awake()
    2.     {
    3.   //
    4. #if UNITY_ANDROID
    5.         //If your game is directed at mixed audiences, use this code to inform Unity Ads when users should not receive personalized ads
    6.         //for Android
    7.         /*
    8.         MetaData metaData = new MetaData("privacy");
    9.         metaData.Set("mode", "mixed"); // This is a mixed audience game.
    10.         Advertisement.SetMetaData(metaData);
    11.         */
    12.         MetaData userMetaData = new MetaData("user");
    13.         userMetaData.Set("nonbehavioral", "true");
    14.         Advertisement.SetMetaData(userMetaData);
    15. #endif
    16.  
    17.         //
    18.         InitializeAds();
    19.     }  

    Initialization is not worked

    adb3.png
     
  8. SamOYUnity3D

    SamOYUnity3D

    Unity Technologies

    Joined:
    May 12, 2019
    Posts:
    626
    It looks like the app is missing the initialize method, did you obfuscate the code or delete any files? Might be worth trying to re-import the Ads SDK or start a new project.
     
  9. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    139
    Hello!

    A quick question.

    If the flag "mixed" is set to true, then before the start of the game need to make a menu of age selection? To determine whether a child or an adult is playing? And after set the value for "nonbehavioral" to "true" or "false". I understand everything correctly?

    Thanks in advance
     
  10. ab2700kd

    ab2700kd

    Joined:
    May 12, 2016
    Posts:
    22
    Solved! It's bug of Unity Ads!... I deleted UnityAds package from project, clean Temp in Project, reload Unity,
    install Unity Ads again and It's worked!