This C# code will call through to the ShowVideo() JS function, the ShowVideo() function will set the required options, save a reference to the callback method (back into unity) and then
invoke Applixir to show a video unit. The C# code will define an export to enable access from C#. Using a wrapper such as applixir.unity.cs:
-------- start -----------
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class ApplixirWebGL
{
// Make sure any optional values you may require such as custom are added to all call parameters
public delegate void SimpleCallback(int val);
[DllImport("__Internal")]
public static extern void ShowVideo(int accountId, int siteId, int
zoneId, int userId, SimpleCallback onCompleted);
[MonoPInvokeCallback(typeof(SimpleCallback))]
private static void ApplixirEventHandler(int result)
{
Debug.Log("GOT VIDEO RESULT CALLBACK: " + result);
PlayVideoResult pvr = PlayVideoResult.ADS_UNAVAILABLE;
switch (result)
{
case 1:
pvr = PlayVideoResult.AD_WATCHED;
break;
case 2:
pvr = PlayVideoResult.NETWORK_ERROR;
break;
case 3:
pvr = PlayVideoResult.AD_BLOCKER;
break;
case 4:
pvr = PlayVideoResult.AD_INTERRUPTED;
break;
case 5:
pvr = PlayVideoResult.ADS_UNAVAILABLE;
break;
case 6:
pvr = PlayVideoResult.CORS_ERROR;
break;
case 7:
pvr = PlayVideoResult.NO_ZONEID;
break;
case 8:
pvr = PlayVideoResult.AD_STARTED;
break;
case 9:
pvr = PlayVideoResult.SYS_CLOSING;
break;
case 10:
pvr = PlayVideoResult.AD_REWARDED;
break;
case 11:
pvr = PlayVideoResult.AD_READY;
break;
case 12:
pvr = PlayVideoResult.AD_REJECTED;
break;
case 13:
pvr = PlayVideoResult.AD_MAXIMUM;
break;
case 14:
pvr = PlayVideoResult.AD_VIOLATION;
break;
default:
pvr = PlayVideoResult.ADS_UNAVAILABLE;
break;
}
if (callback != null)
{
callback(pvr);
}
}
public enum PlayVideoResult
{
NONE, // for zero
AD_WATCHED, // an ad was presented and completed successfully
NETWORK_ERROR, // no connectivity available or similar fatal network error
AD_BLOCKER, // an ad blocker was detected. Notify the user they must allow ads to receive rewards.
AD_INTERRUPTED, // ad was ended abnormally and should not be rewarded
ADS_UNAVAILABLE, // no ads were returned to the player from the ad servers
CORS_ERROR, // A CORS error was returned (this should never happen unless you are not configured correctly)
NO_ZONEID, // There is no zoneId in the options
AD_STARTED, // A video ad has been started (IMA only)
SYS_CLOSING, // The system has completed all processes and is closing
AD_REWARDED, // (RMS only) The reward has been validated
AD_READY, // An IMA video ad has loaded (no processing required, not used for GPT)
AD_REJECTED, // (RMS only) You returned a 418 error code from your RMS endpoint (due to invalid data detected)
AD_MAXIMUM, // (RMS only) This user has already been granted the maximum rewards specified in your RMS settings
AD_VIOLATION // (RMS only) This user has maximum rate violations based on your RMS settings (they are likely scripting)
}
private static Action<PlayVideoResult> callback = null;
/// <summary>
/// Calls out to the applixir service to show a video ad.
///
/// Result is returned via the resultListerens event.
/// Make sure to uncomment any parameters you need and to add them to the ShowVideo call below
/// </summary>
///
public static void PlayVideo(Action<PlayVideoResult> callback)
{
ApplixirWebGL.callback = callback;
ShowVideo(accountId, siteId, zoneId, userId, ApplixirEventHandler);
}
private static int accountId; // from your applixir.com account information
private static int siteId; // from the applixir.com site settings in your account
private static int zoneId; // from the applixir.com site settings in your account
private static string userId; // Unique ID for current user. UUID4 is recommended by Google and advertisers. No PII
// private static string custom; // optional UUID or JSON for use of site data such as specific reward data
// These must be setup and initialized as required by each game length limited by browser parms
// private static int verbosity; // 0..5 logging verbosity
// private static string vSize; // see support documentation for available sizes
// private static string vSizeM; // to set separate size for mobile, see support documentation for available sizes
// private static string vpos; // "top", "middle" or "bottom"
// private static string vposM; // use if you want different pos for mobile - same as vpos
public static void init(int accountId, int siteId, int zoneId, int userId)
{
ApplixirWebGL.accountId = accountId;
ApplixirWebGL.siteId = siteId;
ApplixirWebGL.zoneId = zoneId;
ApplixirWebGL.userId = userId; // ID for current user, required by ad industry. UUID4 recommended and no PII
// ApplixirWebGL.custom = cust: // optional information for your app/site. Also sent to your RMS endpoint if used.
// ApplixirWebGL.verbosity = verbo; // logging verbosity
// ApplixirWebGL.vSize = vSize; // "640x480" (default), "640x360", "max" (use max avail space) or "rsize" (best fit for GPT rewarded)
// ApplixirWebGL.vSizeM = vSizeM; // allows different size for mobile
// ApplixirWebGL.vpos = vpos;
// ApplixirWebGL.vposM = vposM;
}
public static void onPlayVideoResultString(string result)
{
Debug.Log("GOT VIDEO RESULT CALLBACK: " + result);
PlayVideoResult pvr = PlayVideoResult.ADS_UNAVAILABLE;
if (!string.IsNullOrEmpty(result))
{
result = result.ToLower().Trim();
switch (result)
{
case "ad-watched":
pvr = PlayVideoResult.AD_WATCHED;
break;
case "network-error":
pvr = PlayVideoResult.NETWORK_ERROR;
break;
case "ad-blocker":
pvr = PlayVideoResult.AD_BLOCKER;
break;
case "ad-interrupted":
pvr = PlayVideoResult.AD_INTERRUPTED;
break;
case "ads-unavailable":
pvr = PlayVideoResult.ADS_UNAVAILABLE;
break;
case "cors-error":
pvr = PlayVideoResult.CORS_ERROR;
break;
case "no-zoneId":
pvr = PlayVideoResult.NO_ZONEID;
break;
case "ad-started":
pvr = PlayVideoResult.AD_STARTED;
break;
case "sys-closing":
pvr = PlayVideoResult.SYS_CLOSING;
break;
case "ad-rewarded":
pvr = PlayVideoResult.AD_REWARDED;
break;
case "ad-rejected":
pvr = PlayVideoResult.AD_REJECTED;
break;
case "ad-maximum":
pvr = PlayVideoResult.AD_MAXIMUM;
break;
case "ad-violation":
pvr = PlayVideoResult.AD_VIOLATION;
break;
case "ad-ready":
pvr = PlayVideoResult.AD_READY;
break;
default:
pvr = PlayVideoResult.ADS_UNAVAILABLE;
break;
}
}
if (callback != null)
{
callback(pvr);
}
}
}
--------- end -----------