一、常見Unity中內嵌網頁實現方式:
1、UnityWebCore只支持windows
2、Unity-Webview支持Android,IOS
3、UniWebView支持mac os,Android,IOS,WP8(2.0以后)
二、UniWebView 根據手機平台調用相應的WebView組件來顯示網頁,支持和javascript的交互,不支持windows和editor上顯示。
1、下載並導入unitypackage
http://uniwebview.onevcat.com/
2、設置Webview,加載並顯示網頁
// Find the UniWebView component on the gameObject. // It is supposed you have already dragged this script to the same gameObject which UniWebView on. // Or you will get a null exception :( _webView = GetComponent<UniWebView>(); // Listen to some event of UniWebView _webView.OnLoadComplete += OnLoadComplete; _webView.OnReceivedMessage += OnReceivedMessage; _webView.OnEvalJavaScriptFinished += OnEvalJavaScriptFinished;
// Almost full screen but 5 points gap in each edge. _webView.insets = new UniWebViewEdgeInsets(5,5,5,5); // Set a url string to load _webView.url = "http://uniwebview.onevcat.com/demo/index.html";
// Tell the web view begin to load the url just set. _webView.Load(); // Then wait for the OnLoadComplete event //... // The listening method of OnLoadComplete method. void OnLoadComplete(UniWebView webView, bool success, string errorMessage) { if (success) { // Great, everything goes well. Show the web view now. webView.Show(); } else { // Oops, something wrong. Debug.LogError("Something wrong in web view loading: " + errorMessage); } }
3、網址解析
uniwebview://move?direction=up&distance=1會解析成
path = "move" args = { direction = "up", distance = "1" }
4、監聽消息
void OnReceivedMessage(UniWebView webView, UniWebViewMessage message) { Debug.Log(message.rawMessage); if (string.Equals(message.path, "move")) { // It is time to move! // In this example: // message.args["direction"] = "up" // message.args["distance"] = "1" } }
詳細使用方法參考:http://uniwebview.onevcat.com/manual