I have a flashcard app that is for Android. It has Unity Ads.
I also want to build it on Mac.
However, the Mac build attempt produces:
Assets/Scripts/UnityAdsButton.cs(13,25): error CS0103: The name `Advertisement’ does not exist in the current context
Can you help me get Unity Ads code that compiles across all platforms even if there are no actual ads on Mac?
Thanks. Have a blessed day.
For any part of your code that references the Advertisement class, or any part of the Unity Ads API, you should encapsulate it within a conditional compile statement. If the condition is not true, the code will not be compiled.
In the case of Unity Ads, if Ads is enabled in the Services window and is compatible with the current build platform, then the UNITY_ADS symbol will be defined. Otherwise the code is ignored.
For example:
using UnityEngine;
using System.Collections;
#if UNITY_ADS
using UnityEngine.Advertisements;
#endif
public class UnityAdsSimpleExample : MonoBehaviour
{
public string iosGameId;
public string androidGameId;
public bool enableTestMode;
IEnumerator Start ()
{
#if UNITY_ADS
if (Advertisement.isSupported)
{
string gameId = null;
#if UNITY_IOS
gameId = iosGameId;
#elif UNITY_ANDROID
gameId = androidGameId;
#endif
Advertisement.Initialize(gameId,enableTestMode);
}
yield return new WaitUntil(
() => Advertisement.isInitialized && Advertisement.IsReady()
);
Advertisement.Show();
#endif
yield break;
}
}
Note: This example initializes Unity Ads on Start. This is only necessary if you have set the value of AdvertisementSettings.initializeOnStartup to false.