Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HB-6379] Include Adaptive Banner Support in Unity #166

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
2afc9b5
ios updated to use adaptive banners
kushG Aug 23, 2023
9c4d800
WIP banner work for 5.0
kushG Aug 24, 2023
76b0af9
Fixed banner load working on ios
kushG Aug 25, 2023
c944313
android updates
kushG Aug 25, 2023
d95c082
android updates
kushG Aug 28, 2023
d10dbe0
more android update
kushG Aug 28, 2023
5179bc3
banner keywords iOS WIP
kushG Aug 28, 2023
6a74bf7
ios keywords fix
kushG Aug 29, 2023
cc6e7c7
renaming variables
kushG Aug 30, 2023
7910d5c
android updates
kushG Aug 31, 2023
34cf931
Merge branch 'develop' into adaptive-banners
kushG Aug 31, 2023
2041876
working android code for fixed banners
kushG Sep 1, 2023
159b37b
Merge branch 'adaptive-banners' into HB-6379-helium-unity-include-ada…
kushG Sep 1, 2023
82427f7
keeping old API unchanged
kushG Sep 1, 2023
6222808
code for onAdRefreshed added
kushG Sep 1, 2023
0647229
AdWrapper fix for deprecated APIs
kushG Sep 1, 2023
3f62b38
android bridge updates
kushG Sep 1, 2023
3ac7e83
removing unwanted files from git
kushG Sep 1, 2023
5c57f45
destroy added
kushG Sep 1, 2023
a56422f
size name made get only
kushG Sep 1, 2023
4afc455
working gameobject code for android for fixed banners
kushG Sep 6, 2023
1a1cebf
creator added
kushG Sep 6, 2023
9fd35ff
working android and ios e2e
kushG Sep 7, 2023
50c0c1d
onAdViewAdded added on android
kushG Sep 7, 2023
1f91f03
onViewAdded merge
kushG Sep 7, 2023
fd012f7
minor changes
kushG Sep 7, 2023
8e62bd1
UnityBannerAdSize added
kushG Sep 7, 2023
2fd038d
Editor code added
kushG Sep 8, 2023
d6497db
keys commit removed
kushG Sep 8, 2023
3d36991
Merge branch 'develop' of https://github.com/ChartBoost/helium-unity-…
kushG Sep 8, 2023
00fd98e
added missing files
kushG Sep 8, 2023
e783576
added missing files
kushG Sep 8, 2023
c9072c6
Banner drag fix
kushG Sep 8, 2023
6001732
Update com.chartboost.mediation/Runtime/Platforms/IOS/ChartboostMedia…
kushG Sep 8, 2023
6be23e7
default case added in size names for ios
kushG Sep 11, 2023
821715c
using screen width for adaptive banners width
kushG Sep 11, 2023
c555019
WrapperStore added in iOS
kushG Sep 13, 2023
fdafeb0
Update build.gradle
kushG Sep 14, 2023
b1759da
fix for horizontal and vertical alignment on android
kushG Sep 16, 2023
46dc1f5
Apply suggestions from code review
kushG Sep 16, 2023
aa8a847
formatting updates, minor changes based on review comments
kushG Sep 17, 2023
b03c173
more formatting changes
kushG Sep 17, 2023
3b6f51f
testing line end commit
kushG Sep 18, 2023
a45a65f
Extra blank lines at the end of file on android code in Rider
kushG Sep 18, 2023
bfdaf07
Update com.chartboost.mediation/Runtime/AdFormats/Banner/Unity/Chartb…
kushG Sep 18, 2023
b779d3c
Apply suggestions from code review
kushG Sep 18, 2023
9c3b07a
few more updated based on review comments
kushG Sep 18, 2023
cc1592e
not destroying bannerAd in demo's OnDestroy
kushG Sep 18, 2023
d0c71d0
Fixes missing/extra EOL
kushG Sep 18, 2023
4341f0a
removed double code of setting banner ad to visible
kushG Sep 18, 2023
9f989ac
minor fix in gameobject naming
kushG Sep 19, 2023
fc0f79e
finding canvas made better
kushG Sep 19, 2023
f7c40ad
WillAppear changed to DidLoad
kushG Sep 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 129 additions & 72 deletions com.chartboost.mediation.demo/Assets/Demo/Demo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
using System.IO;
using System.Diagnostics.CodeAnalysis;
using Chartboost;
using Chartboost.AdFormats.Banner;
using Chartboost.AdFormats.Banner.Unity;
using Chartboost.AdFormats.Fullscreen;
using Chartboost.Banner;
using Chartboost.Requests;
using Chartboost.Utilities;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.UI;
Expand All @@ -30,13 +33,18 @@ public class Demo : MonoBehaviour
public InputField bannerPlacementInputField;
public Dropdown bannerSizeDropdown;
public Dropdown bannerLocationDropdown;
private ChartboostMediationBannerAd _bannerAd;
private bool _bannerAdIsVisible;
public Dropdown horizontalAlignmentDropdown;
public Dropdown verticalAlignmentDropdown;
SCastanedaMunoz marked this conversation as resolved.
Show resolved Hide resolved

