Hybrid App Setup - Unity SDK (v4)

Notice

Install the Unity SDK and Web SDK respectively on the app and website before setting up the hybrid app.

You can set the Unity SDK to handle Airbridge related tasks occurring on the in-app website in hybrid apps without changing the website code.

Manage Web SDK Commands with the Unity SDK

Unity SDK can handle commands related to event transmission, device settings, and user settings that take place in the Web SDK. Set up using Airbridge.CreateWebInterfaceScript and Airbridge.HandleWebInterfaceCommand functions before displaying the website in the web view.

webToken is a Web SDK token. The Web SDK token can be checked in Airbridge dashboard under [Settings]>[Tokens].

postMessageScript is a JavaScript code that delivers the payload variable, which stores the command passed from the web SDK to the Unity SDK, to the Unity area.

command is the command received from the web SDK delivered to the Unity SDK.

123456789101112131415161718192021222324252627282930313233343536373839404142
WebViewObject webViewObject;
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;
}}";

	postMessageScript = Airbridge.CreateWebInterfaceScript("YOUR_WEB_SDK_TOKEN", PostMessageGenerator("payload"));

	webViewObject.Init(
		cb: (msg) =>
		{
			// do something
    
			// Allow the Airbridge Unity SDK to process the forwarded messages from the WebView. 
			string command = msg;
			Airbridge.HandleWebInterfaceCommand(command);
		},
		err: (msg) => { /* do something */ },
		httpErr: (msg) => { /* do something */ },
		started: (msg) => { /* do something */ },
		hooked: (msg) => { /* do something */ },
		ld: (msg) =>
		{
			// do something
         
			webViewObject.EvaluateJS(postMessageScript);
		}
	);

	webViewObject.LoadURL("https://...");
}

Attention

Beware following.

Was this page helpful?

Have any questions or suggestions?