【广告】导入AudienceNetwork

iOS_release
zhangaotian 2024-06-28 18:47:48 +08:00
parent eabe8a9b75
commit 5d09c43241
47 changed files with 10759 additions and 18 deletions

View File

@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: bbaa99a7e4be14aee98635ce9f81dab0
folderAsset: yes
timeCreated: 1531986660
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 07842ad4a92814a3b80d63d5bda584fc
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<dependencies>
<androidPackages>
<androidPackage spec="com.android.support:support-annotations:28.0.0" />
<androidPackage spec="com.facebook.android:audience-network-sdk:6.4.0" />
</androidPackages>
<iosPods>
<iosPod name="FBAudienceNetwork" version="~&gt; 6.4.1" />
</iosPods>
</dependencies>

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 53e2520257ee14884b66cb776c6a5600
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: a033615a8b9d9480392002260025f399
folderAsset: yes
timeCreated: 1531986660
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,50 @@
using UnityEngine;
using System;
using System.Collections.Generic;
namespace AudienceNetwork
{
public class AdHandler : MonoBehaviour
{
private static readonly Queue<Action> executeOnMainThreadQueue = new Queue<Action>();
public void ExecuteOnMainThread(Action action)
{
lock (executeOnMainThreadQueue)
{
executeOnMainThreadQueue.Enqueue(action);
}
}
void Update()
{
// dispatch stuff on main thread
while (executeOnMainThreadQueue.Count > 0)
{
Action dequeuedAction = null;
lock (executeOnMainThreadQueue)
{
try
{
dequeuedAction = executeOnMainThreadQueue.Dequeue();
}
catch (Exception e)
{
Debug.LogException(e);
}
}
if (dequeuedAction != null)
{
dequeuedAction.Invoke();
}
}
}
public void RemoveFromParent()
{
#if !UNITY_EDITOR
UnityEngine.Object.Destroy(this);
#endif
}
}
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: d47bb3b71586b48dd954f5cdb11b8d19
timeCreated: 1531986666
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,303 @@
using UnityEngine;
using System.Runtime.InteropServices;
namespace AudienceNetwork
{
public static class AdSettings
{
public static void AddTestDevice(string deviceID)
{
AdSettingsBridge.Instance.AddTestDevice(deviceID);
}
public static void SetUrlPrefix(string urlPrefix)
{
AdSettingsBridge.Instance.SetUrlPrefix(urlPrefix);
}
public static void SetMixedAudience(bool mixedAudience)
{
AdSettingsBridge.Instance.SetMixedAudience(mixedAudience);
}
public static void SetDataProcessingOptions(string[] dataProcessingOptions)
{
AdSettingsBridge.Instance.SetDataProcessingOptions(dataProcessingOptions);
}
public static void SetDataProcessingOptions(string[] dataProcessingOptions, int country, int state)
{
AdSettingsBridge.Instance.SetDataProcessingOptions(dataProcessingOptions, country, state);
}
public static string GetBidderToken()
{
return AdSettingsBridge.Instance.GetBidderToken();
}
#if UNITY_IOS
public static void SetAdvertiserTrackingEnabled(bool advertiserTrackingEnabled)
{
AdSettingsBridge.Instance.SetAdvertiserTrackingEnabled(advertiserTrackingEnabled);
}
#endif
}
internal static class AdLogger
{
private enum AdLogLevel
{
None,
Notification,
Error,
Warning,
Log,
Debug,
Verbose
}
private static AdLogLevel logLevel = AdLogLevel.Log;
private static readonly string logPrefix = "Audience Network Unity ";
internal static void Log(string message)
{
AdLogLevel level = AdLogLevel.Log;
if (logLevel >= level)
{
Debug.Log(logPrefix + LevelAsString(level) + message);
}
}
internal static void LogWarning(string message)
{
AdLogLevel level = AdLogLevel.Warning;
if (logLevel >= level)
{
Debug.LogWarning(logPrefix + LevelAsString(level) + message);
}
}
internal static void LogError(string message)
{
AdLogLevel level = AdLogLevel.Error;
if (logLevel >= level)
{
Debug.LogError(logPrefix + LevelAsString(level) + message);
}
}
private static string LevelAsString(AdLogLevel logLevel)
{
switch (logLevel)
{
case AdLogLevel.Error:
{
return "<error>: ";
}
case AdLogLevel.Warning:
{
return "<warn>: ";
}
case AdLogLevel.Log:
{
return "<log>: ";
}
case AdLogLevel.Debug:
{
return "<debug>: ";
}
case AdLogLevel.Verbose:
{
return "<verbose>: ";
}
default:
{
return "";
}
}
}
}
internal interface IAdSettingsBridge
{
void AddTestDevice(string deviceID);
void SetUrlPrefix(string urlPrefix);
void SetMixedAudience(bool mixedAudience);
void SetDataProcessingOptions(string[] dataProcessingOptions);
void SetDataProcessingOptions(string[] dataProcessingOptions, int country, int state);
string GetBidderToken();
#if UNITY_IOS
void SetAdvertiserTrackingEnabled(bool advertiserTrackingEnabled);
#endif
}
internal class AdSettingsBridge : IAdSettingsBridge
{
public static readonly IAdSettingsBridge Instance;
internal AdSettingsBridge()
{
}
static AdSettingsBridge()
{
Instance = CreateInstance();
}
private static IAdSettingsBridge CreateInstance()
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
#if UNITY_IOS
return new AdSettingsBridgeIOS();
#elif UNITY_ANDROID
return new AdSettingsBridgeAndroid();
#endif
}
return new AdSettingsBridge();
}
public virtual void AddTestDevice(string deviceID)
{
}
public virtual void SetUrlPrefix(string urlPrefix)
{
}
public virtual void SetMixedAudience(bool mixedAudience)
{
}
public virtual void SetDataProcessingOptions(string[] dataProcessingOptions)
{
}
public virtual void SetDataProcessingOptions(string[] dataProcessingOptions, int country, int state)
{
}
public virtual string GetBidderToken()
{
return string.Empty;
}
#if UNITY_IOS
public virtual void SetAdvertiserTrackingEnabled(bool advertiserTrackingEnabled)
{
}
#endif
}
#if UNITY_ANDROID
internal class AdSettingsBridgeAndroid : AdSettingsBridge
{
public override void AddTestDevice(string deviceID)
{
AndroidJavaClass adSettings = GetAdSettingsObject();
adSettings.CallStatic("addTestDevice", deviceID);
}
public override void SetUrlPrefix(string urlPrefix)
{
AndroidJavaClass adSettings = GetAdSettingsObject();
adSettings.CallStatic("setUrlPrefix", urlPrefix);
}
public override void SetMixedAudience(bool mixedAudience)
{
AndroidJavaClass adSettings = GetAdSettingsObject();
adSettings.CallStatic("setMixedAudience", mixedAudience);
}
public override void SetDataProcessingOptions(string[] dataProcessingOptions)
{
AndroidJavaClass adSettings = GetAdSettingsObject();
adSettings.CallStatic("setDataProcessingOptions", (object) dataProcessingOptions);
}
public override void SetDataProcessingOptions(string[] dataProcessingOptions, int country, int state)
{
AndroidJavaClass adSettings = GetAdSettingsObject();
adSettings.CallStatic("setDataProcessingOptions", (object) dataProcessingOptions, country, state);
}
public override string GetBidderToken()
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
AndroidJavaClass bidderTokenProvider = new AndroidJavaClass("com.facebook.ads.BidderTokenProvider");
return bidderTokenProvider.CallStatic<string>("getBidderToken", context);
}
private AndroidJavaClass GetAdSettingsObject()
{
return new AndroidJavaClass("com.facebook.ads.AdSettings");
}
}
#endif
#if UNITY_IOS
internal class AdSettingsBridgeIOS : AdSettingsBridge
{
[DllImport("__Internal")]
private static extern void FBAdSettingsBridgeAddTestDevice(string deviceID);
[DllImport("__Internal")]
private static extern void FBAdSettingsBridgeSetURLPrefix(string urlPrefix);
[DllImport("__Internal")]
private static extern void FBAdSettingsBridgeSetMixedAudience(bool mixedAudience);
[DllImport("__Internal")]
private static extern void FBAdSettingsBridgeSetDataProcessingOptions(string[] dataProcessingOptions, int length);
[DllImport("__Internal")]
private static extern void FBAdSettingsBridgeSetDetailedDataProcessingOptions(string[] dataProcessingOptions, int length, int country, int state);
[DllImport("__Internal")]
private static extern string FBAdSettingsBridgeGetBidderToken();
[DllImport("__Internal")]
private static extern void FBAdSettingsBridgeSetAdvertiserTrackingEnabled(bool advertiserTrackingEnabled);
public override void AddTestDevice(string deviceID)
{
FBAdSettingsBridgeAddTestDevice(deviceID);
}
public override void SetUrlPrefix(string urlPrefix)
{
FBAdSettingsBridgeSetURLPrefix(urlPrefix);
}
public override void SetMixedAudience(bool mixedAudience)
{
FBAdSettingsBridgeSetMixedAudience(mixedAudience);
}
public override void SetDataProcessingOptions(string[] dataProcessingOptions)
{
FBAdSettingsBridgeSetDataProcessingOptions(dataProcessingOptions, dataProcessingOptions.Length);
}
public override void SetDataProcessingOptions(string[] dataProcessingOptions, int country, int state)
{
FBAdSettingsBridgeSetDetailedDataProcessingOptions(dataProcessingOptions, dataProcessingOptions.Length, country, state);
}
public override string GetBidderToken()
{
return FBAdSettingsBridgeGetBidderToken();
}
public override void SetAdvertiserTrackingEnabled(bool advertiserTrackingEnabled)
{
FBAdSettingsBridgeSetAdvertiserTrackingEnabled(advertiserTrackingEnabled);
}
}
#endif
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 2c6dc3140db0d40609e64d86ea8038bc
timeCreated: 1531986666
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,211 @@
using UnityEngine;
using System.Runtime.InteropServices;
using System;
namespace AudienceNetwork.Utility
{
public static class AdUtility
{
internal static double Width()
{
return AdUtilityBridge.Instance.Width();
}
internal static double Height()
{
return AdUtilityBridge.Instance.Height();
}
internal static double Convert(double deviceSize)
{
return AdUtilityBridge.Instance.Convert(deviceSize);
}
internal static void Prepare()
{
AdUtilityBridge.Instance.Prepare();
}
internal static bool IsLandscape()
{
return Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight;
}
}
internal interface IAdUtilityBridge
{
double DeviceWidth();
double DeviceHeight();
double Width();
double Height();
double Convert(double deviceSize);
void Prepare();
}
internal class AdUtilityBridge : IAdUtilityBridge
{
public static readonly IAdUtilityBridge Instance;
internal AdUtilityBridge()
{
}
static AdUtilityBridge()
{
Instance = CreateInstance();
}
private static IAdUtilityBridge CreateInstance()
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
#if UNITY_IOS
return new AdUtilityBridgeIOS();
#elif UNITY_ANDROID
return new AdUtilityBridgeAndroid();
#endif
}
return new AdUtilityBridge();
}
public virtual double DeviceWidth()
{
return 2208;
}
public virtual double DeviceHeight()
{
return 1242;
}
public virtual double Width()
{
return 1104;
}
public virtual double Height()
{
return 621;
}
public virtual double Convert(double deviceSize)
{
return 2;
}
public virtual void Prepare()
{
}
}
#if UNITY_ANDROID
internal class AdUtilityBridgeAndroid : AdUtilityBridge
{
private T GetPropertyOfDisplayMetrics<T>(string property)
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
AndroidJavaObject resources = context.Call<AndroidJavaObject>("getResources");
AndroidJavaObject displayMetrics = resources.Call<AndroidJavaObject>("getDisplayMetrics");
return displayMetrics.Get<T>(property);
}
private double Density()
{
return GetPropertyOfDisplayMetrics<float>("density");
}
public override double DeviceWidth()
{
return GetPropertyOfDisplayMetrics<int>("widthPixels");
}
public override double DeviceHeight()
{
return GetPropertyOfDisplayMetrics<int>("heightPixels");
}
public override double Width()
{
// Leaving the original code commented out for now - for reference. Will remove it eventually.
// return this.convert(this.deviceWidth());
return Convert(Screen.width);
}
public override double Height()
{
// Leaving the original code commented out for now - for reference. Will remove it eventually.
//return this.convert(this.deviceHeight());
return Convert(Screen.height);
}
public override double Convert(double deviceSize)
{
return deviceSize / Density();
}
public override void Prepare()
{
#if UNITY_ANDROID
try
{
AndroidJavaClass looperClass = new AndroidJavaClass("android.os.Looper");
looperClass.CallStatic("prepare");
}
catch (Exception)
{
}
#endif
}
}
#endif
#if UNITY_IOS
internal class AdUtilityBridgeIOS : AdUtilityBridge
{
[DllImport("__Internal")]
private static extern double FBAdUtilityBridgeGetDeviceWidth();
[DllImport("__Internal")]
private static extern double FBAdUtilityBridgeGetDeviceHeight();
[DllImport("__Internal")]
private static extern double FBAdUtilityBridgeGetWidth();
[DllImport("__Internal")]
private static extern double FBAdUtilityBridgeGetHeight();
[DllImport("__Internal")]
private static extern double FBAdUtilityBridgeConvertFromDeviceSize(double deviceSize);
public override double DeviceWidth()
{
return FBAdUtilityBridgeGetDeviceWidth();
}
public override double DeviceHeight()
{
return FBAdUtilityBridgeGetDeviceHeight();
}
public override double Width()
{
return FBAdUtilityBridgeGetWidth();
}
public override double Height()
{
return FBAdUtilityBridgeGetHeight();
}
public override double Convert(double deviceSize)
{
return FBAdUtilityBridgeConvertFromDeviceSize(deviceSize);
}
}
#endif
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 50f6966ab7c694899a9a6ec5bfe13ce7
timeCreated: 1531986666
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: ea40c685492bb4a8c8ecc98f34c52ed2
timeCreated: 1531986666
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,42 @@
using UnityEngine;
using System.Runtime.InteropServices;
using System;
namespace AudienceNetwork
{
public static class AudienceNetworkAds
{
#pragma warning disable 0414
private static bool isInitialized;
#pragma warning restore 0414
internal static void Initialize()
{
if (IsInitialized()) { return; }
PlayerPrefs.SetString("an_isUnitySDK", SdkVersion.Build);
#if UNITY_ANDROID
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
AndroidJavaClass audienceNetworkAds = new AndroidJavaClass("com.facebook.ads.AudienceNetworkAds");
audienceNetworkAds.CallStatic("initialize", context);
#endif
isInitialized = true;
}
internal static bool IsInitialized()
{
#if UNITY_ANDROID
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
AndroidJavaClass audienceNetworkAds = new AndroidJavaClass("com.facebook.ads.AudienceNetworkAds");
return audienceNetworkAds.CallStatic<bool>("isInitialized", context);
#else
return isInitialized;
#endif
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4f7b3f57160c342918af28e4b32686e8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,118 @@
using UnityEngine;
using System;
using System.Collections.Generic;
namespace AudienceNetwork
{
[Serializable]
public class ExtraHints
{
public struct Keyword
{
public const string ACCESSORIES = "accessories";
public const string ART_HISTORY = "art_history";
public const string AUTOMOTIVE = "automotive";
public const string BEAUTY = "beauty";
public const string BIOLOGY = "biology";
public const string BOARD_GAMES = "board_games";
public const string BUSINESS_SOFTWARE = "business_software";
public const string BUYING_SELLING_HOMES = "buying_selling_homes";
public const string CATS = "cats";
public const string CELEBRITIES = "celebrities";
public const string CLOTHING = "clothing";
public const string COMIC_BOOKS = "comic_books";
public const string DESKTOP_VIDEO = "desktop_video";
public const string DOGS = "dogs";
public const string EDUCATION = "education";
public const string EMAIL = "email";
public const string ENTERTAINMENT = "entertainment";
public const string FAMILY_PARENTING = "family_parenting";
public const string FASHION = "fashion";
public const string FINE_ART = "fine_art";
public const string FOOD_DRINK = "food_drink";
public const string FRENCH_CUISINE = "french_cuisine";
public const string GOVERNMENT = "government";
public const string HEALTH_FITNESS = "health_fitness";
public const string HOBBIES = "hobbies";
public const string HOME_GARDEN = "home_garden";
public const string HUMOR = "humor";
public const string INTERNET_TECHNOLOGY = "internet_technology";
public const string LARGE_ANIMALS = "large_animals";
public const string LAW = "law";
public const string LEGAL_ISSUES = "legal_issues";
public const string LITERATURE = "literature";
public const string MARKETING = "marketing";
public const string MOVIES = "movies";
public const string MUSIC = "music";
public const string NEWS = "news";
public const string PERSONAL_FINANCE = "personal_finance";
public const string PETS = "pets";
public const string PHOTOGRAPHY = "photography";
public const string POLITICS = "politics";
public const string REAL_ESTATE = "real_estate";
public const string ROLEPLAYING_GAMES = "roleplaying_games";
public const string SCIENCE = "science";
public const string SHOPPING = "shopping";
public const string SOCIETY = "society";
public const string SPORTS = "sports";
public const string TECHNOLOGY = "technology";
public const string TELEVISION = "television";
public const string TRAVEL = "travel";
public const string VIDEO_COMPUTER_GAMES = "video_computer_games";
}
private const int KEYWORDS_MAX_COUNT = 5;
public List<string> keywords;
public string extraData;
public string contentURL;
internal AndroidJavaObject GetAndroidObject()
{
AndroidJavaObject builderExtraHintsAndroid = new AndroidJavaObject("com.facebook.ads.ExtraHints$Builder");
if (builderExtraHintsAndroid != null)
{
if (keywords != null)
{
AndroidJavaClass androidKeywordEnum = new AndroidJavaClass("com.facebook.ads.ExtraHints$Keyword");
AndroidJavaObject[] androidKeywordArray = androidKeywordEnum.CallStatic<AndroidJavaObject[]>("values");
AndroidJavaObject list = new AndroidJavaObject("java.util.ArrayList");
int currentCount = 0;
foreach (string keyword in keywords)
{
if (currentCount == KEYWORDS_MAX_COUNT)
{
break;
}
foreach (AndroidJavaObject obj in androidKeywordArray)
{
if (obj.Call<string>("toString").ToLower() == keyword)
{
list.Call<bool>("add", obj);
currentCount++;
break;
}
}
}
builderExtraHintsAndroid = builderExtraHintsAndroid
.Call<AndroidJavaObject>("keywords", list);
}
if (extraData != null)
{
builderExtraHintsAndroid = builderExtraHintsAndroid
.Call<AndroidJavaObject>("extraData", extraData);
}
if (contentURL != null)
{
builderExtraHintsAndroid = builderExtraHintsAndroid
.Call<AndroidJavaObject>("contentUrl", contentURL);
}
}
return builderExtraHintsAndroid.Call<AndroidJavaObject>("build");
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 327a6820e37284d458d23d5528ef137c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,950 @@
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using AOT;
using AudienceNetwork.Utility;
namespace AudienceNetwork
{
public delegate void FBInterstitialAdBridgeCallback();
public delegate void FBInterstitialAdBridgeErrorCallback(string error);
internal delegate void FBInterstitialAdBridgeExternalCallback(int uniqueId);
internal delegate void FBInterstitialAdBridgeErrorExternalCallback(int uniqueId, string error);
public sealed class InterstitialAd : IDisposable
{
private readonly int uniqueId;
private bool isLoaded;
private AdHandler handler;
public string PlacementId { get; private set; }
public FBInterstitialAdBridgeCallback InterstitialAdDidLoad
{
internal get
{
return interstitialAdDidLoad;
}
set
{
interstitialAdDidLoad = value;
InterstitialAdBridge.Instance.OnLoad(uniqueId, interstitialAdDidLoad);
}
}
public FBInterstitialAdBridgeCallback InterstitialAdWillLogImpression
{
internal get
{
return interstitialAdWillLogImpression;
}
set
{
interstitialAdWillLogImpression = value;
InterstitialAdBridge.Instance.OnImpression(uniqueId, interstitialAdWillLogImpression);
}
}
public FBInterstitialAdBridgeErrorCallback InterstitialAdDidFailWithError
{
internal get
{
return interstitialAdDidFailWithError;
}
set
{
interstitialAdDidFailWithError = value;
InterstitialAdBridge.Instance.OnError(uniqueId, interstitialAdDidFailWithError);
}
}
public FBInterstitialAdBridgeCallback InterstitialAdDidClick
{
internal get
{
return interstitialAdDidClick;
}
set
{
interstitialAdDidClick = value;
InterstitialAdBridge.Instance.OnClick(uniqueId, interstitialAdDidClick);
}
}
public FBInterstitialAdBridgeCallback InterstitialAdWillClose
{
internal get
{
return interstitialAdWillClose;
}
set
{
interstitialAdWillClose = value;
InterstitialAdBridge.Instance.OnWillClose(uniqueId, interstitialAdWillClose);
}
}
public FBInterstitialAdBridgeCallback InterstitialAdDidClose
{
internal get
{
return interstitialAdDidClose;
}
set
{
interstitialAdDidClose = value;
InterstitialAdBridge.Instance.OnDidClose(uniqueId, interstitialAdDidClose);
}
}
public FBInterstitialAdBridgeCallback InterstitialAdActivityDestroyed
{
internal get
{
return interstitialAdActivityDestroyed;
}
set
{
interstitialAdActivityDestroyed = value;
InterstitialAdBridge.Instance.OnActivityDestroyed(uniqueId, interstitialAdActivityDestroyed);
}
}
public FBInterstitialAdBridgeCallback interstitialAdDidLoad;
public FBInterstitialAdBridgeCallback interstitialAdWillLogImpression;
public FBInterstitialAdBridgeErrorCallback interstitialAdDidFailWithError;
public FBInterstitialAdBridgeCallback interstitialAdDidClick;
public FBInterstitialAdBridgeCallback interstitialAdWillClose;
public FBInterstitialAdBridgeCallback interstitialAdDidClose;
public FBInterstitialAdBridgeCallback interstitialAdActivityDestroyed;
public InterstitialAd(string placementId)
{
AudienceNetworkAds.Initialize();
PlacementId = placementId;
if (Application.platform != RuntimePlatform.OSXEditor)
{
uniqueId = InterstitialAdBridge.Instance.Create(placementId, this);
InterstitialAdBridge.Instance.OnLoad(uniqueId, InterstitialAdDidLoad);
InterstitialAdBridge.Instance.OnImpression(uniqueId, InterstitialAdWillLogImpression);
InterstitialAdBridge.Instance.OnClick(uniqueId, InterstitialAdDidClick);
InterstitialAdBridge.Instance.OnError(uniqueId, InterstitialAdDidFailWithError);
InterstitialAdBridge.Instance.OnWillClose(uniqueId, InterstitialAdWillClose);
InterstitialAdBridge.Instance.OnDidClose(uniqueId, InterstitialAdDidClose);
InterstitialAdBridge.Instance.OnActivityDestroyed(uniqueId, InterstitialAdActivityDestroyed);
}
}
~InterstitialAd()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(Boolean iAmBeingCalledFromDisposeAndNotFinalize)
{
Debug.Log("Interstitial Ad Disposed.");
InterstitialAdBridge.Instance.Release(uniqueId);
}
public override string ToString()
{
return string.Format(
"[InterstitialAd: " +
"PlacementId={0}, " +
"InterstitialAdDidLoad={1}, " +
"InterstitialAdWillLogImpression={2}, " +
"InterstitialAdDidFailWithError={3}, " +
"InterstitialAdDidClick={4}, " +
"InterstitialAdWillClose={5}, " +
"InterstitialAdDidClose={6}], " +
"InterstitialAdActivityDestroyed={7}]",
PlacementId,
InterstitialAdDidLoad,
InterstitialAdWillLogImpression,
InterstitialAdDidFailWithError,
InterstitialAdDidClick,
InterstitialAdWillClose,
InterstitialAdDidClose,
InterstitialAdActivityDestroyed);
}
public void Register(GameObject gameObject)
{
handler = gameObject.AddComponent<AdHandler>();
}
public void LoadAd()
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
InterstitialAdBridge.Instance.Load(uniqueId);
}
else
{
InterstitialAdDidLoad();
}
}
public void LoadAd(String bidPayload)
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
InterstitialAdBridge.Instance.Load(uniqueId, bidPayload);
}
else
{
InterstitialAdDidLoad();
}
}
public bool IsValid()
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
return (isLoaded && InterstitialAdBridge.Instance.IsValid(uniqueId));
}
else
{
return true;
}
}
internal void LoadAdFromData()
{
isLoaded = true;
if (InterstitialAdDidLoad != null)
{
handler.ExecuteOnMainThread(() =>
{
InterstitialAdDidLoad();
});
}
}
public bool Show()
{
return InterstitialAdBridge.Instance.Show(uniqueId);
}
public void SetExtraHints(ExtraHints extraHints)
{
InterstitialAdBridge.Instance.SetExtraHints(uniqueId, extraHints);
}
internal void ExecuteOnMainThread(Action action)
{
if (handler)
{
handler.ExecuteOnMainThread(action);
}
}
public static implicit operator bool(InterstitialAd obj)
{
return !(object.ReferenceEquals(obj, null));
}
}
internal interface IInterstitialAdBridge
{
int Create(string placementId,
InterstitialAd interstitialAd);
int Load(int uniqueId);
int Load(int uniqueId, String bidPayload);
bool IsValid(int uniqueId);
bool Show(int uniqueId);
void SetExtraHints(int uniqueId, ExtraHints extraHints);
void Release(int uniqueId);
void OnLoad(int uniqueId,
FBInterstitialAdBridgeCallback callback);
void OnImpression(int uniqueId,
FBInterstitialAdBridgeCallback callback);
void OnClick(int uniqueId,
FBInterstitialAdBridgeCallback callback);
void OnError(int uniqueId,
FBInterstitialAdBridgeErrorCallback callback);
void OnWillClose(int uniqueId,
FBInterstitialAdBridgeCallback callback);
void OnDidClose(int uniqueId,
FBInterstitialAdBridgeCallback callback);
void OnActivityDestroyed(int uniqueId,
FBInterstitialAdBridgeCallback callback);
}
internal class InterstitialAdBridge : IInterstitialAdBridge
{
/* Interface to Interstitial implementation */
public static readonly IInterstitialAdBridge Instance;
internal InterstitialAdBridge()
{
}
static InterstitialAdBridge()
{
Instance = InterstitialAdBridge.CreateInstance();
}
private static IInterstitialAdBridge CreateInstance()
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
#if UNITY_IOS
return new InterstitialAdBridgeIOS();
#elif UNITY_ANDROID
return new InterstitialAdBridgeAndroid();
#endif
}
return new InterstitialAdBridge();
}
public virtual int Create(string placementId,
InterstitialAd InterstitialAd)
{
return 123;
}
public virtual int Load(int uniqueId)
{
return 123;
}
public virtual int Load(int uniqueId, String bidPayload)
{
return 123;
}
public virtual bool IsValid(int uniqueId)
{
return true;
}
public virtual bool Show(int uniqueId)
{
return true;
}
public virtual void SetExtraHints(int uniqueId, ExtraHints extraHints)
{
}
public virtual void Release(int uniqueId)
{
}
public virtual void OnLoad(int uniqueId,
FBInterstitialAdBridgeCallback callback)
{
}
public virtual void OnImpression(int uniqueId,
FBInterstitialAdBridgeCallback callback)
{
}
public virtual void OnClick(int uniqueId,
FBInterstitialAdBridgeCallback callback)
{
}
public virtual void OnError(int uniqueId,
FBInterstitialAdBridgeErrorCallback callback)
{
}
public virtual void OnWillClose(int uniqueId,
FBInterstitialAdBridgeCallback callback)
{
}
public virtual void OnDidClose(int uniqueId,
FBInterstitialAdBridgeCallback callback)
{
}
public virtual void OnActivityDestroyed(int uniqueId,
FBInterstitialAdBridgeCallback callback)
{
}
}
#if UNITY_ANDROID
internal class InterstitialAdBridgeAndroid : InterstitialAdBridge
{
private static Dictionary<int, InterstitialAdContainer> interstitialAds = new Dictionary<int, InterstitialAdContainer>();
private static int lastKey;
private AndroidJavaObject InterstitialAdForuniqueId(int uniqueId)
{
InterstitialAdContainer interstitialAdContainer = null;
bool success = InterstitialAdBridgeAndroid.interstitialAds.TryGetValue(uniqueId, out interstitialAdContainer);
if (success)
{
return interstitialAdContainer.bridgedInterstitialAd;
}
else
{
return null;
}
}
private InterstitialAdContainer InterstitialAdContainerForuniqueId(int uniqueId)
{
InterstitialAdContainer interstitialAdContainer = null;
bool success = InterstitialAdBridgeAndroid.interstitialAds.TryGetValue(uniqueId, out interstitialAdContainer);
if (success)
{
return interstitialAdContainer;
}
else
{
return null;
}
}
private string GetStringForuniqueId(int uniqueId,
string method)
{
AndroidJavaObject interstitialAd = InterstitialAdForuniqueId(uniqueId);
if (interstitialAd != null)
{
return interstitialAd.Call<string>(method);
}
else
{
return null;
}
}
private string GetImageURLForuniqueId(int uniqueId,
string method)
{
AndroidJavaObject interstitialAd = InterstitialAdForuniqueId(uniqueId);
if (interstitialAd != null)
{
AndroidJavaObject image = interstitialAd.Call<AndroidJavaObject>(method);
if (image != null)
{
return image.Call<string>("getUrl");
}
}
return null;
}
public override int Create(string placementId,
InterstitialAd interstitialAd)
{
AdUtility.Prepare();
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
AndroidJavaObject bridgedInterstitialAd = new AndroidJavaObject("com.facebook.ads.InterstitialAd", context, placementId);
InterstitialAdBridgeListenerProxy proxy = new InterstitialAdBridgeListenerProxy(interstitialAd, bridgedInterstitialAd);
InterstitialAdContainer interstitialAdContainer = new InterstitialAdContainer(interstitialAd)
{
bridgedInterstitialAd = bridgedInterstitialAd,
listenerProxy = proxy
};
int key = InterstitialAdBridgeAndroid.lastKey;
InterstitialAdBridgeAndroid.interstitialAds.Add(key, interstitialAdContainer);
InterstitialAdBridgeAndroid.lastKey++;
return key;
}
public override int Load(int uniqueId)
{
AdUtility.Prepare();
InterstitialAdContainer interstitialAdContainer = InterstitialAdContainerForuniqueId(uniqueId);
if (interstitialAdContainer != null)
{
interstitialAdContainer.Load();
}
return uniqueId;
}
public override int Load(int uniqueId, String bidPayload)
{
AdUtility.Prepare();
InterstitialAdContainer interstitialAdContainer = InterstitialAdContainerForuniqueId(uniqueId);
if (interstitialAdContainer != null)
{
interstitialAdContainer.Load(bidPayload);
}
return uniqueId;
}
public override bool IsValid(int uniqueId)
{
AndroidJavaObject interstitialAd = InterstitialAdForuniqueId(uniqueId);
if (interstitialAd != null)
{
return !interstitialAd.Call<bool>("isAdInvalidated");
}
else
{
return false;
}
}
public override bool Show(int uniqueId)
{
AndroidJavaObject interstitialAd = InterstitialAdForuniqueId(uniqueId);
if (interstitialAd != null)
{
return interstitialAd.Call<bool>("show");
}
else
{
return false;
}
}
public override void Release(int uniqueId)
{
AndroidJavaObject interstitialAd = InterstitialAdForuniqueId(uniqueId);
if (interstitialAd != null)
{
interstitialAd.Call("destroy");
}
InterstitialAdBridgeAndroid.interstitialAds.Remove(uniqueId);
}
public override void SetExtraHints(int uniqueId, ExtraHints extraHints)
{
AdUtility.Prepare();
AndroidJavaObject interstitialAd = InterstitialAdForuniqueId(uniqueId);
if (interstitialAd != null)
{
interstitialAd.Call("setExtraHints", extraHints.GetAndroidObject());
}
}
public override void OnLoad(int uniqueId, FBInterstitialAdBridgeCallback callback) { }
public override void OnImpression(int uniqueId, FBInterstitialAdBridgeCallback callback) { }
public override void OnClick(int uniqueId, FBInterstitialAdBridgeCallback callback) { }
public override void OnError(int uniqueId, FBInterstitialAdBridgeErrorCallback callback) { }
public override void OnWillClose(int uniqueId, FBInterstitialAdBridgeCallback callback) { }
public override void OnDidClose(int uniqueId, FBInterstitialAdBridgeCallback callback) { }
public override void OnActivityDestroyed(int uniqueId, FBInterstitialAdBridgeCallback callback) { }
}
#endif
#if UNITY_IOS
internal class InterstitialAdBridgeIOS : InterstitialAdBridge
{
private static Dictionary<int, InterstitialAdContainer> interstitialAds = new Dictionary<int, InterstitialAdContainer>();
private static InterstitialAdContainer interstitialAdContainerForuniqueId(int uniqueId)
{
InterstitialAdContainer interstitialAd = null;
bool success = InterstitialAdBridgeIOS.interstitialAds.TryGetValue(uniqueId, out interstitialAd);
if (success)
{
return interstitialAd;
}
else
{
return null;
}
}
[DllImport("__Internal")]
private static extern int FBInterstitialAdBridgeCreate(string placementId);
[DllImport("__Internal")]
private static extern int FBInterstitialAdBridgeLoad(int uniqueId);
[DllImport("__Internal")]
private static extern int FBInterstitialAdBridgeLoadWithBidPayload(int uniqueId, string bidPayload);
[DllImport("__Internal")]
private static extern bool FBInterstitialAdBridgeIsValid(int uniqueId);
[DllImport("__Internal")]
private static extern bool FBInterstitialAdBridgeShow(int uniqueId);
[DllImport("__Internal")]
private static extern void FBInterstitialAdBridgeSetExtraHints(int uniqueId, string extraHints);
[DllImport("__Internal")]
private static extern void FBInterstitialAdBridgeRelease(int uniqueId);
[DllImport("__Internal")]
private static extern void FBInterstitialAdBridgeOnLoad(int uniqueId,
FBInterstitialAdBridgeExternalCallback callback);
[DllImport("__Internal")]
private static extern void FBInterstitialAdBridgeOnImpression(int uniqueId,
FBInterstitialAdBridgeExternalCallback callback);
[DllImport("__Internal")]
private static extern void FBInterstitialAdBridgeOnClick(int uniqueId,
FBInterstitialAdBridgeExternalCallback callback);
[DllImport("__Internal")]
private static extern void FBInterstitialAdBridgeOnError(int uniqueId,
FBInterstitialAdBridgeErrorExternalCallback callback);
[DllImport("__Internal")]
private static extern void FBInterstitialAdBridgeOnDidClose(int uniqueId,
FBInterstitialAdBridgeExternalCallback callback);
[DllImport("__Internal")]
private static extern void FBInterstitialAdBridgeOnWillClose(int uniqueId,
FBInterstitialAdBridgeExternalCallback callback);
public override int Create(string placementId,
InterstitialAd interstitialAd)
{
int uniqueId = InterstitialAdBridgeIOS.FBInterstitialAdBridgeCreate(placementId);
InterstitialAdBridgeIOS.interstitialAds.Add(uniqueId, new InterstitialAdContainer(interstitialAd));
InterstitialAdBridgeIOS.FBInterstitialAdBridgeOnLoad(uniqueId, interstitialAdDidLoadBridgeCallback);
InterstitialAdBridgeIOS.FBInterstitialAdBridgeOnImpression(uniqueId, interstitialAdWillLogImpressionBridgeCallback);
InterstitialAdBridgeIOS.FBInterstitialAdBridgeOnClick(uniqueId, interstitialAdDidClickBridgeCallback);
InterstitialAdBridgeIOS.FBInterstitialAdBridgeOnError(uniqueId, interstitialAdDidFailWithErrorBridgeCallback);
InterstitialAdBridgeIOS.FBInterstitialAdBridgeOnDidClose(uniqueId, interstitialAdDidCloseBridgeCallback);
InterstitialAdBridgeIOS.FBInterstitialAdBridgeOnWillClose(uniqueId, interstitialAdWillCloseBridgeCallback);
return uniqueId;
}
public override int Load(int uniqueId)
{
return InterstitialAdBridgeIOS.FBInterstitialAdBridgeLoad(uniqueId);
}
public override int Load(int uniqueId, string bidPayload)
{
return InterstitialAdBridgeIOS.FBInterstitialAdBridgeLoadWithBidPayload(uniqueId, bidPayload);
}
public override bool IsValid(int uniqueId)
{
return InterstitialAdBridgeIOS.FBInterstitialAdBridgeIsValid(uniqueId);
}
public override bool Show(int uniqueId)
{
return InterstitialAdBridgeIOS.FBInterstitialAdBridgeShow(uniqueId);
}
public override void SetExtraHints(int uniqueId, ExtraHints extraHints)
{
InterstitialAdBridgeIOS.FBInterstitialAdBridgeSetExtraHints(uniqueId, JsonUtility.ToJson(extraHints));
}
public override void Release(int uniqueId)
{
InterstitialAdBridgeIOS.interstitialAds.Remove(uniqueId);
InterstitialAdBridgeIOS.FBInterstitialAdBridgeRelease(uniqueId);
}
// Sets up internal managed callbacks
public override void OnLoad(int uniqueId,
FBInterstitialAdBridgeCallback callback)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container)
{
container.onLoad = container.interstitialAd.LoadAdFromData;
}
}
public override void OnImpression(int uniqueId,
FBInterstitialAdBridgeCallback callback)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container)
{
container.onImpression = callback;
}
}
public override void OnClick(int uniqueId,
FBInterstitialAdBridgeCallback callback)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container)
{
container.onClick = callback;
}
}
public override void OnError(int uniqueId,
FBInterstitialAdBridgeErrorCallback callback)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container)
{
container.onError = callback;
}
}
public override void OnDidClose(int uniqueId,
FBInterstitialAdBridgeCallback callback)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container)
{
container.onDidClose = callback;
}
}
public override void OnWillClose(int uniqueId,
FBInterstitialAdBridgeCallback callback)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container)
{
container.onWillClose = callback;
}
}
// External unmanaged callbacks (must be static)
[MonoPInvokeCallback(typeof(FBInterstitialAdBridgeExternalCallback))]
private static void interstitialAdDidLoadBridgeCallback(int uniqueId)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container && container.onLoad != null)
{
container.onLoad();
}
}
[MonoPInvokeCallback(typeof(FBInterstitialAdBridgeExternalCallback))]
private static void interstitialAdWillLogImpressionBridgeCallback(int uniqueId)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container && container.onImpression != null)
{
container.onImpression();
}
}
[MonoPInvokeCallback(typeof(FBInterstitialAdBridgeErrorExternalCallback))]
private static void interstitialAdDidFailWithErrorBridgeCallback(int uniqueId, string error)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container && container.onError != null)
{
container.onError(error);
}
}
[MonoPInvokeCallback(typeof(FBInterstitialAdBridgeExternalCallback))]
private static void interstitialAdDidClickBridgeCallback(int uniqueId)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container && container.onClick != null)
{
container.onClick();
}
}
[MonoPInvokeCallback(typeof(FBInterstitialAdBridgeExternalCallback))]
private static void interstitialAdDidCloseBridgeCallback(int uniqueId)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container && container.onDidClose != null)
{
container.onDidClose();
}
}
[MonoPInvokeCallback(typeof(FBInterstitialAdBridgeExternalCallback))]
private static void interstitialAdWillCloseBridgeCallback(int uniqueId)
{
InterstitialAdContainer container = InterstitialAdBridgeIOS.interstitialAdContainerForuniqueId(uniqueId);
if (container && container.onWillClose != null)
{
container.onWillClose();
}
}
}
#endif
internal class InterstitialAdContainer
{
internal InterstitialAd interstitialAd { get; set; }
// iOS
internal FBInterstitialAdBridgeCallback onLoad { get; set; }
internal FBInterstitialAdBridgeCallback onImpression { get; set; }
internal FBInterstitialAdBridgeCallback onClick { get; set; }
internal FBInterstitialAdBridgeErrorCallback onError { get; set; }
internal FBInterstitialAdBridgeCallback onDidClose { get; set; }
internal FBInterstitialAdBridgeCallback onWillClose { get; set; }
internal FBInterstitialAdBridgeCallback onActivityDestroyed { get; set; }
// Android
#if UNITY_ANDROID
internal AndroidJavaProxy listenerProxy;
internal AndroidJavaObject bridgedInterstitialAd;
#endif
internal InterstitialAdContainer(InterstitialAd interstitialAd)
{
this.interstitialAd = interstitialAd;
}
public override string ToString()
{
return string.Format("[InterstitialAdContainer: interstitialAd={0}, onLoad={1}]", interstitialAd, onLoad);
}
public static implicit operator bool(InterstitialAdContainer obj)
{
return !(object.ReferenceEquals(obj, null));
}
#if UNITY_ANDROID
internal AndroidJavaObject LoadAdConfig(String bidPayload)
{
AndroidJavaObject configBuilder = bridgedInterstitialAd.Call<AndroidJavaObject>("buildLoadAdConfig");
configBuilder.Call<AndroidJavaObject>("withAdListener", listenerProxy);
if (bidPayload != null)
{
configBuilder.Call<AndroidJavaObject>("withBid", bidPayload);
}
return configBuilder.Call<AndroidJavaObject>("build");
}
public void Load()
{
Load(null);
}
public void Load(String bidPayload)
{
AndroidJavaObject loadConfig = LoadAdConfig(bidPayload);
bridgedInterstitialAd.Call("loadAd", loadConfig);
}
#endif
}
#if UNITY_ANDROID
internal class InterstitialAdBridgeListenerProxy : AndroidJavaProxy
{
private InterstitialAd interstitialAd;
#pragma warning disable 0414
private readonly AndroidJavaObject bridgedInterstitialAd;
#pragma warning restore 0414
public InterstitialAdBridgeListenerProxy(InterstitialAd interstitialAd,
AndroidJavaObject bridgedInterstitialAd)
: base("com.facebook.ads.InterstitialAdExtendedListener")
{
this.interstitialAd = interstitialAd;
this.bridgedInterstitialAd = bridgedInterstitialAd;
}
void onError(AndroidJavaObject ad,
AndroidJavaObject error)
{
string errorMessage = error.Call<string>("getErrorMessage");
if (interstitialAd.InterstitialAdDidFailWithError != null)
{
interstitialAd.ExecuteOnMainThread(() =>
{
interstitialAd.InterstitialAdDidFailWithError(errorMessage);
});
}
}
void onAdLoaded(AndroidJavaObject ad)
{
interstitialAd.LoadAdFromData();
}
// Not executing on the main thread - otherwise InterstitialAdDidClick
// will only be called after the Interstitial activity be destroyed
void onAdClicked(AndroidJavaObject ad)
{
if (interstitialAd.InterstitialAdDidClick != null)
{
interstitialAd.ExecuteOnMainThread(() =>
{
interstitialAd.InterstitialAdDidClick();
});
}
}
void onInterstitialDisplayed(AndroidJavaObject ad)
{
// Not reporting anything so it won't double with InterstitialAdWillLogImpression
}
void onInterstitialDismissed(AndroidJavaObject ad)
{
if (interstitialAd.InterstitialAdDidClose != null)
{
interstitialAd.ExecuteOnMainThread(() =>
{
interstitialAd.InterstitialAdDidClose();
});
}
}
// Not executing on the main thread - otherwise InterstitialAdWillLogImpression
// will only be called after the Interstitial activity be destroyed
void onLoggingImpression(AndroidJavaObject ad)
{
if (interstitialAd.InterstitialAdWillLogImpression != null)
{
interstitialAd.ExecuteOnMainThread(() =>
{
interstitialAd.InterstitialAdWillLogImpression();
});
}
}
void onInterstitialActivityDestroyed()
{
if (interstitialAd.InterstitialAdActivityDestroyed != null)
{
interstitialAd.ExecuteOnMainThread(() =>
{
interstitialAd.InterstitialAdActivityDestroyed();
});
}
}
}
#endif
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: aed5d3283123b47e9a5780a385f4e6ed
timeCreated: 1531986666
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 111ff137d27be424584e4e2160d44e80
timeCreated: 1531986666
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,33 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
* copy, modify, and distribute this software in source code or binary form for use
* in connection with the web services and APIs provided by Facebook.
*
* As with any software that integrates with the Facebook platform, your use of
* this software is subject to the Facebook Developer Principles and Policies
* [http://developers.facebook.com/policy/]. This copyright notice shall be
* included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace AudienceNetwork
{
public static class SdkVersion
{
public static string Build
{
get
{
return "6.4.0";
}
}
}
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 766cc4756a404418f86a9625e15090fc
timeCreated: 1531986666
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e9232d61943e74d6daf42157201fcaa0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: add9d2dab81c344fe97da6591d3665fb
folderAsset: yes
timeCreated: 1531986660
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,149 @@
using UnityEngine;
using AudienceNetwork;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using AudienceNetwork.Utility;
using System;
public class AdViewScene : BaseScene
{
private AdView adView;
private AdPosition currentAdViewPosition;
private ScreenOrientation currentScreenOrientation;
public Text statusLabel;
private AdSize[] adSizeArray = (AdSize[])Enum.GetValues(typeof(AdSize));
private int currentAdSize;
public Button loadAdButton;
void OnDestroy()
{
// Dispose of banner ad when the scene is destroyed
if (adView)
{
adView.Dispose();
}
Debug.Log("AdViewTest was destroyed!");
}
private void Awake()
{
AudienceNetworkAds.Initialize();
SetLoadAddButtonText();
SettingsScene.InitializeSettings();
}
private void SetLoadAddButtonText()
{
loadAdButton.GetComponentInChildren<Text>().text =
"Load Banner (" + adSizeArray[currentAdSize].ToString() + ")";
}
public void LoadBanner()
{
if (adView)
{
adView.Dispose();
}
statusLabel.text = "Loading Banner...";
// Create a banner's ad view with a unique placement ID
// (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.
adView = new AdView("YOUR_PLACEMENT_ID", adSizeArray[currentAdSize]);
adView.Register(gameObject);
currentAdViewPosition = AdPosition.CUSTOM;
// Set delegates to get notified on changes or when the user interacts
// with the ad.
adView.AdViewDidLoad = delegate ()
{
currentScreenOrientation = Screen.orientation;
adView.Show(100);
string isAdValid = adView.IsValid() ? "valid" : "invalid";
statusLabel.text = "Banner loaded and is " + isAdValid + ".";
Debug.Log("Banner loaded");
};
adView.AdViewDidFailWithError = delegate (string error)
{
statusLabel.text = "Banner failed to load with error: " + error;
Debug.Log("Banner failed to load with error: " + error);
};
adView.AdViewWillLogImpression = delegate ()
{
statusLabel.text = "Banner logged impression.";
Debug.Log("Banner logged impression.");
};
adView.AdViewDidClick = delegate ()
{
statusLabel.text = "Banner clicked.";
Debug.Log("Banner clicked.");
};
// Initiate a request to load an ad.
adView.LoadAd();
}
public void ChangeBannerSize()
{
currentAdSize += 1;
currentAdSize %= adSizeArray.Length;
SetLoadAddButtonText();
}
public void NextScene()
{
SceneManager.LoadScene("RewardedVideoAdScene");
}
// Change button
// Change the position of the ad view when button is clicked
// ad view is at top: move it to bottom
// ad view is at bottom: move it to 100 pixels along y-axis
// ad view is at custom position: move it to the top
public void ChangePosition()
{
switch (currentAdViewPosition)
{
case AdPosition.TOP:
SetAdViewPosition(AdPosition.BOTTOM);
break;
case AdPosition.BOTTOM:
SetAdViewPosition(AdPosition.CUSTOM);
break;
case AdPosition.CUSTOM:
SetAdViewPosition(AdPosition.TOP);
break;
}
}
private void OnRectTransformDimensionsChange()
{
if (adView && Screen.orientation != currentScreenOrientation)
{
SetAdViewPosition(currentAdViewPosition);
currentScreenOrientation = Screen.orientation;
}
}
private void SetAdViewPosition(AdPosition adPosition)
{
switch (adPosition)
{
case AdPosition.TOP:
adView.Show(AdPosition.TOP);
currentAdViewPosition = AdPosition.TOP;
break;
case AdPosition.BOTTOM:
adView.Show(AdPosition.BOTTOM);
currentAdViewPosition = AdPosition.BOTTOM;
break;
case AdPosition.CUSTOM:
adView.Show(100);
currentAdViewPosition = AdPosition.CUSTOM;
break;
}
}
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 905b751bb63e44191a5363d3ed3d31d0
timeCreated: 1531986666
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: cb29e86fb3daa461f8da1a6d755fe9ce
timeCreated: 1531986660
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,23 @@
using UnityEngine;
using UnityEngine.SceneManagement;
public class BaseScene : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (Application.platform == RuntimePlatform.Android)
{
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
return;
}
}
}
public void LoadSettingsScene()
{
SceneManager.LoadScene("SettingsScene");
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f8b448fe9f60a4883aa9907cc3236629
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 8c9f390ad7e3c4ac0a32a1479c44f14b
folderAsset: yes
timeCreated: 1531986660
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,117 @@
using UnityEngine;
using UnityEngine.UI;
using AudienceNetwork;
using UnityEngine.SceneManagement;
using AudienceNetwork.Utility;
public class InterstitialAdScene : BaseScene
{
private InterstitialAd interstitialAd;
private bool isLoaded;
#pragma warning disable 0414
private bool didClose;
#pragma warning restore 0414
// UI elements in scene
public Text statusLabel;
private void Awake()
{
AudienceNetworkAds.Initialize();
SettingsScene.InitializeSettings();
}
// Load button
public void LoadInterstitial()
{
statusLabel.text = "Loading interstitial ad...";
// Create the interstitial unit with a placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.
interstitialAd = new InterstitialAd("YOUR_PLACEMENT_ID");
interstitialAd.Register(gameObject);
// Set delegates to get notified on changes or when the user interacts with the ad.
interstitialAd.InterstitialAdDidLoad = delegate ()
{
Debug.Log("Interstitial ad loaded.");
isLoaded = true;
didClose = false;
string isAdValid = interstitialAd.IsValid() ? "valid" : "invalid";
statusLabel.text = "Ad loaded and is " + isAdValid + ". Click show to present!";
};
interstitialAd.InterstitialAdDidFailWithError = delegate (string error)
{
Debug.Log("Interstitial ad failed to load with error: " + error);
statusLabel.text = "Interstitial ad failed to load. Check console for details.";
};
interstitialAd.InterstitialAdWillLogImpression = delegate ()
{
Debug.Log("Interstitial ad logged impression.");
};
interstitialAd.InterstitialAdDidClick = delegate ()
{
Debug.Log("Interstitial ad clicked.");
};
interstitialAd.InterstitialAdDidClose = delegate ()
{
Debug.Log("Interstitial ad did close.");
didClose = true;
if (interstitialAd != null)
{
interstitialAd.Dispose();
}
};
#if UNITY_ANDROID
/*
* Only relevant to Android.
* This callback will only be triggered if the Interstitial activity has
* been destroyed without being properly closed. This can happen if an
* app with launchMode:singleTask (such as a Unity game) goes to
* background and is then relaunched by tapping the icon.
*/
interstitialAd.interstitialAdActivityDestroyed = delegate() {
if (!didClose) {
Debug.Log("Interstitial activity destroyed without being closed first.");
Debug.Log("Game should resume.");
}
};
#endif
// Initiate the request to load the ad.
interstitialAd.LoadAd();
}
// Show button
public void ShowInterstitial()
{
if (isLoaded)
{
interstitialAd.Show();
isLoaded = false;
statusLabel.text = "";
}
else
{
statusLabel.text = "Ad not loaded. Click load to request an ad.";
}
}
void OnDestroy()
{
// Dispose of interstitial ad when the scene is destroyed
if (interstitialAd != null)
{
interstitialAd.Dispose();
}
Debug.Log("InterstitialAdTest was destroyed!");
}
// Next button
public void NextScene()
{
SceneManager.LoadScene("AdViewScene");
}
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 00d288c1305fc494882c5c1d674f6779
timeCreated: 1531986666
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: f72b5a1d5965144f795ee32584534444
timeCreated: 1531986660
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: c8a0e5fbf02754ed683a9a2e34059b8c
folderAsset: yes
timeCreated: 1531986660
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,148 @@
using UnityEngine;
using UnityEngine.UI;
using AudienceNetwork;
using UnityEngine.SceneManagement;
using AudienceNetwork.Utility;
public class RewardedVideoAdScene : BaseScene
{
private RewardedVideoAd rewardedVideoAd;
private bool isLoaded;
#pragma warning disable 0414
private bool didClose;
#pragma warning restore 0414
// UI elements in scene
public Text statusLabel;
private void Awake()
{
AudienceNetworkAds.Initialize();
SettingsScene.InitializeSettings();
}
// Load button
public void LoadRewardedVideo()
{
statusLabel.text = "Loading rewardedVideo ad...";
// Create the rewarded video unit with a placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.
rewardedVideoAd = new RewardedVideoAd("YOUR_PLACEMENT_ID");
// For S2S validation you can create the rewarded video ad with the reward data
// Refer to documentation here:
// https://developers.facebook.com/docs/audience-network/android/rewarded-video#server-side-reward-validation
// https://developers.facebook.com/docs/audience-network/ios/rewarded-video#server-side-reward-validation
RewardData rewardData = new RewardData
{
UserId = "USER_ID",
Currency = "REWARD_ID"
};
#pragma warning disable 0219
RewardedVideoAd s2sRewardedVideoAd = new RewardedVideoAd("YOUR_PLACEMENT_ID", rewardData);
#pragma warning restore 0219
rewardedVideoAd.Register(gameObject);
// Set delegates to get notified on changes or when the user interacts with the ad.
rewardedVideoAd.RewardedVideoAdDidLoad = delegate ()
{
Debug.Log("RewardedVideo ad loaded.");
isLoaded = true;
didClose = false;
string isAdValid = rewardedVideoAd.IsValid() ? "valid" : "invalid";
statusLabel.text = "Ad loaded and is " + isAdValid + ". Click show to present!";
};
rewardedVideoAd.RewardedVideoAdDidFailWithError = delegate (string error)
{
Debug.Log("RewardedVideo ad failed to load with error: " + error);
statusLabel.text = "RewardedVideo ad failed to load. Check console for details.";
};
rewardedVideoAd.RewardedVideoAdWillLogImpression = delegate ()
{
Debug.Log("RewardedVideo ad logged impression.");
};
rewardedVideoAd.RewardedVideoAdDidClick = delegate ()
{
Debug.Log("RewardedVideo ad clicked.");
};
// For S2S validation you need to register the following two callback
// Refer to documentation here:
// https://developers.facebook.com/docs/audience-network/android/rewarded-video#server-side-reward-validation
// https://developers.facebook.com/docs/audience-network/ios/rewarded-video#server-side-reward-validation
rewardedVideoAd.RewardedVideoAdDidSucceed = delegate ()
{
Debug.Log("Rewarded video ad validated by server");
};
rewardedVideoAd.RewardedVideoAdDidFail = delegate ()
{
Debug.Log("Rewarded video ad not validated, or no response from server");
};
rewardedVideoAd.RewardedVideoAdDidClose = delegate ()
{
Debug.Log("Rewarded video ad did close.");
didClose = true;
if (rewardedVideoAd != null)
{
rewardedVideoAd.Dispose();
}
};
#if UNITY_ANDROID
/*
* Only relevant to Android.
* This callback will only be triggered if the Rewarded Video activity
* has been destroyed without being properly closed. This can happen if
* an app with launchMode:singleTask (such as a Unity game) goes to
* background and is then relaunched by tapping the icon.
*/
rewardedVideoAd.RewardedVideoAdActivityDestroyed = delegate ()
{
if (!didClose)
{
Debug.Log("Rewarded video activity destroyed without being closed first.");
Debug.Log("Game should resume. User should not get a reward.");
}
};
#endif
// Initiate the request to load the ad.
rewardedVideoAd.LoadAd();
}
// Show button
public void ShowRewardedVideo()
{
if (isLoaded)
{
rewardedVideoAd.Show();
isLoaded = false;
statusLabel.text = "";
}
else
{
statusLabel.text = "Ad not loaded. Click load to request an ad.";
}
}
void OnDestroy()
{
// Dispose of rewardedVideo ad when the scene is destroyed
if (rewardedVideoAd != null)
{
rewardedVideoAd.Dispose();
}
Debug.Log("RewardedVideoAdTest was destroyed!");
}
// Next button
public void NextScene()
{
SceneManager.LoadScene("InterstitialAdScene");
}
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 84bf87db85e7a4d18bf8422eb54ac837
timeCreated: 1531986666
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 0079f98ce8e504ce0b9d7c937e331b03
timeCreated: 1531986660
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: fe8cf484a03ff40ffa517b7d74fb82e9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,54 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SettingsScene : MonoBehaviour
{
private static string URL_PREFIX_KEY = "URL_PREFIX";
public InputField urlPrefixInput;
public Text sdkVersionText;
private string urlPrefix;
// Loads settings from previous play session
public static void InitializeSettings()
{
string prefix = PlayerPrefs.GetString(URL_PREFIX_KEY, "");
AudienceNetwork.AdSettings.SetUrlPrefix(prefix);
}
void Start()
{
urlPrefix = PlayerPrefs.GetString(URL_PREFIX_KEY, "");
urlPrefixInput.text = urlPrefix;
sdkVersionText.text = AudienceNetwork.SdkVersion.Build;
}
public void OnEditEnd(string prefix)
{
urlPrefix = prefix;
SaveSettings();
}
public void SaveSettings()
{
PlayerPrefs.SetString(URL_PREFIX_KEY, urlPrefix);
AudienceNetwork.AdSettings.SetUrlPrefix(urlPrefix);
}
public void AdViewScene()
{
SceneManager.LoadScene("AdViewScene");
}
public void InterstitialAdScene()
{
SceneManager.LoadScene("InterstitialAdScene");
}
public void RewardedVideoAdScene()
{
SceneManager.LoadScene("RewardedVideoAdScene");
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 12a33cb7aa8f042ef8bc6a44c91eed4d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d88663653ebb24580ac0c753bb4f140c
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,22 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<projectSettings> <projectSettings>
<projectSetting name="com.google.external-dependency-managerAnalyticsCookie" value="5537a838bfac415bac89394bdb95521e" />
<projectSetting name="com.google.external-dependency-managerAnalyticsEnabled" value="True" />
<projectSetting name="Google.IOSResolver.AutoPodToolInstallInEditor" value="False" />
<projectSetting name="Google.IOSResolver.CocoapodsIntegrationMethod" value="2" />
<projectSetting name="Google.IOSResolver.PodfileAddUseFrameworks" value="True" />
<projectSetting name="Google.IOSResolver.PodfileAllowPodsInMultipleTargets" value="True" />
<projectSetting name="Google.IOSResolver.PodfileAlwaysAddMainTarget" value="True" />
<projectSetting name="Google.IOSResolver.PodfileEnabled" value="True" />
<projectSetting name="Google.IOSResolver.PodfileStaticLinkFrameworks" value="True" />
<projectSetting name="Google.IOSResolver.PodToolExecutionViaShellEnabled" value="True" />
<projectSetting name="Google.IOSResolver.PodToolShellExecutionSetLang" value="True" />
<projectSetting name="Google.IOSResolver.SwiftFrameworkSupportWorkaroundEnabled" value="True" />
<projectSetting name="Google.IOSResolver.SwiftLanguageVersion" value="5.0" />
<projectSetting name="Google.IOSResolver.VerboseLoggingEnabled" value="False" /> <projectSetting name="Google.IOSResolver.VerboseLoggingEnabled" value="False" />
<projectSetting name="Google.PackageManagerResolver.VerboseLoggingEnabled" value="False" />
<projectSetting name="Google.VersionHandler.VerboseLoggingEnabled" value="False" />
<projectSetting name="GooglePlayServices.AutoResolverEnabled" value="False" />
<projectSetting name="GooglePlayServices.PromptBeforeAutoResolution" value="False" />
<projectSetting name="GooglePlayServices.UseJetifier" value="True" />
</projectSettings> </projectSettings>