Unity SDK

What you need

To use the Metica Unity SDK you need:

  • An API key, obtainable in the Metica platform

  • An appId, obtainable in the Metica platform.

Installation

In Unity, open the Package Manager (Window > Package Manager) and click on the '+' button in the top left corner. Select "Add package from git URL..." and enter the following:

https://github.com/meticalabs/metica-unity-sdk.git

How to use the Metica SDK

The recommended way to use the SDK is through MeticaSdk which offers async/await methods alongside coroutine ones.

For backwards compatibility the old deprecated mechanism is still available which has only the coroutine approach: MeticaAPI

You can retrieve an instance of IMeticaSdk with

private IMeticaSdk _sdk = MeticaSdk.SDK;

Setup

The SDK configuration exposes the following parameters:

Property
Description

apiKey

Your API key

appId

The application identifier accessible from Metica's dashboard.

userId

A string that identifies a user. This can change during the lifetime of your app/game so, for example and depending on your needs, this could be a temporary id that later becomes a specific userId, or it can be the current user's id if it's already identified.

Interstitial Ads

Interstitial ads are full-screen or full-page ads that temporarily cover an app’s interface. They’re typically shown at natural pauses or transition points, such as after completing a level in a game or when navigating between major views.

The following sections show you how to load and then show an interstitial ad.

Loading an Interstitial ad

The following code shows you how to attach listeners and load the first interstitial ad:

private void InitializeInterstitialAds() {
    // Attach callbacks
    MeticaAdsCallbacks.Interstitial.OnAdLoadSuccess += OnInterstitialLoadedEvent;
    MeticaAdsCallbacks.Interstitial.OnAdLoadFailed += OnInterstitialFailedEvent;
    MeticaAdsCallbacks.Interstitial.OnAdShowSuccess += OnInterstitialDisplayedEvent;
    MeticaAdsCallbacks.Interstitial.OnAdShowFailed += InterstitialFailedToDisplayEvent;
    MeticaAdsCallbacks.Interstitial.OnAdClicked += OnInterstitialClickedEvent;
    MeticaAdsCallbacks.Interstitial.OnAdHidden += OnInterstitialDismissedEvent;
    // Load the first interstitial
    LoadInterstitial();
}
void LoadInterstitial() {
    if (IsMeticaAdsEnabled) 
    {
        MeticaAds.LoadInterstitial(); 
    } 
    else
    { 
        MaxSdk.LoadInterstitial(InterstitialAdUnitId); 
    }
}
private void OnInterstitialLoadedEvent(string adUnitId) {
    // Interstitial ad is ready to be shown. MeticaAds.IsInterstitialReady() will now return 'true'
}
private void OnInterstitialFailedEvent(string adUnitId, string error) {
    // Interstitial ad failed to load. We recommend retrying with exponentially higher delay.
}
private void OnInterstitialDisplayedEvent(string adUnitId) {
    Debug.Log($"Interstitial shown {adUnitId}");
}
private void InterstitialFailedToDisplayEvent(string adUnitId, string error) {
    // Interstitial ad failed to display. We recommend loading the next ad
    Debug.Log("Interstitial failed to display: " + error);
    LoadInterstitial();
}
private void OnInterstitialClickedEvent(string adUnitId) {
    Debug.Log($"Interstitial clicked {adUnitId}");
}
private void OnInterstitialDismissedEvent(string adUnitId) {
    // Interstitial ad is hidden. Pre-load the next ad
    Debug.Log("Interstitial dismissed");
    LoadInterstitial();
}

Showing an Interstitial Ad

To show an interstitial ad, call ShowInterstitial():

void ShowInterstitial()
{
    if (IsMeticaAdsEnabled) {
        if (MeticaAds.IsInterstitialReady())
        {
            MeticaAds.ShowInterstitial();
        }
        else
        {
            interstitialStatusText.text = "Ad not ready";
        }
    }
    else
    {
        if (MaxSdk.IsInterstitialReady(InterstitialAdUnitId)) 
        {
            intersitialStatusText.text = "Showing";
            MaxSdk.ShowInterstitial(InterstitiaAdUnitId);
        }
        else 
        {
            interstitialStatusText.text = "Ad not ready";
        }
    }
}

Rewarded Ads

Rewarded ads are video ads that users can choose to watch in exchange for in-app rewards.

Loading a Rewarded Ad

The InitializeRewardedAds method attaches listeners for both Metica and MAX SDK rewarded ad callbacks and then loads the first rewarded ad.

