Unity 하이브리드 앱 연동 가이드
DashboardUser GuideDataspecStatus Page

Unity 하이브리드 앱 연동 가이드

개요


Unity 하이브리드 앱 연동 가이드에 대한 내용입니다. Unity 에서 WebView 를 사용하기 위해 다양한 플러그인을 사용 할 수 있습니다. 각각의 플러그인 마다 하이브리드 앱 연동 코드 형태가 다르므로 Javascript Interface 를 활용하기 위한 메서드를 제공합니다.

interface

AirbridgeWebInterface webInterface; webInterface = AirbridgeUnity.CreateWebInterface( "YOUR_WEB_TOKEN", // web token (msg) => $@"..." // post command function );

📘

YOUR_WEB_TOKEN 은 대시보드의 Settings > Tokens > 웹 SDK 토큰 에서 확인할 수 있습니다.

public interface AirbridgeWebInterface { string Script { get; } void Handle(string message); }

이용가이드

아래는 많이 사용되는 plugin 3가지에 대한 연동에 대한 내용입니다.

  • gree/unity-webview
  • UniWebView
  • GPM (Game Package Manger)

gree/unity-webview 바로가기

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 // WebView에서 Unity로 전달된 메세지를 Airbridge Unity SDK에서 처리할 수 있도록 설정한다. 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 바로가기

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) => { // WebView에서 Unity로 전달된 메세지를 Airbridge Unity SDK에서 처리할 수 있도록 설정한다. 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(); }

GPM (Game Package Manger) 바로가기

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); // WebView에서 Unity로 전달된 메세지를 Airbridge Unity SDK에서 처리할 수 있도록 설정한다. 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; ... } }