You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.2 KiB
77 lines
2.2 KiB
using System.Text.RegularExpressions;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class WebViewController : MonoBehaviour
|
|
|
|
{
|
|
private UniWebView webView;
|
|
|
|
public static string LoginMessage { get; set; }
|
|
public static string Server { get; set; }
|
|
public static bool RememberMe { get; set; }
|
|
|
|
private void Start()
|
|
{
|
|
var webViewGameObject = new GameObject("UniWebView");
|
|
webView = webViewGameObject.AddComponent<UniWebView>();
|
|
webView.SetUserAgent(webView.GetUserAgent() + "app");
|
|
webView.Frame = new Rect(0, 0, Screen.width, Screen.height);
|
|
webView.OnOrientationChanged += (view, orientation) =>
|
|
{
|
|
webView.Frame = new Rect(0, 0, Screen.width, Screen.height);
|
|
};
|
|
webView.OnMessageReceived += (view, message) =>
|
|
{
|
|
if (message.Path == "init")
|
|
{
|
|
if (string.IsNullOrEmpty(LoginMessage))
|
|
{
|
|
webView.EvaluateJavaScript($"load()");
|
|
}
|
|
else
|
|
{
|
|
webView.EvaluateJavaScript($"load2('{LoginMessage}')");
|
|
LoginMessage = null;
|
|
}
|
|
}
|
|
else if (message.Path == "facelogin")
|
|
{
|
|
Server = UnityWebRequest.UnEscapeURL(Regex.Match(message.RawMessage, "Server=(.*)&").Groups[1].Value);
|
|
RememberMe = Regex.Match(message.RawMessage, "RememberMe=(.*)").Groups[1].Value == "true";
|
|
this.FaceLogin();
|
|
}
|
|
};
|
|
webView.OnShouldClose += (view) =>
|
|
{
|
|
webView = null;
|
|
return true;
|
|
};
|
|
//
|
|
webView.CleanCache();
|
|
webView.Load(UniWebViewHelper.StreamingAssetURLForPath("wwwroot/index.html"));
|
|
webView.Show();
|
|
}
|
|
|
|
private void OnRectTransformDimensionsChange()
|
|
{
|
|
webView.UpdateFrame();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
CloseWebView();
|
|
}
|
|
|
|
private void FaceLogin()
|
|
{
|
|
SceneManager.LoadScene(1);
|
|
}
|
|
|
|
private void CloseWebView()
|
|
{
|
|
Destroy(webView);
|
|
webView = null;
|
|
}
|
|
} |