Skip to content

Commit

Permalink
add native overlay ads
Browse files Browse the repository at this point in the history
  • Loading branch information
DucNV2000 committed Dec 2, 2024
1 parent 03b740d commit fd25a30
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 35 deletions.
15 changes: 15 additions & 0 deletions VirtueSky/Advertising/Editor/AdSettingEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class AdSettingEditor : Editor
private SerializedProperty _admobRewardVariable;
private SerializedProperty _admobRewardInterVariable;
private SerializedProperty _admobAppOpenVariable;
private SerializedProperty _admobNativeOverlayVariable;
private SerializedProperty _admobEnableTestMode;
private SerializedProperty _enableGDPR;
private SerializedProperty _enableGDPRTestMode;
Expand Down Expand Up @@ -63,6 +64,7 @@ void Initialize()
_admobRewardVariable = serializedObject.FindProperty("admobRewardVariable");
_admobRewardInterVariable = serializedObject.FindProperty("admobRewardInterVariable");
_admobAppOpenVariable = serializedObject.FindProperty("admobAppOpenVariable");
_admobNativeOverlayVariable = serializedObject.FindProperty("admobNativeOverlayVariable");
_admobEnableTestMode = serializedObject.FindProperty("admobEnableTestMode");
_admobDevicesTest = serializedObject.FindProperty("admobDevicesTest");
_enableGDPR = serializedObject.FindProperty("enableGDPR");
Expand Down Expand Up @@ -260,6 +262,19 @@ void SetupAdmob()
}
}

EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(_admobNativeOverlayVariable);
if (_admobNativeOverlayVariable.objectReferenceValue == null)
{
GUILayout.Space(2);
if (GUILayout.Button("Create", GUILayout.Width(55)))
{
_admobNativeOverlayVariable.objectReferenceValue =
CreateAsset.CreateAndGetScriptableAsset<AdmobNativeOverlayVariable>(pathAdmob);
}
}

