Unity中内嵌网页插件UniWebView

一、常见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

posted @   DamonXu  阅读(16368)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示