Unity Hybrid App Integration Guide
Unity Hybrid App Integration
There are several plugins that can be used for webviews in Unity. Since each plugin have different ways to integrate with hybrid apps, Airbridge provides methods to utilize the plugin through a JavaScript interface.
Interface
AirbridgeWebInterface webInterface;
webInterface = AirbridgeUnity.CreateWebInterface(
"YOUR_WEB_TOKEN", // web token
(msg) => $@"..." // post command function
);
YOUR_WEB_TOKEN
can be found at the Airbridge dashboard β Settings β Tokens β Web SDK.
public interface AirbridgeWebInterface
{
string Script { get; }
void Handle(string message);
}
Guide
Below are integration guides for three commonly used plugins.
- gree/unity-webview
- UniWebView
- GPM (Game Package Manger)
WebViewObject webViewObject;
AirbridgeWebInterface webInterface;
string postMessageScript;
public void Display()
{
string PostMessageGenerator(string arg) =>
$@"
if (window && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unityControl) {{
window.webkit.messageHandlers.unityControl.postMessage({arg});
}} else {{
var iframe = document.createElement('IFRAME');
iframe.setAttribute('src', 'unity:' + {arg});
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}}";
webInterface = AirbridgeUnity.CreateWebInterface("YOUR_WEB_TOKEN", (string command) => PostMessageGenerator(command));
postMessageScript = webInterface.Script;
webViewObject.Init(
cb: (msg) =>
{
// do something
// Allow the Airbridge Unity SDK to process the forwarded messages from the WebView.
webInterface.Handle(msg);
},
err: (msg) => { /* do something */ },
httpErr: (msg) => { /* do something */ },
started: (msg) => { /* do something */ },
hooked: (msg) => { /* do something */ },
ld: (msg) =>
{
// do something
webViewObject.EvaluateJS(postMessageScript);
},
...
);
...
}
UniWebView webView;
AirbridgeWebInterface webInterface;
string postMessageScript;
public void Display()
{
string PostMessageGenerator(string arg) => $@"window.location.href = 'uniwebview://airbridge?command=' + {arg}";
webInterface = AirbridgeUnity.CreateWebInterface("YOUR_WEB_TOKEN", (string payload) => PostMessageGenerator(payload));
postMessageScript = webInterface.Script;
webView.OnMessageReceived += (view, message) =>
{
// Allow the Airbridge Unity SDK to process the forwarded messages from the WebView.
if (message.Path.Equals("airbridge"))
{
string command = message.Args["command"];
webInterface.Handle(command);
}
};
webView.OnPageFinished += (view, statusCode, url) =>
{
webViewObject.EvaluateJavaScript(postMessageScript, (payload) =>
{
if (payload.resultCode.Equals("0"))
{
Debug.Log("Airbridge javascript injection succeeded");
}
else
{
Debug.Log("Airbridge javascript injection failed: " + payload.data);
}
});
};
...
webView.Show();
}
AirbridgeWebInterface webInterface;
string postMessageScript;
public void Display()
{
string PostMessageGenerator(string arg) => $@"window.location.href = 'airbridge://?command=' + {arg}";
webInterface = AirbridgeUnity.CreateWebInterface("YOUR_WEB_TOKEN", (string payload) => PostMessageGenerator(payload));
postMessageScript = webInterface.Script;
GpmWebView.ShowUrl(
"WEB_URL",
new GpmWebViewRequest.Configuration() { ... },
OnCallback,
new List<string>()
{
"airbridge" // PUT AIRBRIDGE SCHEME HERE
}
);
}
private void OnCallback(
GpmWebViewCallback.CallbackType callbackType,
string data,
GpmWebViewError error
)
{
switch (callbackType)
{
case GpmWebViewCallback.CallbackType.PageLoad:
if (string.IsNullOrEmpty(data) == false)
{
// do something
GpmWebView.ExecuteJavaScript(postMessageScript);
}
break;
case GpmWebViewCallback.CallbackType.Scheme:
if (error == null)
{
Uri uri = new Uri(data);
// Allow the Airbridge Unity SDK to process the forwarded messages from the WebView.
if (uri.Scheme.Equals("airbridge"))
{
try
{
string encodedCommand = HttpUtility.ParseQueryString(uri.Query)["command"];
string command = HttpUtility.UrlDecode(encodedCommand);
webInterface.Handle(command);
}
catch (Exception e)
{
Debug.LogFormat("Fail to receive command. Error: {0}", e.Message);
}
}
}
else
{
Debug.LogFormat("Fail to custom scheme. Error:{0}", error);
}
break;
...
}
}