private void InitializeRewardedAds() 
{
	if (IsMeticaAdsEnabled)
	{
		MeticaAdsCallbacks.Rewarded.OnAdLoadSuccess += OnRewarderAdLoadedEvent;
		MeticaAdsCallbacks.Rewarded.OnAdLoadFailed += OnRewardedAdFailedEvent;
		MeticaAdsCallbacks.Rewarded.OnAdShowSuccess += OnRewardedAdDisplayedEvent;
		MeticaAdsCallbacks.Rewarded.OnAdShowFailed += OnRewardedAdFailedToDisplayEvent;
		MeticaAdsCallbacks.Rewarded.OnAdClicked += OnRewardedAdClickedEvent;
		MeticaAdsCallbacks.Rewarded.OnAdHidden += OnRewardedAdDismissedEvent;
		MeticaAdsCallbacks.Rewarded.OnAdRewarded += OnRewardedAdReceivedRewardEvent;
	}
	else
	{
       	MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += (adUnitId, adInfo) => OnRewardedAdLoadedEvent(adUnitId); 
       	MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += (adUnitId, errorInfo) => OnRewardedAdFailedEvent(adUnitId, errorInfo.Message); 
		MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += (adUnitId, errorInfo, adInfo) => OnRewardedAdFailedToDisplayEvent(adUnitId, errorInfo.Message); 
		MaxSdkCallbacks.Rewarded.OnAdDisplayedEvent += (adUnitId, adInfo) => OnRewardedAdDisplayedEvent(adUnitId);
		MaxSdkCallbacks.Rewarded.OnAdClickedEvent += (adUnitId, adInfo) => OnRewardedAdClickedEvent(adUnitId);
		MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += (adUnitId, adInfo) => OnRewardedAdDismissedEvent(adUnitId);
		MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += (adUnitId, reward, adInfo) => OnRewardedAdReceivedRewardEvent(adUnitId);
		MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnRewardedAdRevenuePaidEvent;
	}

	// Load the first RewardedAd
	LoadRewardedAd();
}

private void LoadRewardedAd()
{
	rewardedStatusText.text = "Loading..."; 
	if (IsMeticaAdsEnabled)
	{
		MeticaAds.LoadRewarded(); 
	}
	else
	{
		MaxSdk.LoadRewardedAd(RewardedAdUnitId);
	}
}
private void OnRewardedAdLoadedEvent(string adUnitId)
{
	Debug.Log("Rewarded ad loaded");
}

private void OnRewardedAdDisplayedEvent(string adUnitId)
{
	Debug.Log($"Rewarded ad displayed {adUnitId}"); 
}
private void OnRewardedAdFailedToDisplayEvent(string adUnitId, string error)
{
	// Rewarded ad failed to display 
	Debug.Log("Rewarded ad failed to display: " + error);
	LoadRewardedAd();
}

private void OnRewardedAdClickedEvent(string adUnitId)
{
	Debug.Log($"Rewarded ad clicked {adUnitId}");
}

private void OnRewardedAdDismissedEvent(string adUnitId)
{
	// Rewarded ad is hidden Debug.Log("Rewarded ad dismissed"); 
	LoadRewardedAd();
}

private void OnRewardedAdReceivedRewardEvent(string adUnitId)
{
	// Rewarded ad was displayed and user should receive the reward Debug.Log("Rewarded ad received reward"); 
	// Implement your reward logic here
}

Showing a Rewarded Ad

To show an interstitial ad, call ShowRewarded():

void ShowRewardedAd()
{
	if (IsMeticaAdsEnabled)
	{
		if (MeticaAds.IsRewardedReady())
		{
			rewardedStatusText.text = "Showing";
			MeticaAds.ShowRewarded();
		}
		else
		{
			rewardedStatusText.text = "Ad not ready";
		}
	}
	else 
	{
		if (MaxSdk.IsRewardedAdReady(RewardedAdUnitId))
		{
			rewardedStatusText.text = "Showing";
			MaxSdk.ShowRewardedAd(RewardedAdUnitId);
		}
		else
		{
			rewardedSWtatusText.text = "Ad not ready";
		}
	}
}

Purchase Event

To enhance the integration experience, it's crucial to share additional information with Metica, such as in-app purchase details. The Metica SDK offers a method to log a purchase event. This event includes the product's unique ID, the currency code, the total amount, and an optional custom payload for developers to add personalized data.

void LogOfferPurchaseEventWithProductId(string productId, string currencyCode, double totalAmount, Dictionary<string, object> customPayload = null);

Last updated

Was this helpful?