Hybrid App Setup - Unity SDK

    Note

    Before proceeding with the hybrid app setup, install the Airbridge Unity SDK and Web SDK.

    You can configure the Airbridge Unity SDK to handle Airbridge-related tasks occurring within the in-app website of a hybrid app without modifying the website's code.

    Handle Web SDK Commands with the Unity SDK

    The Unity SDK can handle commands related to event transmission, device settings, and user settings that occur within the Web SDK. Before displaying the website in the web view, configure the Unity SDK using the Airbridge.CreateWebInterfaceScript and Airbridge.HandleWebInterfaceCommand functions.

    • webToken: This is the Web SDK token. Your Web SDK token can be found on the [Settings]>[Tokens] page in the Airbridge dashboard.

    • postMessageScript: This JavaScript code transmits the payload variable, which stores the command passed from the Web SDK to the Unity SDK, to the Unity area.

    • command: This command is passed from the Web SDK 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

    このページは役に立ちましたか?

    ご質問やご提案はありますか?