Integrating Unity+WebGL
(developer integration notes)
In Unity, create a .jslib file that will be merged into the WebGL project.
For example: Applixir.jslib
-------- cut -----------
var ApplixirPlugin = {
$impl: {},
adStatusCallbackX: function (status) {
//console.log('Ad Status:', status);
var iresult = 0;
switch (status)
{
case "ad-watched":
iresult = 1;
break;
case "network-error":
iresult = 2;
break;
case "ad-blocker":
iresult = 3;
break;
case "ad-interrupted":
iresult = 4;
break;
case "ads-unavailable":
iresult = 5;
break;
case "ad-fallback":
iresult = 6;
break;
default:
iresult = 0;
break;
}
Runtime.dynCall('vi', window.applixirCallback,
[iresult]);
//console.log('Ad Status done:', status);
},
ShowVideo__deps: [
'$impl',
'adStatusCallbackX'
],
ShowVideo: function (devId, gameId, zoneId, fallback,
callback) {
var local_options = {
zoneId: zoneId, // the zone ID from the "Games" page
devId: devId, // your developer ID from the
"Account" page
gameId: gameId, // the ID for this game from the
"Games" page
adStatusCb: _adStatusCallbackX, // optional
fallback: fallback, // 0|1
verbosity: 0 // 0..5
};
//console.log(local_options);
window.applixirCallback = callback;
invokeApplixirVideoUnit(local_options);
}
};
autoAddDeps(ApplixirPlugin, '$impl');
mergeInto(LibraryManager.library, ApplixirPlugin);
-------- cut -----------
Our 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.
Our C# code will define an export to enable access from C#. Using a wrapper like so:
-------- cut -----------
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class ApplixirWebGL
{
public delegate void SimpleCallback(int val);
[DllImport("__Internal")]
public static extern void ShowVideo(int devId, int gameId, int
zoneId, int fallback, SimpleCallback onCompleted);
[MonoPInvokeCallback(typeof(SimpleCallback))]
private static void ApplixirCompletedHandler(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.AD_FALLBACK;
break;
default:
pvr = PlayVideoResult.ADS_UNAVAILABLE;
break;
}
if (callback != null)
{
callback(pvr);
callback = null;
}
}
public enum PlayVideoResult
{
AD_WATCHED, // an ad was presented and ran for more than 5
seconds
AD_BLOCKER, // an ad blocker was detected
AD_INTERRUPTED, // ad was ended prior to 5 seconds
(abnormal end)
AD_FALLBACK, // fallback mode displayed a banner in
response to ads-unavailable. Will only occur if "fallback:1" is
set in the options.
NETWORK_ERROR, // no connectivity available
ADS_UNAVAILABLE // no ads were returned to the player
}
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.
///
/// </summary>
///
public static void PlayVideo(Action<PlayVideoResult> callback)
{
ApplixirWebGL.callback = callback;
ShowVideo(devId, gameId, zoneId, (fallback ? 1 : 0),
ApplixirCompletedHandler);
}
private static int devId;
private static int gameId;
private static int zoneId;
private static bool fallback;
public static void init(int devId, int gameId, int zoneId,
bool fallback)
{
ApplixirWebGL.devId = devId;
ApplixirWebGL.gameId = gameId;
ApplixirWebGL.zoneId = zoneId;
ApplixirWebGL.fallback = fallback;
}
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 "ad-fallback":
pvr = PlayVideoResult.AD_FALLBACK;
break;
default:
pvr = PlayVideoResult.ADS_UNAVAILABLE;
break;
}
}
if (callback != null)
{
callback(pvr);
callback = null;
}
}
}
-------- cut -----------
An example html wrapper looks like:
-------- cut -----------
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8">
<title>%UNITY_WEB_NAME%</title>
<link rel="shortcut icon" href="TemplateData/favicon.ico">
<link rel="stylesheet" href="TemplateData/style.css">
<script src="TemplateData/UnityProgress.js"></script>
<script src="%UNITY_WEBGL_LOADER_URL%"></script>
<script>
var gameInstance =
UnityLoader.instantiate("gameContainer",
"%UNITY_WEBGL_BUILD_URL%", {
onProgress: UnityProgress
});
</script>
</head>
<body>
<div class="webgl-content">
<div id="gameContainer" style="width: %UNITY_WIDTH%px;
height: %UNITY_HEIGHT%px"></div>
<!-- div class="footer">
<div class="webgl-logo"></div>
<div class="fullscreen"
onclick="gameInstance.SetFullscreen(1)"></div>
<div class="title">%UNITY_WEB_NAME%</div>
</div -->
<script type='text/javascript'
src="https://cdn.applixir.com/applixir.sdk2.2m.js"></script>
<div id="applixir_vanishing_div" hidden><iframe
id="applixir_parent"></iframe></div>
</div>
</body>
</html>
-------- cut -----------