public ScrollRect outputTextScrollRect;
public Text outputText;

public GameObject objectToDestroyForTest;

private bool _bannerAdIsVisible = true;
private bool _bannerAdIsDraggable = true;
private ChartboostMediationBannerAdSize[] _bannerSizes;
private ChartboostMediationBannerAdScreenLocation[] _screenLocations;
private ChartboostMediationUnityBannerAd _bannerAd;

#region Lifecycle
private void Awake()
Expand All @@ -46,11 +54,44 @@ private void Awake()
ChartboostMediation.DidReceivePartnerInitializationData += DidReceivePartnerInitializationData;
ChartboostMediation.DidReceiveImpressionLevelRevenueData += DidReceiveImpressionLevelRevenueData;
ChartboostMediation.UnexpectedSystemErrorDidOccur += UnexpectedSystemErrorDidOccur;
SetupBannerDelegates();
}

private void Start()
{
var screenWidthNative = ChartboostMediationConverters.PixelsToNative(Screen.width);
_bannerSizes = new[]
{
// Fixed
ChartboostMediationBannerAdSize.Standard,
ChartboostMediationBannerAdSize.MediumRect,
ChartboostMediationBannerAdSize.Leaderboard,

// Horizontal
ChartboostMediationBannerAdSize.Adaptive2X1(screenWidthNative),
ChartboostMediationBannerAdSize.Adaptive4X1(screenWidthNative),
ChartboostMediationBannerAdSize.Adaptive6X1(screenWidthNative),
ChartboostMediationBannerAdSize.Adaptive8X1(screenWidthNative),
ChartboostMediationBannerAdSize.Adaptive10X1(screenWidthNative),

// Vertical
ChartboostMediationBannerAdSize.Adaptive1X2(screenWidthNative),
ChartboostMediationBannerAdSize.Adaptive1X4(screenWidthNative),
ChartboostMediationBannerAdSize.Adaptive1X3(screenWidthNative),
ChartboostMediationBannerAdSize.Adaptive9X16(screenWidthNative),
};

_screenLocations = new[]
{
ChartboostMediationBannerAdScreenLocation.TopLeft,
ChartboostMediationBannerAdScreenLocation.TopCenter,
ChartboostMediationBannerAdScreenLocation.TopRight,
ChartboostMediationBannerAdScreenLocation.Center,
ChartboostMediationBannerAdScreenLocation.BottomLeft,
ChartboostMediationBannerAdScreenLocation.BottomCenter,
ChartboostMediationBannerAdScreenLocation.BottomRight,
ChartboostMediationBannerAdScreenLocation.TopCenter
};

if (outputText != null)
outputText.text = string.Empty;
fullScreenPanel.SetActive(true);
Expand All @@ -65,13 +106,8 @@ private void Start()
private void OnDestroy()
{
OnInvalidateFullscreenClick();

if (_bannerAd == null)
return;

_bannerAd.ClearLoaded();
_bannerAd.Destroy();
Log("destroyed an existing banner");
// BannerAd is destroyed on its own`OnDestroy()`
}

private void DidStart(string error)
Expand Down Expand Up @@ -193,97 +229,118 @@ public async void OnShowFullscreenClick()
#endregion

#region Banners
private void SetupBannerDelegates()

public async void OnCreateBannerClick()
{
ChartboostMediation.DidLoadBanner += DidLoadBanner;
ChartboostMediation.DidClickBanner += DidClickBanner;
ChartboostMediation.DidRecordImpressionBanner += DidRecordImpressionBanner;
if(_bannerAd != null)
Destroy(_bannerAd.gameObject);

var size = bannerSizeDropdown.value >= 0 && bannerSizeDropdown.value < _bannerSizes.Length
? _bannerSizes[bannerSizeDropdown.value] : ChartboostMediationBannerAdSize.Standard;

var screenPos = bannerLocationDropdown.value >= 0 && bannerLocationDropdown.value < _screenLocations.Length
? _screenLocations[bannerLocationDropdown.value] : ChartboostMediationBannerAdScreenLocation.Center;

Log($"Creating new Unity banner ad");
var canvas = GetComponentInParent<Canvas>();
_bannerAd = ChartboostMediation.GetUnityBannerAd(bannerPlacementInputField.text, canvas, size, screenPos);
_bannerAd.DidLoad += DidLoadBanner;
_bannerAd.DidClick += DidClickBanner;
_bannerAd.DidRecordImpression += DidRecordImpressionBanner;
_bannerAd.DidDrag += DidDragBanner;

var keywords = _bannerAd.Keywords ??= new Dictionary<string, string>();
keywords.Add("bnr_keyword1", "bnr_value1");
keywords.Add("bnr_keyword2", "bnr_value2");
_bannerAd.Keywords = keywords;

_bannerAd.VerticalAlignment = (ChartboostMediationBannerVerticalAlignment)verticalAlignmentDropdown.value;
_bannerAd.HorizontalAlignment = (ChartboostMediationBannerHorizontalAlignment)horizontalAlignmentDropdown.value;

var result = await _bannerAd.Load();
Log(result.Error == null ? "Successfully loaded banner" : result.Error?.Message);
}

public void OnCreateBannerClick()
public void OnHorizontalAlignmentChange()
{
var size = bannerSizeDropdown.value switch
if (_bannerAd != null)
{
2 => ChartboostMediationBannerAdSize.Leaderboard,
1 => ChartboostMediationBannerAdSize.MediumRect,
_ => ChartboostMediationBannerAdSize.Standard
};

_bannerAd?.Destroy();
_bannerAd.HorizontalAlignment = (ChartboostMediationBannerHorizontalAlignment)horizontalAlignmentDropdown.value;
}
}

Log("Creating banner on placement: " + bannerPlacementInputField.text + " with size: " + size);
_bannerAd = ChartboostMediation.GetBannerAd(bannerPlacementInputField.text, size);

if (_bannerAd == null)
public void OnVerticalAlignmentChange()
{
if (_bannerAd != null)
{
Log("Banner not found");
return;
_bannerAd.VerticalAlignment = (ChartboostMediationBannerVerticalAlignment)verticalAlignmentDropdown.value;
}
}

// example keywords usage
_bannerAd.SetKeyword("bnr_keyword1", "bnr_value1"); // accepted set
_bannerAd.SetKeyword("bnr_keyword2", "bnr_value2"); // accepted set
_bannerAd.SetKeyword(GenerateRandomString(65), "bnr_value2"); // rejected set
_bannerAd.SetKeyword("bnr_keyword3", GenerateRandomString(257)); // rejected set
_bannerAd.SetKeyword("bnr_keyword4", "bnr_value4"); // accepted set
var keyword4 = this._bannerAd.RemoveKeyword("bnr_keyword4"); // removal of existing
_bannerAd.RemoveKeyword("bnr_keyword4"); // removal of non-existing
_bannerAd.SetKeyword("bnr_keyword5", keyword4); // accepted set using prior value
_bannerAd.SetKeyword("bnr_keyword6", "bnr_value6"); // accepted set
_bannerAd.SetKeyword("bnr_keyword6", "bnr_value6_replaced"); // accepted replace
kushG marked this conversation as resolved.
Show resolved Hide resolved
var screenPos = bannerLocationDropdown.value switch
public void OnToggleBannerVisibilityClick()
{
if (_bannerAd != null)
{
0 => ChartboostMediationBannerAdScreenLocation.TopLeft,
1 => ChartboostMediationBannerAdScreenLocation.TopCenter,
2 => ChartboostMediationBannerAdScreenLocation.TopRight,
3 => ChartboostMediationBannerAdScreenLocation.Center,
4 => ChartboostMediationBannerAdScreenLocation.BottomLeft,
5 => ChartboostMediationBannerAdScreenLocation.BottomCenter,
6 => ChartboostMediationBannerAdScreenLocation.BottomRight,
_ => ChartboostMediationBannerAdScreenLocation.TopCenter
};
_bannerAd.Load(screenPos);
_bannerAdIsVisible = !_bannerAdIsVisible;
_bannerAd.gameObject.SetActive(_bannerAdIsVisible);
}
Log("Banner Visibility Toggled");
}

public void OnRemoveBannerClick()
public void OnToggleBannerDraggabilityClick()
{
_bannerAd?.Destroy();
_bannerAd = null;
Log("Banner Removed");
if (_bannerAd != null)
{
_bannerAdIsDraggable = !_bannerAdIsDraggable;
_bannerAd.Draggable = _bannerAdIsDraggable;
}
Log("Banner Draggability Toggled");
}

public void OnClearBannerClick()
public void OnResetBannerClick()
{
if (_bannerAd == null)
{
Log("banner ad does not exist");
return;
}
_bannerAd.ClearLoaded();
Log("banner ad has been cleared");
_bannerAd.ResetAd();
Log("banner ad has been reset");
}

public void OnToggleBannerVisibilityClick()
public void OnRemoveBannerClick()
{
if (_bannerAd != null)
if (_bannerAd == null)
{
_bannerAdIsVisible = !_bannerAdIsVisible;
_bannerAd.SetVisibility(_bannerAdIsVisible);
Log("banner ad does not exist");
return;
}
Log("Banner Visibility Toggled");

Destroy(_bannerAd.gameObject);
Log("banner ad has been destroyed");

}

private void DidLoadBanner(string placementName, string loadId, BidInfo info, string error)
private void DidLoadBanner()
{
_bannerAdIsVisible = true;
Log($"DidLoadBanner{placementName}: \nLoadId: ${loadId} \nPrice: ${info.Price:F4} \nAuction Id: {info.AuctionId} \nPartner Id: {info.PartnerId} \nError: {error}");
Log($"DidLoadBanner {_bannerAd}");
}

private void DidRecordImpressionBanner()
{
Log($"DidRecordImpressionBanner {_bannerAd}");
}

private void DidClickBanner(string placementName, string error)
=> Log($"DidClickBanner {placementName}: {error}");

private void DidRecordImpressionBanner(string placementName, string error)
=> Log($"DidRecordImpressionBanner {placementName}: {error}");
private void DidClickBanner()
{
Log($"DidClickBanner {_bannerAd}");
}

private void DidDragBanner(float x, float y)
{
// This gets called for each frame whenever the banner is dragged on screen
}

#endregion

#region Utility
Expand Down
Loading
Loading