//
// FairBid Unity SDK
//
// Copyright (c) 2019 Fyber. All rights reserved.
//
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
namespace Fyber
{
///
/// Class responsible for managing Banner ads.
///
public class Banner : MonoBehaviour
{
private static BannerListener bannerListener;
private static Banner _instance;
///
/// Displays a banner identified by the placement name
///
/// The placement identifier for the particular ad.
public static void Show(string placementId)
{
Banner.Show(placementId, null);
}
///
/// Displays a banner identified by the placement name
///
/// The placement identifier for the particular ad.
/// The to be used for this display call
public static void Show(string placementId, BannerOptions showOptions)
{
if (showOptions == null)
{
showOptions = new BannerOptions();
}
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
BannerAndroid.Show(placementId, showOptions);
#elif UNITY_IOS
BannerIOS.Show(placementId, showOptions);
#endif
#else
string message = "Call received to show an Banner, but the SDK does not function in the editor. You must use a device/emulator to fetch/show ads.";
UnityEngine.Debug.LogWarning(message);
_instance.StartCoroutine(InvokeCallbackNextFrame(CallbackInfo.ForError(placementId, message)));
#endif
}
///
/// Destroys the banner identified by the placement name, if any
///
/// The placement identifier for the particular ad.
public static void Destroy(string placementId)
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
BannerAndroid.Destroy(placementId);
#elif UNITY_IOS
BannerIOS.Destroy(placementId);
#endif
#else
#endif
}
///
/// Refreshes the banner identified by the placement name, if any
///
/// The placement identifier for the particular ad.
public static void Refresh(string placementId)
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
BannerAndroid.Refresh(placementId);
#elif UNITY_IOS
BannerIOS.Refresh(placementId);
#endif
#else
#endif
}
///
/// Hides the banner identified by the placement name, if any
///
/// The placement identifier for the particular ad.
public static void Hide(string placementId)
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
BannerAndroid.Hide(placementId);
#elif UNITY_IOS
BannerIOS.Hide(placementId);
#endif
#else
#endif
}
///
/// Sets the that will be notified about Banner events
///
public static void SetBannerListener(BannerListener listener)
{
Banner.bannerListener = listener;
}
///
/// The amount of Banner impressions for this session
///
/// the amount of impressions
public static int GetImpressionDepth()
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
#if UNITY_ANDROID
return BannerAndroid.GetImpressionDepth();
#elif UNITY_IOS
return BannerIOS.GetImpressionDepth();
#endif
#else
return 0;
#endif
}
#region Internal methods
public static void InitReceiver()
{
if (_instance == null)
{
GameObject receiverObject = new GameObject("FairBidBanner");
DontDestroyOnLoad(receiverObject);
_instance = receiverObject.AddComponent();
}
}
public void InvokeCallback(string message)
{
CallbackInfo callbackInfo = CallbackInfo.FromJson(message);
Banner.InvokeCallback(callbackInfo);
}
private static void InvokeCallback(CallbackInfo callbackInfo)
{
if (Banner.bannerListener != null)
{
string placementId = callbackInfo.placement_id;
switch (callbackInfo.callback)
{
case CallbackInfo.CallbackShow:
Banner.bannerListener.OnShow(placementId, callbackInfo.impressionData);
break;
case CallbackInfo.CallbackError:
Banner.bannerListener.OnError(placementId, callbackInfo.error);
break;
case CallbackInfo.CallbackClick:
Banner.bannerListener.OnClick(placementId);
break;
case CallbackInfo.CallbackLoad:
Banner.bannerListener.OnLoad(placementId);
break;
case CallbackInfo.CallbackRequestStart:
Banner.bannerListener.OnRequestStart(placementId, callbackInfo.request_id);
break;
default:
Console.WriteLine("Unknown callback for Banner");
break;
}
}
}
private static IEnumerator InvokeCallbackNextFrame(CallbackInfo callbackInfo)
{
yield return null; // wait a frame
Banner.InvokeCallback(callbackInfo);
}
#endregion
}
#region Platform-specific translations
#if UNITY_IOS && !UNITY_EDITOR
public class BannerIOS : MonoBehaviour
{
[DllImport ("__Internal")]
private static extern void fyb_sdk_show_banner(string placementId, string position, bool adaptive, string refreshMode);
[DllImport ("__Internal")]
private static extern bool fyb_sdk_destroy_banner(string placementId);
[DllImport ("__Internal")]
private static extern bool fyb_sdk_hide_banner(string placementId);
[DllImport ("__Internal")]
private static extern bool fyb_sdk_refresh_banner(string placementId);
[DllImport ("__Internal")]
private static extern int fyb_sdk_impression_depth_banner();
public static void Show(string placementId, BannerOptions showOptions)
{
fyb_sdk_show_banner(placementId, showOptions.position, showOptions.adaptive, showOptions.refreshMode.ToString());
}
public static void Destroy(string placementId)
{
fyb_sdk_destroy_banner(placementId);
}
public static void Hide(string placementId)
{
fyb_sdk_hide_banner(placementId);
}
public static void Refresh(string placementId)
{
fyb_sdk_refresh_banner(placementId);
}
public static int GetImpressionDepth()
{
return fyb_sdk_impression_depth_banner();
}
}
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
public class BannerAndroid : MonoBehaviour
{
public static void Show(string placementId, BannerOptions showOptions)
{
if(Application.platform != RuntimePlatform.Android) return;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.sdk.extensions.unity3d.UnityHelper"))
{
jc.CallStatic("showBanner", placementId, showOptions.position, showOptions.adaptive, showOptions.refreshMode.ToString());
}
}
public static void Destroy(string placementId)
{
if(Application.platform != RuntimePlatform.Android) return;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.ads.Banner"))
{
jc.CallStatic("destroy", placementId);
}
}
public static void Refresh(string placementId)
{
if(Application.platform != RuntimePlatform.Android) return;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.ads.Banner"))
{
jc.CallStatic("refresh", placementId);
}
}
public static void Hide(string placementId)
{
if(Application.platform != RuntimePlatform.Android) return;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.ads.Banner"))
{
jc.CallStatic("hide", placementId);
}
}
public static int GetImpressionDepth()
{
if(Application.platform != RuntimePlatform.Android) return 0;
AndroidJNIHelper.debug = false;
using (AndroidJavaClass jc = new AndroidJavaClass("com.fyber.fairbid.ads.Banner"))
{
return jc.CallStatic("getImpressionDepth");
}
}
}
#endif
#endregion
}