private static string[] AdUnitInterstitialOrRewardedFile(AdUnitSettings unitSettings) => new string[] { "using System.Threading.Tasks;", "using UnityEngine;", "using UnityEngine.Advertisements;", "", "namespace Local.Ads", "{", " public class " + unitSettings.className + "AdUnit : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener", " {", "", " #region Singleton", "", " // This is the behaviour where all functions will be executed.", " private static " + unitSettings.className + "AdUnit _host;", " private static " + unitSettings.className + "AdUnit Host", "", " {", " get", " {", " if (_host == null)", " {", " // Create the host", " _host = AdsInitializer.Host.AddComponent<" + unitSettings.className + "AdUnit>();", " DontDestroyOnLoad(_host);", " }", "", " return _host;", " }", " }", "", "", " private static TaskCompletionSource<bool> tks; // The task of the ad", " public static bool IsReady { get; private set; } = false; // If add is initialized and loaded", "", "", "#if UNITY_EDITOR", " private static float lastTimeScale = 1;", " //private static bool lastBannerState = false;", "#endif", "", "", " // Load the ad", " public static void Load()", " {", " // Is no ready", " IsReady = false;", "", " if (!Env.UnityAdsSettings.IsSupported) return;", "", " if (!Advertisement.isInitialized)", " {", " if (Env.UnityAdsSettings.showLogs) Debug.LogError($" + quote + "" + unitSettings.className + "AdUnit.Load: Ads are not initialized" + quote + ");", " return;", " }", "", " if (Advertisement.isShowing)", " {", " if (Env.UnityAdsSettings.showLogs) Debug.LogError($" + quote + "" + unitSettings.className + "AdUnit.Load: An ad is showing, wait for it to finish" + quote + ");", " return;", " }", "", " // Add this component and Load", " Host.LoadAd();", "", " }", "", "", " // Show the ad", " public static Task<bool> Show()", " {", " // If a task exist, finish it", " if (tks != null)", " if (!tks.Task.IsCompleted)", " tks.SetException(new System.InvalidOperationException(" + quote + "A new add was called before this one ended" + quote + "));", "", " // Create a new Task", " tks = new TaskCompletionSource<bool>();", "", " // If is not suported", " if (!Env.UnityAdsSettings.IsSupported)", " {", " if (Env.UnityAdsSettings.InNoSuportedDevicesAdsMustSucced) tks.SetResult(true);", " else tks.SetException(new System.InvalidOperationException(" + quote + "This device is not compatible with ads" + quote + "));", "", " return tks.Task;", " }", "", " // If not ready , return", " if (!IsReady)", " {", " if (Env.UnityAdsSettings.showLogs) Debug.Log(" + quote + "" + unitSettings.className + "AdUnit: Ad is no Ready" + quote + ");", " tks.SetException(new System.InvalidOperationException(" + quote + "Add is no Ready" + quote + "));", " return tks.Task;", " }", "", " Host.ShowAd();", "", " return tks.Task;", "", " }", "", " #endregion Singleton", "", "", " #region MonoBehaviour", "", " private string adUnitId; // This will remain null for unsupported platforms.", "", "", " private void Awake()", " {", "#if UNITY_ANDROID", " adUnitId = Env.UnityAdsSettings.RewardedSettings.androidAdUnitId;", "#elif UNITY_IOS", " adUnitId = Env.UnityAdsSettings.RewardedAd.iOSAdUnitId;", "#endif", " }", "", "", " // Load content to the Ad Unit and add callbacks", " private void LoadAd()", " {", " // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).", " if (Env.UnityAdsSettings.showLogs) Debug.Log(" + quote + "" + unitSettings.className + "AdUnit.LoadAd: Loading Ad: " + quote + " + adUnitId);", " Advertisement.Load(adUnitId, this);", " }", "", "", " // Show the content and add callbacks ", " private void ShowAd()", " {", " // Note that if the ad content wasn't previously loaded, this method will fail", " if (Env.UnityAdsSettings.showLogs) Debug.Log(" + quote + "" + unitSettings.className + "AdUnit.ShowAd: Showing Ad: " + quote + " + adUnitId);", " Advertisement.Show(adUnitId, this);", " }", "", "", " #region Load Callbacks", "", " public void OnUnityAdsAdLoaded(string placementId)", " {", " if (!placementId.Equals(adUnitId)) return;", "", " // When is loaded, set is ready", " IsReady = true;", "", " if (Env.UnityAdsSettings.showLogs) Debug.Log($" + quote + "" + unitSettings.className + "AdUnit.OnUnityAdsAdLoaded: Ad {placementId} is Ready" + quote + ");", " }", "", " public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)", " {", " if (!placementId.Equals(adUnitId)) return;", "", " // When is loaded, set is ready", " IsReady = false;", "", " if (Env.UnityAdsSettings.showLogs) Debug.Log($" + quote + "" + unitSettings.className + "AdUnit.OnUnityAdsFailedToLoad: Error loading Ad Unit: {adUnitId} - {error.ToString()} - {message}" + quote + ");", " }", "", " #endregion Load Callbacks", "", "", " #region Show Callbacks", "", " public void OnUnityAdsShowClick(string placementId)", " {", " if (!placementId.Equals(adUnitId)) return;", "", " if (Env.UnityAdsSettings.showLogs) Debug.Log($" + quote + "" + unitSettings.className + "AdUnit.OnUnityAdsShowClick: On ad {placementId}" + quote + ");", " }", "", " public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)", " {", " if (!placementId.Equals(adUnitId)) return;", "", "", " if (showCompletionState == UnityAdsShowCompletionState.COMPLETED) tks.SetResult(true);", " else tks.SetException(new System.InvalidOperationException($" + quote + "OnUnityAdsShowComplete: {showCompletionState}" + quote + "));", "", " if (Env.UnityAdsSettings.showLogs) Debug.Log($" + quote + "" + unitSettings.className + "AdUnit.OnUnityAdsShowComplete: {placementId} , {showCompletionState}" + quote + ");", "", " // Load other add", " Load();", "", "#if UNITY_EDITOR", " // Resume in Editor, in final devices is not necesary", " Time.timeScale = lastTimeScale;", " //if (lastBannerState) Advertisement.Banner.();", "#endif", "", " }", "", " public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)", " {", " if (!placementId.Equals(adUnitId)) return;", "", " tks.SetException(new System.InvalidOperationException($" + quote + "OnUnityAdsShowFailure: {error}, {message}" + quote + "));", "", " if (Env.UnityAdsSettings.showLogs) Debug.Log($" + quote + "" + unitSettings.className + "AdUnit.OnUnityAdsShowFailure: Error showing Ad {adUnitId}: {error.ToString()} - {message}" + quote + ");", "", " // Load other add", " Load();", "", "#if UNITY_EDITOR", " // Resume in Editor, in final devices is not necesary", " Time.timeScale = lastTimeScale;", " //if (lastBannerState) Advertisement.Banner.Show();", "#endif", "", " }", "", " public void OnUnityAdsShowStart(string placementId)", " {", " if (!placementId.Equals(adUnitId)) return;", "", " if (Env.UnityAdsSettings.showLogs) Debug.Log($" + quote + "" + unitSettings.className + "AdUnit.OnUnityAdsShowStart: Ad {placementId} is starting" + quote + ");", "", "#if UNITY_EDITOR", " // Pause In editor, in final devices is not necesary", " lastTimeScale = Time.timeScale;", " Time.timeScale = 0;", "", " // Hide banner in editor, in final devices is not necesary", " //lastBannerState = AdsInitializer.bannerIsVisible;", " //Advertisement.Banner.Hide();", "#endif", "", " }", "", "", " #endregion Show Callbacks", "", "", " #endregion MonoBehaviour", "", " }", "}", };
private static string[] AdUnitBannerFile(AdUnitSettings unitSettings) => new string[] { "using UnityEngine;", "using UnityEngine.Advertisements;", "", "namespace Local.Ads", "{", " public class " + unitSettings.className + "AdUnit : MonoBehaviour", " {", "", " #region Singleton", "", " // This is the behaviour where all functions will be executed.", " private static " + unitSettings.className + "AdUnit _host;", " private static " + unitSettings.className + "AdUnit Host", "", " {", " get", " {", " if (_host == null)", " {", " // Create the host", " _host = AdsInitializer.Host.AddComponent<" + unitSettings.className + "AdUnit>();", " DontDestroyOnLoad(_host);", " }", "", " return _host;", " }", " }", "", " public static bool IsReady { get; private set; } = false; // If add is initialized and loaded", "", "", " // Load the banner", " public static void Load()", " {", " // Is no ready", " IsReady = false;", "", " if (!Env.UnityAdsSettings.IsSupported) return;", "", " if (!Advertisement.isInitialized)", " {", " if (Env.UnityAdsSettings.showLogs) Debug.LogError($" + quote + "" + unitSettings.className + "AdUnit.Load: Ads are not initialized" + quote + ");", " return;", " }", "", " // Add this component and Load", " Host.LoadAd();", "", " }", "", "", " // Show the banner", " public static void Show()", " {", " if (!Env.UnityAdsSettings.IsSupported) return;", "", " // If not ready , return", " if (!IsReady)", " {", " if (Env.UnityAdsSettings.showLogs) Debug.Log(" + quote + "" + unitSettings.className + "AdUnit: Ad is no Ready" + quote + ");", " return;", " }", "", " Host.ShowAd();", "", " }", "", "", " // Hide the banner", " public static void Hide()", " {", " if (!Env.UnityAdsSettings.IsSupported) return;", "", " Advertisement.Banner.Hide();", " }", "", "", " // Change banner position", " public static void ChangePosition(BannerPosition position)", " {", " if (!Env.UnityAdsSettings.IsSupported) return;", "", " Advertisement.Banner.SetPosition(position);", " }", "", " #endregion Singleton", "", "", " #region MonoBehaviour", "", " private BannerPosition bannerPosition; // Default banner position", " private string adUnitId; // This will remain null for unsupported platforms.", "", " private void Awake()", " {", " if (!Env.UnityAdsSettings.IsSupported) return;", "", "#if UNITY_ANDROID", " adUnitId = Env.UnityAdsSettings.BannerSettings.androidAdUnitId;", "#elif UNITY_IOS", " adUnitId = Env.UnityAdsSettings." + unitSettings.className + "AdUnit.iOSAdUnitId;", "#endif", "", " // Set the banner default Position", " bannerPosition = Env.UnityAdsSettings.BannerSettings.possitionIfIsBanner;", " }", "", "", " // Load content to the Ad Unit and add callbacks", " private void LoadAd()", " {", " // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).", " if (Env.UnityAdsSettings.showLogs) Debug.Log(" + quote + "" + unitSettings.className + "AdUnit.LoadAd: Loading Ad: " + quote + " + adUnitId);", "", " // Load the banner with custom props", " Advertisement.Banner.SetPosition(bannerPosition);", " Advertisement.Banner.Load(adUnitId, new BannerLoadOptions", " {", " loadCallback = OnBannerLoaded,", " errorCallback = OnBannerError,", " });", " }", "", "", " // Show the content and add callbacks ", " private void ShowAd()", " {", " // Note that if the ad content wasn't previously loaded, this method will fail", " if (Env.UnityAdsSettings.showLogs) Debug.Log(" + quote + "" + unitSettings.className + "AdUnit.ShowAd: Showing Ad: " + quote + " + adUnitId);", "", " Advertisement.Banner.Show(adUnitId, new BannerOptions", " {", " clickCallback = OnBannerClicked,", " hideCallback = OnBannerHidden,", " showCallback = OnBannerShown,", " });", " }", "", "", " #region Load Callbacks", "", " private void OnBannerError(string message)", " {", " IsReady = false;", "", " if (Env.UnityAdsSettings.showLogs) Debug.Log($" + quote + "" + unitSettings.className + "AdUnit.OnBannerError: Error loading banner: {adUnitId}" + quote + ");", " }", "", " private void OnBannerLoaded()", " {", " IsReady = true;", "", " if (Env.UnityAdsSettings.showLogs) Debug.Log($" + quote + "" + unitSettings.className + "AdUnit.OnBannerLoaded: Loaded Banner: {adUnitId}" + quote + ");", " }", "", " #endregion Load Callbacks", "", "", " #region Show Callbacks", "", " private void OnBannerShown()", " {", " if (Env.UnityAdsSettings.showLogs) Debug.Log($" + quote + "" + unitSettings.className + "AdUnit.OnBannerShown: Shown Banner: {adUnitId}" + quote + ");", "", "", "#if UNITY_EDITOR", " AdsInitializer.bannerIsVisible = true;", "#endif", "", " }", "", " private void OnBannerHidden()", " {", " if (Env.UnityAdsSettings.showLogs) Debug.Log($" + quote + "" + unitSettings.className + "AdUnit.OnBannerHidden: Hidden Banner: {adUnitId}" + quote + ");", "", "#if UNITY_EDITOR", " AdsInitializer.bannerIsVisible = false;", "#endif", "", " }", "", " private void OnBannerClicked()", " {", " if (Env.UnityAdsSettings.showLogs) Debug.Log($" + quote + "" + unitSettings.className + "AdUnit.OnBannerClicked: Clicked Banner: {adUnitId}" + quote + ");", " }", "", "", " #endregion Show Callbacks", "", "", " #endregion MonoBehaviour", "", " }", "}", };
private static string AdUnitFileName(AdUnitSettings adUnit) => $"{adUnit.className}AdUnit.cs";