Skip to content

Instantly share code, notes, and snippets.

@abeldantas
Created March 2, 2017 11:57
Show Gist options
  • Save abeldantas/3febda307c724b1d32f38c853c4f9653 to your computer and use it in GitHub Desktop.
Save abeldantas/3febda307c724b1d32f38c853c4f9653 to your computer and use it in GitHub Desktop.
Hit'n'run - Showing ads with supersonic
using System;
using UnityEngine;
public class AdManager : GameObjectSingleton<AdManager>
{
public string appKey = "3fbb9d89";
bool hasRewardBeenGiven;
Action onSuccess;
Action onCanceled;
public override void Awake()
{
base.Awake();
DontDestroyOnLoad( gameObject );
}
public void Start()
{
Supersonic.Agent.start();
string uniqueUserId = SystemInfo.deviceUniqueIdentifier;
Supersonic.Agent.initRewardedVideo( appKey, uniqueUserId );
SupersonicEvents.onRewardedVideoAdRewardedEvent += RewardedVideoAdRewardedEvent;
SupersonicEvents.onRewardedVideoAdClosedEvent += RewardedVideoAdClosedEvent;
}
public void Load()
{
}
void RewardedVideoAdClosedEvent()
{
if ( hasRewardBeenGiven )
{
onSuccess();
}
else
{
onCanceled();
}
}
void RewardedVideoAdRewardedEvent( SupersonicPlacement obj )
{
hasRewardBeenGiven = true;
}
public void ShowVideoAd( Action adOnSuccess, Action onFailure, Action AdOnCanceled )
{
if ( IsAdAvailable() )
{
hasRewardBeenGiven = false;
this.onSuccess = adOnSuccess;
this.onCanceled = AdOnCanceled;
Supersonic.Agent.showRewardedVideo();
}
else
{
onFailure();
}
}
public bool IsAdAvailable()
{
#if UNITY_EDITOR
return true;
#else
return Supersonic.Agent.isRewardedVideoAvailable();
#endif
}
}
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class DebugUI : MonoBehaviour
{
public Button WatchAdButton;
public Text Title;
public void Start()
{
SupersonicEvents.onRewardedVideoInitSuccessEvent += RewardedVideoInitSuccessEvent;
SupersonicEvents.onVideoAvailabilityChangedEvent += VideoAvailabilityChangedEvent;
WatchAdButton.interactable = false;
AdManager.Instance.Load();
StartCoroutine( CheckForAdAvailabilityEverySecond() );
}
public IEnumerator CheckForAdAvailabilityEverySecond()
{
while ( true )
{
if ( AdManager.Instance.IsAdAvailable() )
{
WatchAdButton.interactable = true;
Title.text = "Ads ARE available";
Camera.main.backgroundColor = Color.green;
}
else
{
Title.text = "Ads not available";
Camera.main.backgroundColor = Color.white;
WatchAdButton.interactable = false;
}
yield return new WaitForSeconds( 1f );
}
}
void RewardedVideoInitSuccessEvent()
{
Debug.Log( "RewardedVideoInitSuccessEvent" );
}
void VideoAvailabilityChangedEvent( bool available )
{
//Change the in-app 'Traffic Driver' state according to availability.
bool rewardedVideoAvailability = available;
Debug.Log( string.Format( "RewardedVideo is {0}", rewardedVideoAvailability ) );
}
public void WatchAdButtonListener()
{
if ( AdManager.Instance.IsAdAvailable() )
{
Debug.Log( "Ad is available" );
}
else
{
Debug.Log( "Ad isnt available" );
}
AdManager.Instance.ShowVideoAd(
() => { Debug.Log( "adOnSuccess" ); },
() => { Debug.Log( "onFailure" ); },
() => { Debug.Log( "AdOnCanceled" ); } );
}
}
@abeldantas
Copy link
Author

The SupersonicEventsPrefab is on the scene, events are picked up.
Problem is that with Unity Ads the adOnSuccess action is called instead of the onCanceled action. Unlike with other ad networks where the adOnSuccess is called upon closing the ad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment