开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (9)
master
example-for-417
experimental/new_window
experimental/libmonobdwgc
experimental/android_fit_for_softkeyboard
experimental/monopinvokecallback
experimental/firebase
experimental/android_visibility
experimental/disable_movies
master
分支 (9)
master
example-for-417
experimental/new_window
experimental/libmonobdwgc
experimental/android_fit_for_softkeyboard
experimental/monopinvokecallback
experimental/firebase
experimental/android_visibility
experimental/disable_movies
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (9)
master
example-for-417
experimental/new_window
experimental/libmonobdwgc
experimental/android_fit_for_softkeyboard
experimental/monopinvokecallback
experimental/firebase
experimental/android_visibility
experimental/disable_movies
unity-webview
/
plugins
/
WebViewObject.cs
unity-webview
/
plugins
/
WebViewObject.cs
WebViewObject.cs 36.77 KB
一键复制 编辑 原始数据 按行查看 历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
/*
* Copyright (C) 2011 Keijiro Takahashi
* 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 UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
#if UNITY_2018_4_OR_NEWER
using UnityEngine.Networking;
#endif
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
using System.IO;
using System.Text.RegularExpressions;
#endif
using Callback = System.Action<string>;
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
public class UnitySendMessageDispatcher
{
public static void Dispatch(string name, string method, string message)
{
GameObject obj = GameObject.Find(name);
if (obj != null)
obj.SendMessage(method, message);
}
}
#endif
public class WebViewObject : MonoBehaviour
{
Callback onJS;
Callback onError;
Callback onHttpError;
Callback onStarted;
Callback onLoaded;
Callback onHooked;
bool visibility;
bool alertDialogEnabled = true;
bool scrollBounceEnabled = true;
int mMarginLeft;
int mMarginTop;
int mMarginRight;
int mMarginBottom;
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
IntPtr webView;
Rect rect;
Texture2D texture;
string inputString;
bool hasFocus;
#elif UNITY_IPHONE
IntPtr webView;
#elif UNITY_ANDROID
AndroidJavaObject webView;
bool mVisibility;
bool mIsKeyboardVisible0;
bool mIsKeyboardVisible;
float mResumedTimestamp;
void OnApplicationPause(bool paused)
{
if (webView == null)
return;
if (!paused)
{
webView.Call("SetVisibility", false);
mResumedTimestamp = Time.realtimeSinceStartup;
}
webView.Call("OnApplicationPause", paused);
}
void Update()
{
if (webView == null)
return;
if (mResumedTimestamp != 0.0f && Time.realtimeSinceStartup - mResumedTimestamp > 0.5f)
{
mResumedTimestamp = 0.0f;
webView.Call("SetVisibility", mVisibility);
}
}
/// Called from Java native plugin to set when the keyboard is opened
public void SetKeyboardVisible(string pIsVisible)
{
mIsKeyboardVisible = (pIsVisible == "true");
if (mIsKeyboardVisible != mIsKeyboardVisible0)
{
mIsKeyboardVisible0 = mIsKeyboardVisible;
SetMargins(mMarginLeft, mMarginTop, mMarginRight, mMarginBottom);
} else if (mIsKeyboardVisible) {
SetMargins(mMarginLeft, mMarginTop, mMarginRight, mMarginBottom);
}
}
public int AdjustBottomMargin(int bottom)
{
if (!mIsKeyboardVisible)
{
return bottom;
}
else
{
int keyboardHeight = 0;
using(AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject View = UnityClass.GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer").Call<AndroidJavaObject>("getView");
using(AndroidJavaObject Rct = new AndroidJavaObject("android.graphics.Rect"))
{
View.Call("getWindowVisibleDisplayFrame", Rct);
keyboardHeight = View.Call<int>("getHeight") - Rct.Call<int>("height");
}
}
return (bottom > keyboardHeight) ? bottom : keyboardHeight;
}
}
#else
IntPtr webView;
#endif
public bool IsKeyboardVisible
{
get
{
#if !UNITY_EDITOR && UNITY_ANDROID
return mIsKeyboardVisible;
#elif !UNITY_EDITOR && UNITY_IPHONE
return TouchScreenKeyboard.visible;
#else
return false;
#endif
}
}
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
#if WEBVIEW_SEPARATED
[DllImport("WebViewSeparated")]
private static extern string _CWebViewPlugin_GetAppPath();
[DllImport("WebViewSeparated")]
private static extern IntPtr _CWebViewPlugin_Init(
string gameObject, bool transparent, int width, int height, string ua, bool ineditor);
[DllImport("WebViewSeparated")]
private static extern int _CWebViewPlugin_Destroy(IntPtr instance);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_SetRect(
IntPtr instance, int width, int height);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_SetVisibility(
IntPtr instance, bool visibility);
[DllImport("WebViewSeparated")]
private static extern bool _CWebViewPlugin_SetURLPattern(
IntPtr instance, string allowPattern, string denyPattern, string hookPattern);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_LoadURL(
IntPtr instance, string url);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_LoadHTML(
IntPtr instance, string html, string baseUrl);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_EvaluateJS(
IntPtr instance, string url);
[DllImport("WebViewSeparated")]
private static extern int _CWebViewPlugin_Progress(
IntPtr instance);
[DllImport("WebViewSeparated")]
private static extern bool _CWebViewPlugin_CanGoBack(
IntPtr instance);
[DllImport("WebViewSeparated")]
private static extern bool _CWebViewPlugin_CanGoForward(
IntPtr instance);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_GoBack(
IntPtr instance);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_GoForward(
IntPtr instance);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_Update(IntPtr instance,
int x, int y, float deltaY, bool down, bool press, bool release,
bool keyPress, short keyCode, string keyChars,
bool refreshBitmap);
[DllImport("WebViewSeparated")]
private static extern int _CWebViewPlugin_BitmapWidth(IntPtr instance);
[DllImport("WebViewSeparated")]
private static extern int _CWebViewPlugin_BitmapHeight(IntPtr instance);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_SetTextureId(IntPtr instance, int textureId);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_SetCurrentInstance(IntPtr instance);
[DllImport("WebViewSeparated")]
private static extern IntPtr GetRenderEventFunc();
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_AddCustomHeader(IntPtr instance, string headerKey, string headerValue);
[DllImport("WebViewSeparated")]
private static extern string _CWebViewPlugin_GetCustomHeaderValue(IntPtr instance, string headerKey);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_RemoveCustomHeader(IntPtr instance, string headerKey);
[DllImport("WebViewSeparated")]
private static extern void _CWebViewPlugin_ClearCustomHeader(IntPtr instance);
[DllImport("WebViewSeparated")]
private static extern string _CWebViewPlugin_GetMessage(IntPtr instance);
#else
[DllImport("WebView")]
private static extern string _CWebViewPlugin_GetAppPath();
[DllImport("WebView")]
private static extern IntPtr _CWebViewPlugin_Init(
string gameObject, bool transparent, int width, int height, string ua, bool ineditor);
[DllImport("WebView")]
private static extern int _CWebViewPlugin_Destroy(IntPtr instance);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_SetRect(
IntPtr instance, int width, int height);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_SetVisibility(
IntPtr instance, bool visibility);
[DllImport("WebView")]
private static extern bool _CWebViewPlugin_SetURLPattern(
IntPtr instance, string allowPattern, string denyPattern, string hookPattern);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_LoadURL(
IntPtr instance, string url);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_LoadHTML(
IntPtr instance, string html, string baseUrl);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_EvaluateJS(
IntPtr instance, string url);
[DllImport("WebView")]
private static extern int _CWebViewPlugin_Progress(
IntPtr instance);
[DllImport("WebView")]
private static extern bool _CWebViewPlugin_CanGoBack(
IntPtr instance);
[DllImport("WebView")]
private static extern bool _CWebViewPlugin_CanGoForward(
IntPtr instance);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_GoBack(
IntPtr instance);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_GoForward(
IntPtr instance);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_Update(IntPtr instance,
int x, int y, float deltaY, bool down, bool press, bool release,
bool keyPress, short keyCode, string keyChars,
bool refreshBitmap);
[DllImport("WebView")]
private static extern int _CWebViewPlugin_BitmapWidth(IntPtr instance);
[DllImport("WebView")]
private static extern int _CWebViewPlugin_BitmapHeight(IntPtr instance);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_SetTextureId(IntPtr instance, int textureId);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_SetCurrentInstance(IntPtr instance);
[DllImport("WebView")]
private static extern IntPtr GetRenderEventFunc();
[DllImport("WebView")]
private static extern void _CWebViewPlugin_AddCustomHeader(IntPtr instance, string headerKey, string headerValue);
[DllImport("WebView")]
private static extern string _CWebViewPlugin_GetCustomHeaderValue(IntPtr instance, string headerKey);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_RemoveCustomHeader(IntPtr instance, string headerKey);
[DllImport("WebView")]
private static extern void _CWebViewPlugin_ClearCustomHeader(IntPtr instance);
[DllImport("WebView")]
private static extern string _CWebViewPlugin_GetMessage(IntPtr instance);
#endif
#elif UNITY_IPHONE
[DllImport("__Internal")]
private static extern IntPtr _CWebViewPlugin_Init(string gameObject, bool transparent, string ua, bool enableWKWebView);
[DllImport("__Internal")]
private static extern int _CWebViewPlugin_Destroy(IntPtr instance);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_SetMargins(
IntPtr instance, float left, float top, float right, float bottom, bool relative);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_SetVisibility(
IntPtr instance, bool visibility);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_SetAlertDialogEnabled(
IntPtr instance, bool enabled);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_SetScrollBounceEnabled(
IntPtr instance, bool enabled);
[DllImport("__Internal")]
private static extern bool _CWebViewPlugin_SetURLPattern(
IntPtr instance, string allowPattern, string denyPattern, string hookPattern);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_LoadURL(
IntPtr instance, string url);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_LoadHTML(
IntPtr instance, string html, string baseUrl);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_EvaluateJS(
IntPtr instance, string url);
[DllImport("__Internal")]
private static extern int _CWebViewPlugin_Progress(
IntPtr instance);
[DllImport("__Internal")]
private static extern bool _CWebViewPlugin_CanGoBack(
IntPtr instance);
[DllImport("__Internal")]
private static extern bool _CWebViewPlugin_CanGoForward(
IntPtr instance);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_GoBack(
IntPtr instance);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_GoForward(
IntPtr instance);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_AddCustomHeader(IntPtr instance, string headerKey, string headerValue);
[DllImport("__Internal")]
private static extern string _CWebViewPlugin_GetCustomHeaderValue(IntPtr instance, string headerKey);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_RemoveCustomHeader(IntPtr instance, string headerKey);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_ClearCustomHeader(IntPtr instance);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_ClearCookies();
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_SaveCookies();
[DllImport("__Internal")]
private static extern string _CWebViewPlugin_GetCookies(string url);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_SetBasicAuthInfo(IntPtr instance, string userName, string password);
#elif UNITY_WEBGL
[DllImport("__Internal")]
private static extern void _gree_unity_webview_init(string name);
[DllImport("__Internal")]
private static extern void _gree_unity_webview_setMargins(string name, int left, int top, int right, int bottom);
[DllImport("__Internal")]
private static extern void _gree_unity_webview_setVisibility(string name, bool visible);
[DllImport("__Internal")]
private static extern void _gree_unity_webview_loadURL(string name, string url);
[DllImport("__Internal")]
private static extern void _gree_unity_webview_evaluateJS(string name, string js);
[DllImport("__Internal")]
private static extern void _gree_unity_webview_destroy(string name);
#endif
public static bool IsWebViewAvailable()
{
#if !UNITY_EDITOR && UNITY_ANDROID
return (new AndroidJavaObject("net.gree.unitywebview.CWebViewPlugin")).CallStatic<bool>("IsWebViewAvailable");
#else
return true;
#endif
}
public void Init(
Callback cb = null,
bool transparent = false,
string ua = "",
Callback err = null,
Callback httpErr = null,
Callback ld = null,
bool enableWKWebView = false,
Callback started = null,
Callback hooked = null)
{
onJS = cb;
onError = err;
onHttpError = httpErr;
onStarted = started;
onLoaded = ld;
onHooked = hooked;
#if UNITY_WEBGL
#if !UNITY_EDITOR
_gree_unity_webview_init(name);
#endif
#elif UNITY_WEBPLAYER
Application.ExternalCall("unityWebView.init", name);
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
Debug.LogError("Webview is not supported on this platform.");
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
{
var uri = new Uri(_CWebViewPlugin_GetAppPath());
var info = File.ReadAllText(uri.LocalPath + "Contents/Info.plist");
if (Regex.IsMatch(info, @"<key>CFBundleGetInfoString</key>\s*<string>Unity version [5-9]\.[3-9]")
&& !Regex.IsMatch(info, @"<key>NSAppTransportSecurity</key>\s*<dict>\s*<key>NSAllowsArbitraryLoads</key>\s*<true/>\s*</dict>")) {
Debug.LogWarning("<color=yellow>WebViewObject: NSAppTransportSecurity isn't configured to allow HTTP. If you need to allow any HTTP access, please shutdown Unity and invoke:</color>\n/usr/libexec/PlistBuddy -c \"Add NSAppTransportSecurity:NSAllowsArbitraryLoads bool true\" /Applications/Unity/Unity.app/Contents/Info.plist");
}
}
#if UNITY_EDITOR_OSX
// if (string.IsNullOrEmpty(ua)) {
// ua = @"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53";
// }
#endif
webView = _CWebViewPlugin_Init(
name,
transparent,
Screen.width,
Screen.height,
ua,
Application.platform == RuntimePlatform.OSXEditor);
// define pseudo requestAnimationFrame.
EvaluateJS(@"(function() {
var vsync = 1000 / 60;
var t0 = window.performance.now();
window.requestAnimationFrame = function(callback, element) {
var t1 = window.performance.now();
var duration = t1 - t0;
var d = vsync - ((duration > vsync) ? duration % vsync : duration);
var id = window.setTimeout(function() {t0 = window.performance.now(); callback(t1 + d);}, d);
return id;
};
})()");
rect = new Rect(0, 0, Screen.width, Screen.height);
OnApplicationFocus(true);
#elif UNITY_IPHONE
webView = _CWebViewPlugin_Init(name, transparent, ua, enableWKWebView);
#elif UNITY_ANDROID
webView = new AndroidJavaObject("net.gree.unitywebview.CWebViewPlugin");
webView.Call("Init", name, transparent, ua);
#else
Debug.LogError("Webview is not supported on this platform.");
#endif
}
protected virtual void OnDestroy()
{
#if UNITY_WEBGL
#if !UNITY_EDITOR
_gree_unity_webview_destroy(name);
#endif
#elif UNITY_WEBPLAYER
Application.ExternalCall("unityWebView.destroy", name);
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_Destroy(webView);
webView = IntPtr.Zero;
Destroy(texture);
#elif UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_Destroy(webView);
webView = IntPtr.Zero;
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("Destroy");
webView = null;
#endif
}
// Use this function instead of SetMargins to easily set up a centered window
// NOTE: for historical reasons, `center` means the lower left corner and positive y values extend up.
public void SetCenterPositionWithScale(Vector2 center, Vector2 scale)
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#else
float left = (Screen.width - scale.x) / 2.0f + center.x;
float right = Screen.width - (left + scale.x);
float bottom = (Screen.height - scale.y) / 2.0f + center.y;
float top = Screen.height - (bottom + scale.y);
SetMargins((int)left, (int)top, (int)right, (int)bottom);
#endif
}
public void SetMargins(int left, int top, int right, int bottom, bool relative = false)
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
if (webView == IntPtr.Zero)
return;
#elif UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
#elif UNITY_ANDROID
if (webView == null)
return;
#endif
mMarginLeft = left;
mMarginTop = top;
mMarginRight = right;
mMarginBottom = bottom;
#if UNITY_WEBGL
#if !UNITY_EDITOR
_gree_unity_webview_setMargins(name, left, top, right, bottom);
#endif
#elif UNITY_WEBPLAYER
Application.ExternalCall("unityWebView.setMargins", name, left, top, right, bottom);
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
int width = Screen.width - (left + right);
int height = Screen.height - (bottom + top);
_CWebViewPlugin_SetRect(webView, width, height);
rect = new Rect(left, bottom, width, height);
#elif UNITY_IPHONE
if (relative) {
float w = (float)Screen.width;
float h = (float)Screen.height;
_CWebViewPlugin_SetMargins(webView, left / w, top / h, right / w, bottom / h, true);
} else {
_CWebViewPlugin_SetMargins(webView, (float)left, (float)top, (float)right, (float)bottom, false);
}
#elif UNITY_ANDROID
if (relative) {
float w = (float)Screen.width;
float h = (float)Screen.height;
int iw = Screen.currentResolution.width;
int ih = Screen.currentResolution.height;
webView.Call("SetMargins", (int)(left / w * iw), (int)(top / h * ih), (int)(right / w * iw), AdjustBottomMargin((int)(bottom / h * ih)));
} else {
webView.Call("SetMargins", left, top, right, AdjustBottomMargin(bottom));
}
#endif
}
public void SetVisibility(bool v)
{
#if UNITY_WEBGL
#if !UNITY_EDITOR
_gree_unity_webview_setVisibility(name, v);
#endif
#elif UNITY_WEBPLAYER
Application.ExternalCall("unityWebView.setVisibility", name, v);
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_SetVisibility(webView, v);
#elif UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_SetVisibility(webView, v);
#elif UNITY_ANDROID
if (webView == null)
return;
mVisibility = v;
webView.Call("SetVisibility", v);
#endif
visibility = v;
}
public bool GetVisibility()
{
return visibility;
}
public void SetAlertDialogEnabled(bool e)
{
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
// TODO: UNSUPPORTED
#elif UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_SetAlertDialogEnabled(webView, e);
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("SetAlertDialogEnabled", e);
#else
// TODO: UNSUPPORTED
#endif
alertDialogEnabled = e;
}
public bool GetAlertDialogEnabled()
{
return alertDialogEnabled;
}
public void SetScrollBounceEnabled(bool e)
{
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
// TODO: UNSUPPORTED
#elif UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_SetScrollBounceEnabled(webView, e);
#elif UNITY_ANDROID
// TODO: UNSUPPORTED
#else
// TODO: UNSUPPORTED
#endif
scrollBounceEnabled = e;
}
public bool GetScrollBounceEnabled()
{
return scrollBounceEnabled;
}
public bool SetURLPattern(string allowPattern, string denyPattern, string hookPattern)
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
return false;
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
return false;
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return false;
return _CWebViewPlugin_SetURLPattern(webView, allowPattern, denyPattern, hookPattern);
#elif UNITY_ANDROID
if (webView == null)
return false;
return webView.Call<bool>("SetURLPattern", allowPattern, denyPattern, hookPattern);
#endif
}
public void LoadURL(string url)
{
if (string.IsNullOrEmpty(url))
return;
#if UNITY_WEBGL
#if !UNITY_EDITOR
_gree_unity_webview_loadURL(name, url);
#endif
#elif UNITY_WEBPLAYER
Application.ExternalCall("unityWebView.loadURL", name, url);
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_LoadURL(webView, url);
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("LoadURL", url);
#endif
}
public void LoadHTML(string html, string baseUrl)
{
if (string.IsNullOrEmpty(html))
return;
if (string.IsNullOrEmpty(baseUrl))
baseUrl = "";
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_LoadHTML(webView, html, baseUrl);
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("LoadHTML", html, baseUrl);
#endif
}
public void EvaluateJS(string js)
{
#if UNITY_WEBGL
#if !UNITY_EDITOR
_gree_unity_webview_evaluateJS(name, js);
#endif
#elif UNITY_WEBPLAYER
Application.ExternalCall("unityWebView.evaluateJS", name, js);
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_EvaluateJS(webView, js);
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("EvaluateJS", js);
#endif
}
public int Progress()
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
return 0;
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
return 0;
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return 0;
return _CWebViewPlugin_Progress(webView);
#elif UNITY_ANDROID
if (webView == null)
return 0;
return webView.Get<int>("progress");
#endif
}
public bool CanGoBack()
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
return false;
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
return false;
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return false;
return _CWebViewPlugin_CanGoBack(webView);
#elif UNITY_ANDROID
if (webView == null)
return false;
return webView.Get<bool>("canGoBack");
#endif
}
public bool CanGoForward()
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
return false;
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
return false;
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return false;
return _CWebViewPlugin_CanGoForward(webView);
#elif UNITY_ANDROID
if (webView == null)
return false;
return webView.Get<bool>("canGoForward");
#endif
}
public void GoBack()
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_GoBack(webView);
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("GoBack");
#endif
}
public void GoForward()
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_GoForward(webView);
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("GoForward");
#endif
}
public void CallOnError(string error)
{
if (onError != null)
{
onError(error);
}
}
public void CallOnHttpError(string error)
{
if (onHttpError != null)
{
onHttpError(error);
}
}
public void CallOnStarted(string url)
{
if (onStarted != null)
{
onStarted(url);
}
}
public void CallOnLoaded(string url)
{
if (onLoaded != null)
{
onLoaded(url);
}
}
public void CallFromJS(string message)
{
if (onJS != null)
{
#if !UNITY_ANDROID
#if UNITY_2018_4_OR_NEWER
message = UnityWebRequest.UnEscapeURL(message);
#else // UNITY_2018_4_OR_NEWER
message = WWW.UnEscapeURL(message);
#endif // UNITY_2018_4_OR_NEWER
#endif // !UNITY_ANDROID
onJS(message);
}
}
public void CallOnHooked(string message)
{
if (onHooked != null)
{
#if !UNITY_ANDROID
#if UNITY_2018_4_OR_NEWER
message = UnityWebRequest.UnEscapeURL(message);
#else // UNITY_2018_4_OR_NEWER
message = WWW.UnEscapeURL(message);
#endif // UNITY_2018_4_OR_NEWER
#endif // !UNITY_ANDROID
onHooked(message);
}
}
public void AddCustomHeader(string headerKey, string headerValue)
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_AddCustomHeader(webView, headerKey, headerValue);
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("AddCustomHeader", headerKey, headerValue);
#endif
}
public string GetCustomHeaderValue(string headerKey)
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
return null;
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
return null;
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return null;
return _CWebViewPlugin_GetCustomHeaderValue(webView, headerKey);
#elif UNITY_ANDROID
if (webView == null)
return null;
return webView.Call<string>("GetCustomHeaderValue", headerKey);
#endif
}
public void RemoveCustomHeader(string headerKey)
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_RemoveCustomHeader(webView, headerKey);
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("RemoveCustomHeader", headerKey);
#endif
}
public void ClearCustomHeader()
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_ClearCustomHeader(webView);
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("ClearCustomHeader");
#endif
}
public void ClearCookies()
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_IPHONE && !UNITY_EDITOR
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_ClearCookies();
#elif UNITY_ANDROID && !UNITY_EDITOR
if (webView == null)
return;
webView.Call("ClearCookies");
#endif
}
public void SaveCookies()
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_IPHONE && !UNITY_EDITOR
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_SaveCookies();
#elif UNITY_ANDROID && !UNITY_EDITOR
if (webView == null)
return;
webView.Call("SaveCookies");
#endif
}
public string GetCookies(string url)
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
return "";
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
return "";
#elif UNITY_IPHONE && !UNITY_EDITOR
if (webView == IntPtr.Zero)
return "";
return _CWebViewPlugin_GetCookies(url);
#elif UNITY_ANDROID && !UNITY_EDITOR
if (webView == null)
return "";
return webView.Call<string>("GetCookies", url);
#else
//TODO: UNSUPPORTED
return "";
#endif
}
public void SetBasicAuthInfo(string userName, string password)
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
//TODO: UNSUPPORTED
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
//TODO: UNSUPPORTED
#elif UNITY_IPHONE
if (webView == IntPtr.Zero)
return;
_CWebViewPlugin_SetBasicAuthInfo(webView, userName, password);
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("SetBasicAuthInfo", userName, password);
#endif
}
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
void OnApplicationFocus(bool focus)
{
hasFocus = focus;
}
void Update()
{
if (hasFocus) {
inputString += Input.inputString;
}
for (;;) {
if (webView == IntPtr.Zero)
break;
string s = _CWebViewPlugin_GetMessage(webView);
if (s == null)
break;
switch (s[0]) {
case 'E':
CallOnError(s.Substring(1));
break;
case 'S':
CallOnStarted(s.Substring(1));
break;
case 'L':
CallOnLoaded(s.Substring(1));
break;
case 'J':
CallFromJS(s.Substring(1));
break;
case 'H':
CallOnHooked(s.Substring(1));
break;
}
}
}
public int bitmapRefreshCycle = 1;
void OnGUI()
{
if (webView == IntPtr.Zero || !visibility)
return;
Vector3 pos = Input.mousePosition;
bool down = Input.GetButton("Fire1");
bool press = Input.GetButtonDown("Fire1");
bool release = Input.GetButtonUp("Fire1");
float deltaY = Input.GetAxis("Mouse ScrollWheel");
bool keyPress = false;
string keyChars = "";
short keyCode = 0;
if (inputString != null && inputString.Length > 0) {
keyPress = true;
keyChars = inputString.Substring(0, 1);
keyCode = (short)inputString[0];
inputString = inputString.Substring(1);
}
bool refreshBitmap = (Time.frameCount % bitmapRefreshCycle == 0);
_CWebViewPlugin_Update(webView,
(int)(pos.x - rect.x), (int)(pos.y - rect.y), deltaY,
down, press, release, keyPress, keyCode, keyChars,
refreshBitmap);
if (refreshBitmap) {
{
var w = _CWebViewPlugin_BitmapWidth(webView);
var h = _CWebViewPlugin_BitmapHeight(webView);
if (texture == null || texture.width != w || texture.height != h) {
texture = new Texture2D(w, h, TextureFormat.RGBA32, false, true);
texture.filterMode = FilterMode.Bilinear;
texture.wrapMode = TextureWrapMode.Clamp;
}
}
_CWebViewPlugin_SetTextureId(webView, (int)texture.GetNativeTexturePtr());
_CWebViewPlugin_SetCurrentInstance(webView);
#if UNITY_4_6 || UNITY_5_0 || UNITY_5_1
GL.IssuePluginEvent(-1);
#else
GL.IssuePluginEvent(GetRenderEventFunc(), -1);
#endif
}
if (texture != null) {
Matrix4x4 m = GUI.matrix;
GUI.matrix
= Matrix4x4.TRS(
new Vector3(0, Screen.height, 0),
Quaternion.identity,
new Vector3(1, -1, 1));
GUI.DrawTexture(rect, texture);
GUI.matrix = m;
}
}
#endif
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/coding2233/unity-webview.git
git@gitee.com:coding2233/unity-webview.git
coding2233
unity-webview
unity-webview
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /