C# 使用UniWebView只能访问网页一次
这是我的代码,我正在尝试使用UniWebView在UIView中打开网页。网页只打开一次。无法再次打开它。我正在用一个按钮打开页面。按钮上附有此纸条和UniWebViewC# 使用UniWebView只能访问网页一次,c#,unity3d,uniwebview,C#,Unity3d,Uniwebview,这是我的代码,我正在尝试使用UniWebView在UIView中打开网页。网页只打开一次。无法再次打开它。我正在用一个按钮打开页面。按钮上附有此纸条和UniWebView using UnityEngine; using System.Collections; using System.Net; using System.IO; public class InternetCheck : MonoBehaviour { UniWebView _webView; void Awake()
using UnityEngine;
using System.Collections;
using System.Net;
using System.IO;
public class InternetCheck : MonoBehaviour
{
UniWebView _webView;
void Awake()
{
_webView = GetComponent<UniWebView> ();
}
void Start()
{
_webView.OnLoadComplete += OnLoadComplete;
_webView.OnWebViewShouldClose += OnWebViewShouldClose;
}
void OnLoadComplete(UniWebView webView, bool success,string errorMessage)
{
if(success)
{
webView.Show();
}
else
{
Debug.LogError("Unable to load");
}
webView.ShowToolBar (true);
}
void BtnClicked()
{
if (_webView == null)
{
_webView = GetComponent<UniWebView>();
}
if(isInternetAvailable())
{
_webView.insets = new UniWebViewEdgeInsets(0,0,0,0);
_webView.url = "http://google.com";
_webView.Load();
}
else
if(!isInternetAvailable())
{
_webView.insets = new UniWebViewEdgeInsets(0,0,0,0);
_webView.url = Application.streamingAssetsPath + "/Privacy Policy _ Terms of Use _ Cartoon Network.html";
_webView.Load();
}
}
bool OnWebViewShouldClose(UniWebView webView) {
if (webView == _webView) {
_webView = null;
return true;
}
return false;
}
public static bool isInternetAvailable()
{
string HtmlText = GetHtmlFromUri("http://google.com");
if(HtmlText == "")
{
//MNAndroidMessage.Create(Const.NO_NETWORK_ALERT_TITLE, Const.NO_NETWORK_ALERT_MESSAGE);
// Debug.Log (" Please check your internet conection ");
return false;
}
else if(!HtmlText.Contains("schema.org/WebPage"))
{
//MNAndroidMessage.Create(Const.NETWORK_LOGIN_ALERT_TITLE, Const.NETWORK_LOGIN_ALERT_MESSAGE);
// Debug.Log (" Please check your internet conection might be you need to password to connect");
return false;
}
else
{
// Debug.Log("Network available ");
return true;
}
}
public static string GetHtmlFromUri(string resource)
{
string html = string.Empty;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(resource);
try
{
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
bool isSuccess = (int)resp.StatusCode < 299 && (int)resp.StatusCode >= 200;
if (isSuccess)
{
using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
{
//We are limiting the array to 80 so we don't have
//to parse the entire html document feel free to
//adjust (probably stay under 300)
char[] cs = new char[80];
reader.Read(cs, 0, cs.Length);
foreach(char ch in cs)
{
html +=ch;
}
}
}
}
}
catch
{
return "";
}
return html;
}
}
使用UnityEngine;
使用系统集合;
Net系统;
使用System.IO;
公共类InternetCheck:MonoBehavior
{
UniWebView webView;
无效唤醒()
{
_webView=GetComponent();
}
void Start()
{
_webView.OnLoadComplete+=OnLoadComplete;
_webView.OnWebViewShouldClose+=OnWebViewShouldClose;
}
void OnLoadComplete(UniWebView webView、bool成功、字符串错误消息)
{
如果(成功)
{
webView.Show();
}
其他的
{
Debug.LogError(“无法加载”);
}
webView.ShowToolBar(true);
}
void BtnClicked()
{
如果(_webView==null)
{
_webView=GetComponent();
}
如果(isInternetAvailable())
{
_webView.insets=新的UniWebViewEdgeInsets(0,0,0,0);
_webView.url=”http://google.com";
_Load();
}
其他的
如果(!isInternetAvailable())
{
_webView.insets=新的UniWebViewEdgeInsets(0,0,0,0);
_webView.url=Application.streamingAssetsPath+“/Privacy Policy uu使用条款uu卡通网络.html”;
_Load();
}
}
bool onwebview应关闭(UniWebView webView){
如果(网络视图==\u网络视图){
_webView=null;
返回true;
}
返回false;
}
公共静态bool可跨网络使用()
{
字符串HtmlText=GetHtmlFromUri(“http://google.com");
如果(HtmlText==“”)
{
//MNAndroidMessage.Create(Const.NO\u NETWORK\u ALERT\u TITLE,Const.NO\u NETWORK\u ALERT\u MESSAGE);
//Log(“请检查您的internet连接”);
返回false;
}
如果(!HtmlText.Contains(“schema.org/WebPage”),则为else
{
//MNAndroidMessage.Create(Const.NETWORK\u LOGIN\u ALERT\u TITLE,Const.NETWORK\u LOGIN\u ALERT\u MESSAGE);
//Log(“请检查您的internet连接可能需要密码才能连接”);
返回false;
}
其他的
{
//Debug.Log(“网络可用”);
返回true;
}
}
公共静态字符串GetHtmlFromUri(字符串资源)
{
string html=string.Empty;
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(资源);
尝试
{
使用(HttpWebResponse resp=(HttpWebResponse)req.GetResponse())
{
bool issucess=(int)resp.StatusCode<299&(int)resp.StatusCode>=200;
如果(isSuccess)
{
使用(StreamReader=newstreamreader(resp.GetResponseStream())
{
//我们将数组限制为80,因此没有
//要解析整个html文档,请随意
//调整(可能保持在300以下)
char[]cs=新字符[80];
reader.Read(cs,0,cs.Length);
foreach(cs中的字符ch)
{
html+=ch;
}
}
}
}
}
抓住
{
返回“”;
}
返回html;
}
}
解决了这个问题。只需在移除UniWebView组件时将其添加回去。旁注:最好有指向“UniWebView”类的链接,并再次检查“unity”(DI框架)是否是正确的标记(可能您指的是“unity3d”)是的,我会更改它。我想出来了。单击按钮时,我只需要重新分配UniWebView组件。