This commit is contained in:
2020-07-04 14:41:25 +08:00
parent 70c346d2c1
commit a8f02e4da5
3748 changed files with 587372 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Example for Custom header features.
/// If you want to try custom headers, put this class as a component to the scene.
/// </summary>
public class SampleCustomHeader : MonoBehaviour
{
const float BUTTON_HEIGHT = 50.0f;
const string CUSTOM_HEADER_KEY_NAME = "custom_timestamp";
WebViewObject _webviewObject;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
float h = Screen.height;
if (GUI.Button(new Rect(.0f, h - BUTTON_HEIGHT, Screen.width, BUTTON_HEIGHT), "check for request header"))
{
this._webviewObject = GameObject.Find("WebViewObject").GetComponent<WebViewObject>();
this._webviewObject.LoadURL("http://httpbin.org/headers");
}
h -= BUTTON_HEIGHT;
if (GUI.Button(new Rect(.0f, h - BUTTON_HEIGHT, Screen.width, BUTTON_HEIGHT), "add custom header"))
{
this._webviewObject = GameObject.Find("WebViewObject").GetComponent<WebViewObject>();
this._webviewObject.AddCustomHeader(CUSTOM_HEADER_KEY_NAME, System.DateTime.Now.ToString());
}
h -= BUTTON_HEIGHT;
if (GUI.Button(new Rect(.0f, h - BUTTON_HEIGHT, Screen.width, BUTTON_HEIGHT), "get custom header"))
{
this._webviewObject = GameObject.Find("WebViewObject").GetComponent<WebViewObject>();
Debug.Log("custom_timestamp is " + this._webviewObject.GetCustomHeaderValue(CUSTOM_HEADER_KEY_NAME));
}
h -= BUTTON_HEIGHT;
if (GUI.Button(new Rect(.0f, h - BUTTON_HEIGHT, Screen.width, BUTTON_HEIGHT), "remove custom header"))
{
this._webviewObject = GameObject.Find("WebViewObject").GetComponent<WebViewObject>();
this._webviewObject.RemoveCustomHeader(CUSTOM_HEADER_KEY_NAME);
}
h -= BUTTON_HEIGHT;
if (GUI.Button(new Rect(.0f, h - BUTTON_HEIGHT, Screen.width, BUTTON_HEIGHT), "clear custom header"))
{
this._webviewObject = GameObject.Find("WebViewObject").GetComponent<WebViewObject>();
this._webviewObject.ClearCustomHeader();
}
h -= BUTTON_HEIGHT;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 41042fdda5d9347c2b7f7aed789c6b79
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,266 @@
/*
* Copyright (C) 2012 GREE, Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Coolape;
public class UWebView : MonoBehaviour
{
public string Url;
//public Text status;
WebViewObject webViewObject;
public void init(object onCallFromJS, object onCallOnError, object onCallOnStarted, object onCallOnLoaded)
{
if (webViewObject != null) return;
webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>();
webViewObject.Init(
cb: (msg) =>
{
Debug.Log(string.Format("CallFromJS[{0}]", msg));
//status.text = msg;
//status.GetComponent<Animation>().Play();
Utl.doCallback(onCallFromJS, msg);
},
err: (msg) =>
{
Debug.Log(string.Format("CallOnError[{0}]", msg));
//status.text = msg;
//status.GetComponent<Animation>().Play();
Utl.doCallback(onCallOnError, msg);
},
started: (msg) =>
{
Debug.Log(string.Format("CallOnStarted[{0}]", msg));
Utl.doCallback(onCallOnStarted, msg);
},
ld: (msg) =>
{
Debug.Log(string.Format("CallOnLoaded[{0}]", msg));
Utl.doCallback(onCallOnLoaded, msg);
#if UNITY_EDITOR_OSX || !UNITY_ANDROID
// NOTE: depending on the situation, you might prefer
// the 'iframe' approach.
// cf. https://github.com/gree/unity-webview/issues/189
#if true
webViewObject.EvaluateJS(@"
if (window && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unityControl) {
window.Unity = {
call: function(msg) {
window.webkit.messageHandlers.unityControl.postMessage(msg);
}
}
} else {
window.Unity = {
call: function(msg) {
window.location = 'unity:' + msg;
}
}
}
");
#else
webViewObject.EvaluateJS(@"
if (window && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unityControl) {
window.Unity = {
call: function(msg) {
window.webkit.messageHandlers.unityControl.postMessage(msg);
}
}
} else {
window.Unity = {
call: function(msg) {
var iframe = document.createElement('IFRAME');
iframe.setAttribute('src', 'unity:' + msg);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}
}
}
");
#endif
#endif
webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)");
},
//ua: "custom user agent string",
enableWKWebView: true);
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
webViewObject.bitmapRefreshCycle = 1;
#endif
//webViewObject.SetAlertDialogEnabled(false);
webViewObject.SetMargins(5, 100, 5, Screen.height / 4);
webViewObject.SetVisibility(true);
}
public void setVisibility(bool val)
{
if (webViewObject != null)
{
webViewObject.SetVisibility(val);
}
}
public void setMargins(int left, int top, int right, int bottom)
{
if (webViewObject != null)
{
webViewObject.SetMargins(left, top, right, bottom);
}
}
public float progress {
get {
if (webViewObject == null) return 0;
return webViewObject.Progress();
}
}
public void loadUrl(string url)
{
if (webViewObject == null) return;
this.Url = url;
StartCoroutine(LoadURL());
}
IEnumerator LoadURL()
{
yield return null;
#if !UNITY_WEBPLAYER && !UNITY_WEBGL
if (Url.StartsWith("http") || Url.StartsWith("file:"))
{
webViewObject.LoadURL(Url.Replace(" ", "%20"));
}
else
{
var exts = new string[]{
".jpg",
".js",
".html" // should be last
};
foreach (var ext in exts)
{
//var url = Url.Replace(".html", ext);
//var src = System.IO.Path.Combine(Application.streamingAssetsPath, url);
#if UNITY_EDITOR
var dst = System.IO.Path.Combine(Application.dataPath, Url);
#else
var dst = System.IO.Path.Combine(Application.persistentDataPath, Url);
#endif
//byte[] result = null;
//if (src.Contains("://"))
//{ // for Android
// var www = new WWW(src);
// yield return www;
// result = www.bytes;
//}
//else
//{
// result = System.IO.File.ReadAllBytes(src);
//}
//System.IO.File.WriteAllBytes(dst, result);
if (ext == ".html")
{
Debug.Log("file://" + dst.Replace(" ", "%20"));
webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
break;
}
}
}
#else
if (Url.StartsWith("http") || Url.StartsWith("file:")) {
webViewObject.LoadURL(Url.Replace(" ", "%20"));
} else {
webViewObject.LoadURL("StreamingAssets/" + Url.Replace(" ", "%20"));
}
webViewObject.EvaluateJS(
"parent.$(function() {" +
" window.Unity = {" +
" call:function(msg) {" +
" parent.unityWebView.sendMessage('WebViewObject', msg)" +
" }" +
" };" +
"});");
#endif
}
public void goBack()
{
if (webViewObject != null) {
webViewObject.GoBack();
}
}
public void goForward()
{
if (webViewObject != null)
{
webViewObject.GoForward();
}
}
public void destroy()
{
if (webViewObject != null)
{
Destroy(webViewObject.gameObject);
webViewObject = null;
}
}
/*
#if !UNITY_WEBPLAYER && !UNITY_WEBGL
void OnGUI()
{
GUI.enabled = webViewObject.CanGoBack();
if (GUI.Button(new Rect(10, 10, 80, 80), "<")) {
webViewObject.GoBack();
}
GUI.enabled = true;
GUI.enabled = webViewObject.CanGoForward();
if (GUI.Button(new Rect(100, 10, 80, 80), ">")) {
webViewObject.GoForward();
}
GUI.enabled = true;
GUI.TextField(new Rect(200, 10, 300, 80), "" + webViewObject.Progress());
if (GUI.Button(new Rect(600, 10, 80, 80), "*")) {
var g = GameObject.Find("WebViewObject");
if (g != null) {
Destroy(g);
} else {
StartCoroutine(Start());
}
}
GUI.enabled = true;
if (GUI.Button(new Rect(700, 10, 80, 80), "c")) {
Debug.Log(webViewObject.GetCookies(Url));
}
GUI.enabled = true;
}
#endif
*/
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1437a603a062d4f3d8a747e9bf0f7bfb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: