Skip to content

Commit

Permalink
Unity: Breaking out the Android ad event into individual unity callba…
Browse files Browse the repository at this point in the history
…ck events (#7)
  • Loading branch information
marcsantiago authored May 3, 2021
1 parent 558cb89 commit 6d8d46c
Show file tree
Hide file tree
Showing 21 changed files with 273 additions and 183 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

# Autogenerated Jetbrains Rider plugin
[Aa]ssets/Plugins/Editor/JetBrains*
**/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/
Expand Down Expand Up @@ -80,4 +81,3 @@ crashlytics-build.properties

*.DotSettings
**/*.DotSettings

9 changes: 4 additions & 5 deletions sample-app/Assets/Example/Scripts/Activate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
using UnityEngine;

namespace Example.Scripts {
public class Activate:MonoBehaviour {
public class Activate : MonoBehaviour {
public List<GameObject> toActivate;

private bool _alreadyTriggered;

private void OnTriggerEnter2D(Collider2D other) {
var player = other.gameObject.GetComponent<NimbusPlayerController>();
if (player == null || _alreadyTriggered) return;
_alreadyTriggered = true;

foreach (var a in toActivate) {
a.SetActive(true);
}
foreach (var a in toActivate) a.SetActive(true);
}
}
}
4 changes: 2 additions & 2 deletions sample-app/Assets/Example/Scripts/BannerExample.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Nimbus.Scripts;
using Nimbus.Scripts.Internal;
using Nimbus.Runtime.Scripts;
using Nimbus.Runtime.Scripts.Internal;
using Unity.Mathematics;
using UnityEngine;

Expand Down
4 changes: 2 additions & 2 deletions sample-app/Assets/Example/Scripts/BannerExample2.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections;
using Nimbus.Scripts;
using Nimbus.Scripts.Internal;
using Nimbus.Runtime.Scripts;
using Nimbus.Runtime.Scripts.Internal;
using UnityEngine;

namespace Example.Scripts {
Expand Down
5 changes: 3 additions & 2 deletions sample-app/Assets/Example/Scripts/FullScreenExample.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections;
using Nimbus.Scripts;
using Nimbus.Scripts.Internal;
using Nimbus.Runtime.Scripts;
using Nimbus.Runtime.Scripts.Internal;
using UnityEngine;

namespace Example.Scripts {
Expand All @@ -26,6 +26,7 @@ private void OnTriggerEnter2D(Collider2D other) {
break;
}

// this is an example that uses the internal state of the ad and requires 0 event notifications
if (_ad.GetCurrentAdState() == AdEventTypes.LOADED ||
_ad.GetCurrentAdState() == AdEventTypes.IMPRESSION && !loaded) {
Debug.unityLogger.Log(
Expand Down
6 changes: 3 additions & 3 deletions sample-app/Assets/Example/Scripts/GUIButtonTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Nimbus.Scripts;
using Nimbus.Scripts.Internal;
using Nimbus.Runtime.Scripts;
using Nimbus.Runtime.Scripts.Internal;
using TMPro;
using UnityEngine;

Expand Down Expand Up @@ -48,7 +48,7 @@ public void LoadBanner() {


public void LoadInterstitial() {
_adUnit = NimbusManager.Instance.LoadAndShowFullScreenAd("unity_demo_interstitial_position",0.0f, 0.0f);
_adUnit = NimbusManager.Instance.LoadAndShowFullScreenAd("unity_demo_interstitial_position", 0.0f, 0.0f);
_isUnique = true;
}

Expand Down
86 changes: 30 additions & 56 deletions sample-app/Assets/Example/Scripts/NimbusEventListenerExample.cs
Original file line number Diff line number Diff line change
@@ -1,69 +1,43 @@
using System;
using Nimbus.Scripts.Internal;
using Nimbus.Runtime.Scripts.Internal;
using UnityEngine;

namespace Example.Scripts {
public class NimbusEventListenerExample : MonoBehaviour, IAdEvents {
public void AdWasRendered(NimbusAdUnit nimbusAdUnit) {
public void OnAdWasRendered(NimbusAdUnit nimbusAdUnit) {
Debug.unityLogger.Log(
$"NimbusEventListenerExample Ad was rendered for ad instance {nimbusAdUnit.InstanceID}");
$"NimbusEventListenerExample Ad was rendered for ad instance {nimbusAdUnit.InstanceID}");
}

public void AdError(NimbusAdUnit nimbusAdUnit) {
public void OnAdError(NimbusAdUnit nimbusAdUnit) {
Debug.unityLogger.Log($"NimbusEventListenerExample Err {nimbusAdUnit.ErrorMessage()}");
} // ReSharper disable SwitchStatementMissingSomeEnumCasesNoDefault
public void AdEvent(NimbusAdUnit nimbusAdUnit) {
Debug.unityLogger.Log(
$"NimbusEventListenerExample AdEvent {nimbusAdUnit.GetCurrentAdState()} for ad of type {nimbusAdUnit.AdType}");
switch (nimbusAdUnit.AdType) {
// Handle Events for Banner and Interstitial ads
case AdUnityType.Banner:
case AdUnityType.Interstitial:
switch (nimbusAdUnit.GetCurrentAdState()) {
case AdEventTypes.NOT_LOADED:
break;
case AdEventTypes.LOADED:
break;
case AdEventTypes.CLICKED:
break;
case AdEventTypes.IMPRESSION:
break;
case AdEventTypes.DESTROYED:
break;
}
}

public void OnAdLoaded(NimbusAdUnit nimbusAdUnit) {
// TODO
}

break;
// Handle Events for Rewarded video ads
case AdUnityType.Rewarded:
switch (nimbusAdUnit.GetCurrentAdState()) {
case AdEventTypes.NOT_LOADED:
break;
case AdEventTypes.LOADED:
break;
case AdEventTypes.PAUSED:
break;
case AdEventTypes.RESUME:
break;
case AdEventTypes.CLICKED:
break;
case AdEventTypes.FIRST_QUARTILE:
break;
case AdEventTypes.MIDPOINT:
break;
case AdEventTypes.THIRD_QUARTILE:
break;
case AdEventTypes.COMPLETED:
break;
case AdEventTypes.IMPRESSION:
break;
case AdEventTypes.DESTROYED:
break;
}
public void OnAdImpression(NimbusAdUnit nimbusAdUnit) {
// TODO
}

public void OnAdClicked(NimbusAdUnit nimbusAdUnit) {
// TODO
}

public void OnAdDestroyed(NimbusAdUnit nimbusAdUnit) {
// TODO
}

public void OnVideoAdPaused(NimbusAdUnit nimbusAdUnit) {
// TODO
}

public void OnVideoAdResume(NimbusAdUnit nimbusAdUnit) {
// TODO
}

break;
default:
throw new ArgumentOutOfRangeException();
}
public void OnVideoAdCompleted(NimbusAdUnit nimbusAdUnit) {
// TODO
}
}
}
69 changes: 33 additions & 36 deletions sample-app/Assets/Example/Scripts/RewardedVideoExample.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using Nimbus.Scripts;
using Nimbus.Scripts.Internal;
using Nimbus.Runtime.Scripts;
using Nimbus.Runtime.Scripts.Internal;
using UnityEngine;

namespace Example.Scripts {
Expand All @@ -25,44 +24,42 @@ private void OnTriggerEnter2D(Collider2D other) {
_alreadyTriggered = true;
}

public void AdWasRendered(NimbusAdUnit nimbusAdUnit) {
public void OnAdWasRendered(NimbusAdUnit nimbusAdUnit) {
Debug.unityLogger.Log(
$"NimbusEventListenerExample Ad was rendered for ad instance {nimbusAdUnit.InstanceID}, bid value: {nimbusAdUnit.GetBidValue()}, network: {nimbusAdUnit.GetNetwork()}, auction_id: {nimbusAdUnit.GetAuctionID()}");
}

public void AdError(NimbusAdUnit nimbusAdUnit) {
public void OnAdError(NimbusAdUnit nimbusAdUnit) {
Debug.unityLogger.Log($"NimbusEventListenerExample Err {nimbusAdUnit.ErrorMessage()}");
} // ReSharper disable SwitchStatementMissingSomeEnumCasesNoDefault
public void AdEvent(NimbusAdUnit nimbusAdUnit) {
if (nimbusAdUnit.AdType != AdUnityType.Rewarded) return;
switch (nimbusAdUnit.GetCurrentAdState()) {
case AdEventTypes.NOT_LOADED:
break;
case AdEventTypes.LOADED:
break;
case AdEventTypes.PAUSED:
break;
case AdEventTypes.RESUME:
break;
case AdEventTypes.CLICKED:
break;
case AdEventTypes.FIRST_QUARTILE:
break;
case AdEventTypes.MIDPOINT:
break;
case AdEventTypes.THIRD_QUARTILE:
break;
case AdEventTypes.COMPLETED:
Debug.unityLogger.Log("Rewarding the player for watching the whole video!");
UnityThread.ExecuteInUpdate(RewardUser);
break;
case AdEventTypes.IMPRESSION:
break;
case AdEventTypes.DESTROYED:
break;
default:
throw new ArgumentOutOfRangeException();
}
}

public void OnAdLoaded(NimbusAdUnit nimbusAdUnit) {
// TODO
}

public void OnAdImpression(NimbusAdUnit nimbusAdUnit) {
// TODO
}

public void OnAdClicked(NimbusAdUnit nimbusAdUnit) {
// TODO
}

public void OnAdDestroyed(NimbusAdUnit nimbusAdUnit) {
// TODO
}

public void OnVideoAdPaused(NimbusAdUnit nimbusAdUnit) {
// TODO
}

public void OnVideoAdResume(NimbusAdUnit nimbusAdUnit) {
// TODO
}

public void OnVideoAdCompleted(NimbusAdUnit nimbusAdUnit) {
Debug.unityLogger.Log("Rewarding the player for watching the whole video!");
UnityThread.ExecuteInUpdate(RewardUser);
}

private IEnumerator MakeItRain() {
Expand Down

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

Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
using System;

namespace Nimbus.Scripts.Internal {
namespace Nimbus.Runtime.Scripts.Internal {
public class AdEvents {
internal event Action<NimbusAdUnit> OnAdRendered;
internal event Action<NimbusAdUnit> OnAdError;

internal event Action<NimbusAdUnit> OnAdEvent;

internal event Action<NimbusAdUnit> OnAdLoaded;
internal event Action<NimbusAdUnit> OnAdImpression;
internal event Action<NimbusAdUnit> OnAdClicked;
internal event Action<NimbusAdUnit> OnAdDestroyed;
internal event Action<NimbusAdUnit> OnVideoAdPaused;
internal event Action<NimbusAdUnit> OnVideoAdResume;
internal event Action<NimbusAdUnit> OnVideoAdCompleted;

internal void EmitOnAdError(NimbusAdUnit obj) {
OnAdError?.Invoke(obj);
}

internal void EmitOnAdEvent(NimbusAdUnit obj) {
OnAdEvent?.Invoke(obj);
}

internal void EmitOnAdRendered(NimbusAdUnit obj) {
OnAdRendered?.Invoke(obj);
}

internal void EmitOnOnAdLoaded(NimbusAdUnit obj) {
OnAdLoaded?.Invoke(obj);
}

internal void EmitOnOnAdImpression(NimbusAdUnit obj) {
OnAdImpression?.Invoke(obj);
}

internal void EmitOnOnAdClicked(NimbusAdUnit obj) {
OnAdClicked?.Invoke(obj);
}

internal void EmitOnOnAdDestroyed(NimbusAdUnit obj) {
OnAdDestroyed?.Invoke(obj);
}

internal void EmitOnOnVideoAdPaused(NimbusAdUnit obj) {
OnVideoAdPaused?.Invoke(obj);
}

internal void EmitOnOnVideoAdResume(NimbusAdUnit obj) {
OnVideoAdResume?.Invoke(obj);
}

internal void EmitOnOnVideoAdCompleted(NimbusAdUnit obj) {
OnVideoAdCompleted?.Invoke(obj);
}
}


Expand Down
Loading

0 comments on commit 6d8d46c

Please sign in to comment.