uniwebview2.1使用
插件下载地址:
http://download.csdn.net/detail/onafioo/9576200
示例
public class BrowserMgr { private static UniWebView _webView; static GameObject BrowserGo; public static void Open(string url){ if(!CheckURL(url)){ Debug.Log("[N]---browser url error!! url:"+url); return; } if(null == BrowserGo)BrowserGo = new GameObject("browser"); _webView = BrowserGo.GetComponent<UniWebView>(); if (_webView == null) { _webView = BrowserGo.AddComponent<UniWebView>(); _webView.OnReceivedMessage += OnReceivedMessage; _webView.OnLoadComplete += OnLoadComplete; _webView.OnWebViewShouldClose += OnWebViewShouldClose; //_webView.OnEvalJavaScriptFinished += OnEvalJavaScriptFinished; _webView.InsetsForScreenOreitation += InsetsForScreenOreitation; } int bottomInset = (int)(UniWebViewHelper.screenHeight); _webView.insets = new UniWebViewEdgeInsets(0,0,0,0); _webView.url = url; //_webView.url = "http://uniwebview.onevcat.com/demo/index1-1.html"; //_webView.url = "http://192.168.12.110:8401/t.html"; //_webView.url = "http://www.sina.com.cn/"; _webView.SetSpinnerLabelText("载入中..."); _webView.SetShowSpinnerWhenLoading(true); Debug.Log("URL:"+_webView.url); ShowViewEventArgs e = new ShowViewEventArgs(ViewNames.HomeView.ToString(), true, true, true); App.Instance.EventManager.SendEvent(e); _webView.Load(); } /// <summary> /// 浏览器载入成功回调 /// </summary> /// <param name="webView">Web view.</param> /// <param name="success">If set to <c>true</c> success.</param> /// <param name="errorMessage">Error message.</param> static void OnLoadComplete(UniWebView webView, bool success, string errorMessage) { if (success) { Debug.Log("[N]---browser load complete!"); webView.Show(); } else { Debug.Log("[N]---Something wrong in webview loading: " + errorMessage); } } /// <summary> /// 页面标签信息回调 /// </summary> /// <param name="webView">Web view.</param> /// <param name="message">Message.</param> static void OnReceivedMessage(UniWebView webView, UniWebViewMessage message) { Debug.Log("[N]---"+message.rawMessage); //NTODO 处理下必要的页面标签返回信息 } /// <summary> /// 弹出Alert /// </summary> /// <param name="alert">Alert.</param> public void ShowAlertInWebview(string alert) {_webView.EvaluatingJavaScript(alert);} /// <summary> /// 浏览器关闭回调 /// </summary> /// <param name="webView">Web view.</param> static bool OnWebViewShouldClose(UniWebView webView) { Debug.Log("[N]---browser close!"); if (webView == _webView) { _webView = null; return true; } return false; } /// <summary> /// 横竖屏切换 /// </summary> /// <returns>The for screen oreitation.</returns> /// <param name="webView">Web view.</param> /// <param name="orientation">Orientation.</param> static UniWebViewEdgeInsets InsetsForScreenOreitation(UniWebView webView, UniWebViewOrientation orientation) { int bottomInset = (int)(UniWebViewHelper.screenHeight); if (orientation == UniWebViewOrientation.Portrait) return new UniWebViewEdgeInsets(0,0,bottomInset,0); else return new UniWebViewEdgeInsets(0,0,bottomInset,0); } /// <summary> /// 检测URL合法性 /// </summary> /// <returns><c>true</c>, if UR was checked, <c>false</c> otherwise.</returns> /// <param name="url">URL.</param> static bool CheckURL(string url){ bool b = false; b = Regex.IsMatch(url,"^http://[a-z0-9A-Z.-]*"); return b; } }