EditorGUILayout.EndHorizontal();
EditorGUILayout.PropertyField(_admobEnableTestMode);
EditorGUILayout.PropertyField(_admobDevicesTest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace VirtueSky.Ads
[EditorIcon("icon_scriptable")]
public class AdmobBannerVariable : AdmobAdUnitVariable
{
public BannerSize size = BannerSize.Adaptive;
public BannerPosition position = BannerPosition.Bottom;
public AdsSize size = AdsSize.Adaptive;
public AdsPosition position = AdsPosition.Bottom;
public bool useCollapsible;
public bool useTestId;
#if VIRTUESKY_ADS && VIRTUESKY_ADMOB
Expand Down Expand Up @@ -138,12 +138,12 @@ public AdSize ConvertSize()
{
switch (size)
{
case BannerSize.Adaptive:
case AdsSize.Adaptive:
return AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(
AdSize.FullWidth);
case BannerSize.MediumRectangle: return AdSize.MediumRectangle;
case BannerSize.Leaderboard: return AdSize.Leaderboard;
case BannerSize.IABBanner: return AdSize.IABBanner;
case AdsSize.MediumRectangle: return AdSize.MediumRectangle;
case AdsSize.Leaderboard: return AdSize.Leaderboard;
case AdsSize.IABBanner: return AdSize.IABBanner;
//case BannerSize.SmartBanner: return AdSize.SmartBanner;
default: return AdSize.Banner;
}
Expand All @@ -153,23 +153,23 @@ public AdPosition ConvertPosition()
{
switch (position)
{
case BannerPosition.Top: return AdPosition.Top;
case BannerPosition.Bottom: return AdPosition.Bottom;
case BannerPosition.TopLeft: return AdPosition.TopLeft;
case BannerPosition.TopRight: return AdPosition.TopRight;
case BannerPosition.BottomLeft: return AdPosition.BottomLeft;
case BannerPosition.BottomRight: return AdPosition.BottomRight;
case AdsPosition.Top: return AdPosition.Top;
case AdsPosition.Bottom: return AdPosition.Bottom;
case AdsPosition.TopLeft: return AdPosition.TopLeft;
case AdsPosition.TopRight: return AdPosition.TopRight;
case AdsPosition.BottomLeft: return AdPosition.BottomLeft;
case AdsPosition.BottomRight: return AdPosition.BottomRight;
default: return AdPosition.Bottom;
}
}

public string ConvertPlacementCollapsible()
{
if (position == BannerPosition.Top)
if (position == AdsPosition.Top)
{
return "top";
}
else if (position == BannerPosition.Bottom)
else if (position == AdsPosition.Bottom)
{
return "bottom";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
using System;
using UnityEngine;
using VirtueSky.Inspector;
using VirtueSky.Utils;
using System.Collections;
#if VIRTUESKY_ADS && VIRTUESKY_ADMOB
using GoogleMobileAds.Api;
#endif
using VirtueSky.Core;
using VirtueSky.Misc;
using VirtueSky.Tracking;

namespace VirtueSky.Ads
{
[Serializable]
[EditorIcon("icon_scriptable")]
public class AdmobNativeOverlayVariable : AdmobAdUnitVariable
{
public enum NativeTemplate
{
Small,
Medium
}

[SerializeField] private bool useTestId;
#if VIRTUESKY_ADS && VIRTUESKY_ADMOB
[HeaderLine("NativeAd Options", false)] [SerializeField]
private AdChoicesPlacement adChoicesPlacement;

[SerializeField] private MediaAspectRatio mediaAspectRatio;
[SerializeField] private VideoOptions videoOptions;


[HeaderLine("NativeAd Style", false)] public NativeTemplate nativeTemplate;
public AdsSize adsSize = AdsSize.MediumRectangle;
public AdsPosition adsPosition = AdsPosition.Bottom;

// public NativeTemplateFontStyle nativeTemplateFontStyle;
private NativeOverlayAd _nativeOverlayAd;
#endif
private readonly WaitForSeconds _waitReload = new WaitForSeconds(5f);
private IEnumerator _reload;

/// <summary>
/// Init ads and register callback tracking
/// </summary>
public override void Init()
{
if (useTestId)
{
GetUnitTest();
}
#if VIRTUESKY_ADS && VIRTUESKY_ADMOB
if (AdStatic.IsRemoveAd || string.IsNullOrEmpty(Id)) return;
paidedCallback += AppTracking.TrackRevenue;
#endif
}

/// <summary>
/// Load ads
/// </summary>
public override void Load()
{
#if VIRTUESKY_ADS && VIRTUESKY_ADMOB
if (AdStatic.IsRemoveAd || string.IsNullOrEmpty(Id)) return;
if (_nativeOverlayAd != null)
{
Destroy();
}

var adRequest = new AdRequest();
var option = new NativeAdOptions
{
AdChoicesPlacement = adChoicesPlacement,
MediaAspectRatio = mediaAspectRatio,
VideoOptions = videoOptions
};
NativeOverlayAd.Load(Id, adRequest, option, AdLoadCallback);
#endif
}

public override bool IsReady()
{
#if VIRTUESKY_ADS && VIRTUESKY_ADMOB
return _nativeOverlayAd != null;
#else
return false;
#endif
}

protected override void ShowImpl()
{
if (_nativeOverlayAd != null)
_nativeOverlayAd.Show();
}

/// <summary>
/// destroy native overlay ads
/// </summary>
public override void Destroy()
{
if (_nativeOverlayAd != null)
{
_nativeOverlayAd.Destroy();
_nativeOverlayAd = null;
}
}

/// <summary>
/// Hide native overlay ads
/// </summary>
public void Hide()
{
if (_nativeOverlayAd != null) _nativeOverlayAd.Hide();
}

public NativeTemplateStyle Style()
{
return new NativeTemplateStyle
{
TemplateId = nativeTemplate.ToString().ToLower(),
// MainBackgroundColor = Color.red,
// CallToActionText = new NativeTemplateTextStyle
// {
// BackgroundColor = Color.green,
// TextColor = Color.white,
// FontSize = 9,
// Style = nativeTemplateFontStyle
// }
};
}

/// <summary>
/// Render native overlay ads default
/// </summary>
public void RenderAd()
{
#if VIRTUESKY_ADS && VIRTUESKY_ADS
if (_nativeOverlayAd == null) return;
_nativeOverlayAd.RenderTemplate(Style(), ConvertSize(), ConvertPosition());
#endif
}

/// <summary>
/// Render native ads according to uiElement, use canvas overlay
/// </summary>
/// <param name="uiElement"></param>
public void RenderAd(RectTransform uiElement)
{
#if VIRTUESKY_ADS && VIRTUESKY_ADS
if (_nativeOverlayAd == null) return;
var screenPosition = uiElement.ToWorldPosition();

float dpi = Screen.dpi / 160f;
var admobX = (int)(screenPosition.x / dpi);
var admobY = (int)((Screen.height - (int)screenPosition.y) / dpi);
_nativeOverlayAd.RenderTemplate(Style(), admobX, admobY);
#endif
}

/// <summary>
/// Render native ads according to uiElement, use canvas screen-space camera
/// </summary>
/// <param name="uiElement"></param>
/// <param name="canvas"></param>
public void RenderAd(RectTransform uiElement, Canvas canvas)
{
if (_nativeOverlayAd == null) return;
var worldPosition = uiElement.TransformPoint(uiElement.position);
Vector2 screenPosition = canvas.worldCamera.WorldToScreenPoint(worldPosition);

float dpi = Screen.dpi / 160f;
var admobX = (int)(screenPosition.x / dpi);
var admobY = (int)((Screen.height - (int)screenPosition.y) / dpi);

_nativeOverlayAd?.RenderTemplate(Style(), admobX, admobY);
}

#if VIRTUESKY_ADS && VIRTUESKY_ADMOB
AdPosition ConvertPosition()
{
return adsPosition switch
{
AdsPosition.Top => AdPosition.Top,
AdsPosition.Bottom => AdPosition.Bottom,
AdsPosition.TopLeft => AdPosition.TopLeft,
AdsPosition.TopRight => AdPosition.TopRight,
AdsPosition.BottomLeft => AdPosition.BottomLeft,
AdsPosition.BottomRight => AdPosition.BottomRight,
_ => AdPosition.Center
};
}

AdSize ConvertSize()
{
return adsSize switch
{
AdsSize.Banner => AdSize.Banner,
AdsSize.MediumRectangle => AdSize.MediumRectangle,
AdsSize.IABBanner => AdSize.IABBanner,
AdsSize.Leaderboard => AdSize.Leaderboard,
_ => AdSize.MediumRectangle,
};
}

private void AdLoadCallback(NativeOverlayAd ad, LoadAdError error)
{
if (error != null || ad == null)
{
OnAdFailedToLoad(error);
return;
}

_nativeOverlayAd = ad;
_nativeOverlayAd.OnAdPaid += OnAdPaided;
_nativeOverlayAd.OnAdClicked += OnAdClicked;
_nativeOverlayAd.OnAdFullScreenContentOpened += OnAdOpening;
_nativeOverlayAd.OnAdFullScreenContentClosed += OnAdClosed;
OnAdLoaded();
}

private void OnAdLoaded()
{
Common.CallActionAndClean(ref loadedCallback);
OnLoadAdEvent?.Invoke();
}

private void OnAdClosed()
{
Common.CallActionAndClean(ref closedCallback);
OnClosedAdEvent?.Invoke();
}

private void OnAdOpening()
{
Common.CallActionAndClean(ref displayedCallback);
OnDisplayedAdEvent?.Invoke();
}

private void OnAdClicked()
{
Common.CallActionAndClean(ref clickedCallback);
OnClickedAdEvent?.Invoke();
}

private void OnAdPaided(AdValue value)
{
paidedCallback?.Invoke(value.Value / 1000000f,
"Admob",
Id,
"NativeOverlayAd", AdNetwork.Admob.ToString());
}

private void OnAdFailedToLoad(LoadAdError error)
{
Common.CallActionAndClean(ref failedToLoadCallback);
OnFailedToLoadAdEvent?.Invoke(error.GetMessage());
if (_reload != null) App.StopCoroutine(_reload);
_reload = DelayReload();
App.StartCoroutine(_reload);
}

private IEnumerator DelayReload()
{
yield return _waitReload;
Load();
}
#endif
[ContextMenu("Get Id test")]
void GetUnitTest()
{
#if UNITY_ANDROID
androidId = "ca-app-pub-3940256099942544/2247696110";
#elif UNITY_IOS
iOSId = "ca-app-pub-3940256099942544/3986624511";
#endif
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fd25a30

Please sign in to comment.