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
|
#if UNITY_IOS || UNITY_ANDROID || UNITY_WP8
//
// UniWebView.cs
// Created by Wang Wei(@onevcat) on 2013-10-20.
//
using UnityEngine;
using System.Collections;
using System;
/// <summary>
/// The main class of UniWebView.
/// </summary>
/// <description>
/// Each gameObject with this script represent a webview object
/// in system. Be careful: when this script's Awake() get called, it will change the name of
/// the gameObject to make it unique in the game. So make sure this script is appeneded to a
/// gameObject that you don't care its name.
/// </description>
public class UniWebView : MonoBehaviour {
#region Events and Delegate
//Delegate and event
public delegate void LoadCompleteDelegate(UniWebView webView, bool success, string errorMessage);
/// <summary>
/// Occurs when a UniWebView finished loading a webpage.
/// </summary>
/// <description>
/// If loading finished successfully, success will be true, otherwise false and with an errorMessage.
/// </description>
public event LoadCompleteDelegate OnLoadComplete;
public delegate void LoadBeginDelegate(UniWebView webView, string loadingUrl);
/// <summary>
/// Occurs when a UniWebView began to load a webpage.
/// </summary>
/// <description>
/// You can do something with the UniWebView passed by parameter to get some info
/// or do your things when a url begins to load
/// Sometimes, the webpage contains some other parts besides of the main frame. Whenever these parts
/// begin to load, this event will be fired. You can get the url from parameter to know what url is
/// about to be loaded.
/// It is useful when you want to get some parameters from url when user clicked a link.
/// </description>
public event LoadBeginDelegate OnLoadBegin;
public delegate void ReceivedMessageDelegate(UniWebView webView, UniWebViewMessage message);
/// <summary>
/// Occurs when a UniWebView received message.
/// </summary>
/// <description>
/// If a url with format of "uniwebview://yourPath?param1=value1¶m2=value2" clicked,
/// this event will get raised with a <see cref="UniWebViewMessage"/> object.
/// </description>
public event ReceivedMessageDelegate OnReceivedMessage;
public delegate void EvalJavaScriptFinishedDelegate(UniWebView webView, string result);
/// <summary>
/// Occurs when a UniWebView finishes eval a javascript and returned something.
/// </summary>
/// <description>
/// You can use EvaluatingJavaScript method to make the webview to eval a js.
/// The string-returned version of EvaluatingJavaScript is removed. You should
/// always listen this event to get the result of js.
/// </description>
public event EvalJavaScriptFinishedDelegate OnEvalJavaScriptFinished;
public delegate bool WebViewShouldCloseDelegate(UniWebView webView);
/// <summary>
/// Occurs when on web view will be closed by native. Ask if this webview should be closed or not.
/// </summary>
/// The users can close the webView by tapping back button (Android) or done button (iOS).
/// When the webview will be closed, this event will be raised.
/// If you return false, the webview will not be closed. If you did not implement it, webview will be closed.
/// </description>
public event WebViewShouldCloseDelegate OnWebViewShouldClose;
public delegate void ReceivedKeyCodeDelegate(UniWebView webView, int keyCode);
/// <summary>
/// Occurs when users clicks or taps any key while webview is actived. This event only fired on Android.
/// </summary>
/// <description>
/// On Android, the key down event can not be passed back to Unity due to some issue in Unity 4.3's Android Player.
/// As result, you are not able to get the key input on Android by just using Unity's Input.GetKeyDown or GetKey method.
/// If you want to know the user input while the webview on, you can subscribe this event.
/// This event will be fired with a int number indicating the key code tapped as parameter.
/// You can refer to Android's documentation to find out which key is tapped (http://developer.android.com/reference/android/view/KeyEvent.html)
/// This event will be never raised on iOS, because iOS forbids the tracking of user's keyboard or device button events.
/// </description>
public event ReceivedKeyCodeDelegate OnReceivedKeyCode;
public delegate UniWebViewEdgeInsets InsetsForScreenOreitationDelegate(UniWebView webView, UniWebViewOrientation orientation);
/// <summary>
/// Called when the webview need to know the insets for calculating webview's frame.
/// </summary>
/// <description>
/// If you need to show web content in both portrait and landscpace mode, you might want to
/// specify different insets for the screen orientation separately.
/// This event will be called when the Show() method gets called and the screen orientation
/// changes. You can implement this method and check current orientation, then return correct insets.
/// If this event is not implemented, the old insets value will be used to resize the webview size.
/// If you do not use auto-rotating in your webview, you can ignore this method and just set the <see cref="insets"/>
/// property directly.
/// </description>
public event InsetsForScreenOreitationDelegate InsetsForScreenOreitation;
#endregion
[SerializeField]
private UniWebViewEdgeInsets _insets = new UniWebViewEdgeInsets(0,0,0,0);
/// <summary>
/// Gets or sets the insets of a UniWebView object.
/// </summary>
/// <value>The insets in point from top, left, bottom and right edge from the screen.</value>
/// <description>
/// Default value is UniWebViewEdgeInsets(0,0,0,0), which means a full screen webpage.
/// If you want use different insets in portrait and landscape screen, use <see cref="InsetsForScreenOreitation"/>
/// </description>
public UniWebViewEdgeInsets insets {
get {
return _insets;
}
set {
if (_insets != value) {
ForceUpdateInsetsInternal(value);
}
}
}
private void ForceUpdateInsetsInternal(UniWebViewEdgeInsets insets) {
_insets = insets;
UniWebViewPlugin.ChangeInsets(gameObject.name,
this.insets.top,
this.insets.left,
this.insets.bottom,
this.insets.right);
#if UNITY_EDITOR
CreateTexture(this.insets.left,
this.insets.bottom,
Screen.width - this.insets.left - this.insets.right,
Screen.height - this.insets.top - this.insets.bottom
);
#endif
}
/// <summary>
/// The url this UniWebView should load. You should set it before loading webpage.
/// Do not use this value when you want to get the url of current page. It is only for loading the first page.
/// The value will not change as the users navigating between the pages.
/// Use <seealso cref="currentUrl"/> if you want to know current url.
/// </summary>
public string url;
/// <summary>
/// If true, load the set url when in script's Start() method.
/// Otherwise, you should call Load() method yourself.
/// </summary>
public bool loadOnStart;
/// <summary>
/// If true, show the webview automatically when it finished loading.
/// Otherwise, you should listen the OnLoadComplete event and call Show() method your self.
/// </summary>
public bool autoShowWhenLoadComplete;
/// <summary>
/// Gets the current URL of the web page.
/// </summary>
/// <value>The current URL of this webview.</value>
/// <description>
/// This value indicates the main frame url of the webpage.
/// It will be updated only when the webpage finishs or fails loading.
/// </description>
public string currentUrl {
get {
return UniWebViewPlugin.GetCurrentUrl(gameObject.name);
}
}
private bool _backButtonEnable = true;
private bool _bouncesEnable;
private bool _zoomEnable;
private string _currentGUID;
private int _lastScreenHeight;
private bool _immersiveMode = true;
private Action _showTransitionAction = null;
private Action _hideTransitionAction = null;
/// <summary>
/// Gets or sets a value indicating whether the back button of this <see cref="UniWebView"/> is enabled.
/// </summary>
/// <description>
/// It is only for Android and Windows Phone 8. If set true, users can use the back button of these devices to goBack or close the web view
/// if there is nothing to goBack. Otherwise, the back button will do nothing when the webview is shown.
/// When set true, Unity will not receive the Input.KeyDown or other similar event in Android. You could use <seealso cref="OnReceivedKeyCode"/> to watch the key event instead.
/// This value means nothing for iOS, since there is no back button for iOS devices.
/// </description>
/// <value><c>true</c> if back button enabled; otherwise, <c>false</c>. Default is true</value>.
public bool backButtonEnable {
get {
return _backButtonEnable;
}
set {
if (_backButtonEnable != value) {
_backButtonEnable = value;
#if (UNITY_ANDROID || UNITY_WP8) && !UNITY_EDITOR
UniWebViewPlugin.SetBackButtonEnable(gameObject.name, _backButtonEnable);
#endif
}
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="UniWebView"/> can bounces or not.
/// </summary>
/// <description>
/// The default iOS webview has a bounces effect when drag out of edge.
/// The default Android webview has a color indicator when drag beyond the edge.
/// UniWebView disabled these bounces effect by default. If you want the bounces, set this property to true.
/// This property does noting in editor or Windows Phone 8.
/// </description>
/// <value><c>true</c> if bounces enable; otherwise, <c>false</c>.</value>
public bool bouncesEnable {
get {
return _bouncesEnable;
}
set {
if (_bouncesEnable != value) {
_bouncesEnable = value;
#if !UNITY_EDITOR
UniWebViewPlugin.SetBounces(gameObject.name, _bouncesEnable);
#endif
}
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="UniWebView"/> can be zoomed or not.
/// </summary>
/// <description>
/// If true, users can zoom in or zoom out the webpage by a pinch gesture.
/// This propery will valid immediately on Android. But on iOS, it will not valid until the next page loaded.
/// You can set this property before page loading, or use Reload() to refresh current page to make it valid.
/// This property does noting in Windows Phone 8, since there is no way to control the default behavior for it.
/// If you need to disable users zoom or pinch guesture, you need to add a viewport tag with "user-scalable=no" to your page.
/// </description>
/// <value><c>true</c> if zoom enabled; otherwise, <c>false</c>.</value> Default is false.
public bool zoomEnable {
get {
return _zoomEnable;
}
set {
if (_zoomEnable != value) {
_zoomEnable = value;
#if !UNITY_EDITOR
UniWebViewPlugin.SetZoomEnable(gameObject.name, _zoomEnable);
#endif
}
}
}
/// <summary>
/// Get the user agent of this webview.
/// </summary>
/// <value>The string of user agent using by the webview.</value>
/// <description>
/// It is a read-only property of webview. Once it is set when the webview gets created, the user agent can not be changed again.
/// If you want to use a customized user agent, you should call <see cref="SetUserAgent"/> method before create a webview component.
/// </description>
public string userAgent {
get {
return UniWebViewPlugin.GetUserAgent(gameObject.name);
}
}
/// <summary>
/// Get or set the alpha of current webview.
/// </summary>
/// <value>The alpha.</value>
/// <description>
/// This value indicates the alpha of webview.
/// The value should be between 0.0~1.0, which 1.0 means opaque and 0.0 is transparent.
/// This property will work on iOS, Android and WP8. In editor it will be always 1.0 and opaque.
/// </description>
public float alpha {
get {
return UniWebViewPlugin.GetAlpha(gameObject.name);
}
set {
UniWebViewPlugin.SetAlpha(gameObject.name, Mathf.Clamp01(value));
}
}
/// <summary>
/// Whether the new page should be opened in an external browser when user clicks a link.
/// </summary>
/// <value>A boolean indicates link clicking should be opened externally or not.</value>
/// <description>
/// If true, when user clicks on a link, an external web page browser on the device will be opened
/// and direct to the target link. It will be useful if you need navigate your user to a device installed
/// browser (like Mobile Safari on iOS or Chrome on Android).
/// This will only work when your user click a link in your page. Due to the lack detection of Android web view,
/// some javascript embedded link will not be able to open externally correctly. If you need more control of it,
/// consider to add all http/https scheme with AddUrlScheme method and handle them case by case in the OnReceivedMessage callback.
/// The default value is false, which means all link clicking will be opened in place.
/// </description>
public bool openLinksInExternalBrowser {
get {
return UniWebViewPlugin.GetOpenLinksInExternalBrowser(gameObject.name);
}
set {
UniWebViewPlugin.SetOpenLinksInExternalBrowser(gameObject.name, value);
}
}
/// <summary>
/// Get or set a value indicating whether this <see cref="UniWebView"/> is in immersive mode.
/// </summary>
/// <value><c>true</c> if immersive mode; otherwise, <c>false</c>.</value>
/// <description>
///
/// </description>
public bool immersiveMode {
get {
return _immersiveMode;
}
set {
#if UNITY_ANDROID && !UNITY_EDITOR
_immersiveMode = value;
UniWebViewPlugin.SetImmersiveModeEnabled(gameObject.name, _immersiveMode);
#endif
}
}
/// <summary>
/// Set user agent string for webview. This method has no effect on the webviews which are already initiated.
/// The user agent of a UniWebView component can not be changed once it is created.
/// If you want to change the user agent for the webview, you have to call it before creating the UniWebView instance.
/// </summary>
/// <param name="value">The value user agent should be. Set it to null will reset the user agent to the default one.</param>
public static void SetUserAgent(string value) {
UniWebViewPlugin.SetUserAgent(value);
}
/// <summary>
/// Reset the user agent of webview. This method has no effect on the webviews which are already initiated.
/// The user agent of a UniWebView component can not be changed once it is created.
/// If you want to set user agent, use the <see cref="SetUserAgent"/> method.
/// </summary>
public static void ResetUserAgent() {
SetUserAgent(null);
}
/// <summary>
/// Set the text in Done button of tool bar in iOS.
/// User could use this button in iOS to close the web view. By default, "Done" will be shown for this button.
/// If you want to change the text of tool bar done button for the webview, you have to call it before creating the UniWebView instance.
/// This method only works for iOS. See <seealso cref="ShowToolBar"/>.
/// </summary>
/// <param name="text">The text you want to show for the Done button. Set it to null will reset it to "Done".</param>
public static void SetDoneButtonText(string text) {
#if UNITY_IOS && !UNITY_EDITOR
UniWebViewPlugin.SetDoneButtonText(text);
#endif
}
/// <summary>
/// Load the url set in <seealso cref="url"/> property of this UniWebView.
/// </summary>
public void Load() {
string loadUrl = String.IsNullOrEmpty(url) ? "about:blank" : url.Trim();
UniWebViewPlugin.Load(gameObject.name, loadUrl);
}
/// <summary>
/// A alias method to load a specified url.
/// </summary>
/// <param name="aUrl">A url to set and load</param>
/// <description>
/// It will set the url of this UniWebView and then call Load it.
/// </description>
public void Load(string aUrl) {
url = aUrl;
Load();
}
/// <summary>
/// Load a HTML string.
/// </summary>
/// <param name="htmlString">The content HTML string for the web page.</param>
/// <param name="baseUrl">The base URL in which the webview should to refer other resources</param>
/// <description>
/// If you want to specify a local baseUrl, you need to encode it to proper format.
/// See the "Can I load some html string by using UniWebView?" section in FAQ page (http://uniwebview.onevcat.com/faqs.html) for more.
/// </description>
public void LoadHTMLString(string htmlString, string baseUrl) {
UniWebViewPlugin.LoadHTMLString(gameObject.name, htmlString, baseUrl);
}
/// <summary>
/// Reload current page.
/// </summary>
public void Reload() {
UniWebViewPlugin.Reload(gameObject.name);
}
/// <summary>
/// Stop loading the current request. It will do nothing if the webpage is not loading.
/// </summary>
public void Stop() {
UniWebViewPlugin.Stop(gameObject.name);
}
/// <summary>
/// Show this UniWebView on screen.
/// </summary>
/// <description>
/// Usually, it should be called when you get the LoadCompleteDelegate raised with a success flag true.
/// The webview will not be visible until this method is called.
/// </description>
public void Show(bool fade = false, UniWebViewTransitionEdge direction = UniWebViewTransitionEdge.None, float duration = 0.4f, Action finishAction = null) {
_lastScreenHeight = UniWebViewHelper.screenHeight;
ResizeInternal();
UniWebViewPlugin.Show(gameObject.name, fade, (int)direction, duration);
_showTransitionAction = finishAction;
if (toolBarShow) {
ShowToolBar(true);
}
#if UNITY_EDITOR
_webViewId = UniWebViewPlugin.GetId(gameObject.name);
_hidden = false;
#endif
}
/// <summary>
/// Hide this UniWebView.
/// </summary>
/// <description>
/// Calling this method on a UniWebView will hide it.
/// </description>
public void Hide(bool fade = false, UniWebViewTransitionEdge direction = UniWebViewTransitionEdge.None, float duration = 0.4f, Action finishAction = null) {
#if UNITY_EDITOR
_hidden = true;
#endif
UniWebViewPlugin.Hide(gameObject.name, fade, (int)direction, duration);
_hideTransitionAction = finishAction;
}
/// <summary>
/// Send a piece of javascript to the web page and evaluate (execute) it.
/// </summary>
/// <param name="javaScript">A single javascript method call to be sent to and executed in web page</param>
/// <description>
/// Although you can write complex javascript code and evaluate it at once, a suggest way is calling this method with a single js method name.
/// The webview will try evaluate (execute) the javascript. When it finished, OnEvalJavaScriptFinished will be raised with the result.
/// You can add your js function to the html page by referring to the js file in your html page, or add it by using AddJavaScript(string javaScript) method.
/// </description>
public void EvaluatingJavaScript(string javaScript) {
UniWebViewPlugin.EvaluatingJavaScript(gameObject.name, javaScript);
}
/// <summary>
/// Add some javascript to the web page.
/// </summary>
/// <param name="javaScript">Some javascript code you want to add to the page.</param>
/// <description>
/// This method will execute the input javascript code without raising an
/// OnEvalJavaScriptFinished event. You can use this method to add some customized js
/// function to the web page, then use EvaluatingJavaScript(string javaScript) to execute it.
/// This method will add js in a async way in Android, so you should call it earlier than EvaluatingJavaScript
/// </description>
public void AddJavaScript(string javaScript) {
UniWebViewPlugin.AddJavaScript(gameObject.name, javaScript);
}
/// <summary>
/// Clean the cache of this UniWebView.
/// </summary>
public void CleanCache() {
UniWebViewPlugin.CleanCache(gameObject.name);
}
/// <summary>
/// Clean the cookie using in the app.
/// </summary>
/// <param name="key">The key under which you want to clean the cache.</param>
/// <description>
/// Try to clean cookies under the specified key using in the app.
/// If you leave the key as null or send an empty string as key, all cache will be cleared.
/// This method will clear the cookies in memory and try to
/// sync the change to disk. The memory opreation will return
/// right away, but the disk operation is async and could take some time.
/// Caution, in Android, there is no way to remove a specified cookie.
/// So this method will call setCookie method with the key to set
/// it to an empty value instead. Please refer to Android
/// documentation on CookieManager for more information.
/// The key parameter will be ignored at all on Windows Phone 8. All cookie will be cleared.
/// </description>
public void CleanCookie(string key = null) {
UniWebViewPlugin.CleanCookie(gameObject.name, key);
}
/// <summary>
/// Set the background of webview to transparent.
/// </summary>
/// <description>
/// In iOS, there is a grey background in webview. If you don't want it, just call this method to set it transparent.
/// There is no way to set Windows Phone 8 background to transparent, so this method will do noting on Windows Phone.
/// </description>
[Obsolete("SetTransparentBackground is deprecated, please use SetBackgroundColor instead.")]
public void SetTransparentBackground(bool transparent = true) {
UniWebViewPlugin.TransparentBackground(gameObject.name, transparent);
}
/// <summary>
/// Set the background color of webview.
/// </summary>
/// <description>
/// Set the background color of the webview. In iOS, it will only take in action when the web page has no background color from css.
/// There is no way to set Windows Phone 8, so this method will do noting on Windows Phone.
/// And in OSX Editor, it is limited and can be only used to set white or clear background.
/// </description>
public void SetBackgroundColor(Color color) {
UniWebViewPlugin.SetBackgroundColor(gameObject.name, color.r, color.g, color.b, color.a);
}
/// <summary>
/// If the tool bar is showing or not.
/// </summary>
/// <description>
/// This parameter is only available in iOS. In other platform, it will be always false.
/// It will take no effect when you set this after you call Show on the webview. Use `ShowToolBar` and `HideToolBar` instead if the webview is already showing.
/// </description>
public bool toolBarShow = false;
/// <summary>
/// Show the tool bar. The tool bar contains three buttons: go back, go forward and close webview.
/// </summary>
/// <param name="animate">If set to <c>true</c>, show it with an animation.</param>
/// <description>
/// The tool bar is only available in iOS. In Android and Windows Phone, you can use the back button of device to go back.
/// </description>
public void ShowToolBar(bool animate) {
#if UNITY_IOS && !UNITY_EDITOR
toolBarShow = true;
UniWebViewPlugin.ShowToolBar(gameObject.name,animate);
#endif
}
/// <summary>
/// Hide the tool bar. The tool bar contains three buttons: go back, go forward and close webview.
/// </summary>
/// <param name="animate">If set to <c>true</c>, show it with an animation.</param>
/// <description>
/// The tool bar is only available in iOS. For Android and Windows Phone, you can use the back button of device to go back.
/// </description>
public void HideToolBar(bool animate) {
#if UNITY_IOS && !UNITY_EDITOR
toolBarShow = false;
UniWebViewPlugin.HideToolBar(gameObject.name,animate);
#endif
}
/// <summary>
/// Set if a default spinner should show when loading the webpage.
/// </summary>
/// <description>
/// The default value is true, which means a spinner will show when the webview is on, and it is loading some thing.
/// The spinner contains a label and you can set a message to it. <see cref=""/>
/// You can set it false if you do not want a spinner show when loading.
/// A progress bar will be used in Windows Phone instead of a spinner.
/// </description>
/// <param name="show">If set to <c>true</c> show.</param>
public void SetShowSpinnerWhenLoading(bool show) {
UniWebViewPlugin.SetSpinnerShowWhenLoading(gameObject.name, show);
}
/// <summary>
/// Set the label text for the spinner showing when webview loading.
/// The default value is "Loading..."
/// </summary>
/// <param name="text">Text.</param>
/// <description>
/// There is no text for Windows Phone spinner, so it will do noting for it.
/// </description>
public void SetSpinnerLabelText(string text) {
UniWebViewPlugin.SetSpinnerText(gameObject.name, text);
}
/// <summary>
/// Set to use wide view port support or not.
/// </summary>
/// <param name="use">If set to <c>true</c> use view port tag in the html to determine the layout.</param>
/// <description>
/// This method only works (and be necessary) for Android. If you are using viewport tag in you page, you may
/// want to enable it before you loading and showing your page.
/// </description>
public void SetUseWideViewPort(bool use) {
#if UNITY_ANDROID && !UNITY_EDITOR
UniWebViewPlugin.SetUseWideViewPort(gameObject.name, use);
#endif
}
/// <summary>
/// Set to load with overview mode or not.
/// </summary>
/// <param name="overview">
/// If set to <c>true</c>, use overview mode to load the page.
/// </param>
/// <description>
/// This method only works for Android. If you need to load your page with overview mode, you may
/// want to enable it before you loading and showing your page.
/// </description>
public void LoadWithOverviewMode(bool overview) {
#if UNITY_ANDROID && !UNITY_EDITOR
UniWebViewPlugin.LoadWithOverviewMode(gameObject.name, overview);
#endif
}
/// <summary>
/// Determines whether the webview can go back.
/// </summary>
/// <returns><c>true</c> if this instance can go back, which means there is at least one page in the navigation stack below;
/// otherwise, <c>false</c>.</returns>
public bool CanGoBack() {
return UniWebViewPlugin.CanGoBack(gameObject.name);
}
/// <summary>
/// Determines whether this webview can go forward.
/// </summary>
/// <returns><c>true</c> if this instance can go forward, which means the user did at least once back;
/// otherwise, <c>false</c>.</returns>
public bool CanGoForward() {
return UniWebViewPlugin.CanGoForward(gameObject.name);
}
/// <summary>
/// Go to the previous page if there is any one.
/// </summary>
public void GoBack() {
UniWebViewPlugin.GoBack(gameObject.name);
}
/// <summary>
/// Go to the next page if there is any one.
/// </summary>
public void GoForward() {
UniWebViewPlugin.GoForward(gameObject.name);
}
/// <summary>
/// Add a url to white list of permission request trust sites.
/// </summary>
/// <description>
/// This method only works in Android. In Android, if you request audio or video accessibility from web page,
/// you should add the site url to a white list by using this method, or it will not be granted to use those protected resources.
/// There is no need to call it in iOS, and this method will do nothing in platforms other than Android.
/// For more about Android protected permission, see https://developer.android.com/reference/android/webkit/PermissionRequest.html.
/// </description>
/// <param name="url">URL.</param>
public void AddPermissionRequestTrustSite(string url) {
#if UNITY_ANDROID && !UNITY_EDITOR
UniWebViewPlugin.AddPermissionRequestTrustSite(gameObject.name, url);
#endif
}
/// <summary>
/// Adds the URL scheme. After be added, all link of this scheme will send a message when clicked.
/// </summary>
/// <param name="scheme">Scheme.</param>
/// <description>
/// The scheme should not contain "://". For example, if you want to receive a url like "xyz://mydomian.com", you can pass "xyz" here.
/// </description>
public void AddUrlScheme(string scheme) {
UniWebViewPlugin.AddUrlScheme(gameObject.name, scheme);
}
/// <summary>
/// Removes the URL scheme. After be removed, this kind of url will be handled by the webview.
/// </summary>
/// <param name="scheme">Scheme.</param>
public void RemoveUrlScheme(string scheme) {
UniWebViewPlugin.RemoveUrlScheme(gameObject.name, scheme);
}
/// <summary>
/// Set the header field.
/// </summary>
/// <description>
/// Specify customized header field for the next url loading.
/// You could call this method multiple times to add key/value pairs for different fields.
/// Only the requests by using Load() method of current web view will use these setting for header.
/// </description>
/// <param name="key">Key of the header field. Empty string or null will be ignored.</param>
/// <param name="value">Value of the header field. Pass null to remove an existing value. Empty string will be ignored.</param>
public void SetHeaderField(string key, string value) {
#if UNITY_WP8
Debug.LogWarning("Not implemented for Windows Phone 8.");
#else
UniWebViewPlugin.SetHeaderField(gameObject.name, key, value);
#endif
}
/// <summary>
/// Set visibility of vertical bar for the web view.
/// </summary>
/// <description>
/// This method will not work for editor and Windows Phone 8.
/// </description>
/// <param name="show">Whether the vertical scroll bar should be visible or not when the web view is being scrolled.</param>
public void SetVerticalScrollBarShow(bool show) {
#if UNITY_WP8
Debug.LogWarning("Not implemented for Windows Phone 8.");
#else
UniWebViewPlugin.SetVerticalScrollBarShow(gameObject.name, show);
#endif
}
/// <summary>
/// Set visibility of horizontal bar for the web view.
/// </summary>
/// <description>
/// This method will not work for editor and Windows Phone 8.
/// </description>
/// <param name="show">Whether the horizontal scroll bar should be visible or not when the web view is being scrolled.</param>
public void SetHorizontalScrollBarShow(bool show) {
#if UNITY_WP8
Debug.LogWarning("Not implemented for Windows Phone 8.");
#else
UniWebViewPlugin.SetHorizontalScrollBarShow(gameObject.name, show);
#endif
}
/// <summary>
/// Set web content could be debug or not. Only works for Android.
/// </summary>
/// <description>
/// You can enable Remote Debugging for Android devices by calling this method and passing true. See Google's
/// Remote Debugging Android Devices documentation for more about it.
/// https://developers.google.com/web/tools/chrome-devtools/debug/remote-debugging/remote-debugging
///
/// This method will do nothing for iOS or editor.
/// </description>
/// <param name="enabled">Whether this page could be debug or not.</param>
public static void SetWebContentsDebuggingEnabled(bool enabled) {
#if UNITY_ANDROID && !UNITY_EDITOR
UniWebViewPlugin.SetWebContentsDebuggingEnabled(enabled);
#endif
}
private bool OrientationChanged() {
int newHeight = UniWebViewHelper.screenHeight;
if (_lastScreenHeight != newHeight) {
_lastScreenHeight = newHeight;
return true;
} else {
return false;
}
}
public void ForceResize()
{
ResizeInternal();
}
private void ResizeInternal() {
int newHeight = UniWebViewHelper.screenHeight;
int newWidth = UniWebViewHelper.screenWidth;
UniWebViewEdgeInsets newInset = this.insets;
if (InsetsForScreenOreitation != null) {
UniWebViewOrientation orientation =
newHeight >= newWidth ? UniWebViewOrientation.Portrait : UniWebViewOrientation.LandScape;
newInset = InsetsForScreenOreitation(this, orientation);
}
ForceUpdateInsetsInternal(newInset);
}
#region Messages from native
public void LoadWebViewComplete(string message)
{
LoadComplete(message);
}
private void LoadComplete(string message) {
bool loadSuc = string.Equals(message, "");
bool hasCompleteListener = (OnLoadComplete != null);
if (loadSuc) {
if (hasCompleteListener) {
OnLoadComplete(this, true, null);
}
if (autoShowWhenLoadComplete) {
Show();
}
} else {
Debug.LogWarning("Web page load failed: " + gameObject.name + "; url: " + url + "; error:" + message);
#if UNITY_EDITOR
if (message.Contains("App Transport Security")) {
Debug.LogWarning("It seems that ATS is enabled in Editor. You can not load http pages when it is on. Please visit our help center for more information: https://onevcat.zendesk.com/hc/en-us/articles/215527307-I-cannot-open-the-web-page-in-Unity-Editor-");
}
#endif
if (hasCompleteListener) {
OnLoadComplete(this, false, message);
}
}
}
private void LoadBegin(string url) {
if (OnLoadBegin != null) {
OnLoadBegin(this, url);
}
}
private void ReceivedMessage(string rawMessage) {
UniWebViewMessage message = new UniWebViewMessage(rawMessage);
if (OnReceivedMessage != null) {
OnReceivedMessage(this,message);
}
}
private void WebViewDone(string message) {
bool destroy = true;
if (OnWebViewShouldClose != null) {
destroy = OnWebViewShouldClose(this);
}
if (destroy) {
Hide();
Destroy(this);
}
}
private void WebViewKeyDown(string message) {
int keyCode = Convert.ToInt32(message);
if (OnReceivedKeyCode != null) {
OnReceivedKeyCode(this, keyCode);
}
}
private void EvalJavaScriptFinished(string result) {
if (OnEvalJavaScriptFinished != null) {
OnEvalJavaScriptFinished(this, result);
}
}
private void AnimationFinished(string identifier) {
}
private void ShowTransitionFinished(string message) {
if (_showTransitionAction != null) {
_showTransitionAction();
_showTransitionAction = null;
}
}
private void HideTransitionFinished(string message) {
if (_hideTransitionAction != null) {
_hideTransitionAction();
_hideTransitionAction = null;
}
}
private IEnumerator LoadFromJarPackage(string jarFilePath) {
WWW stream = new WWW(jarFilePath);
yield return stream;
if (stream.error != null) {
if (OnLoadComplete != null) {
OnLoadComplete(this,false,stream.error);
}
yield break;
} else {
LoadHTMLString(stream.text, "");
}
}
#endregion
#region Life Cycle
void Awake() {
_currentGUID = System.Guid.NewGuid().ToString();
gameObject.name = gameObject.name + _currentGUID;
UniWebViewPlugin.Init(gameObject.name,
this.insets.top,
this.insets.left,
this.insets.bottom,
this.insets.right);
_lastScreenHeight = UniWebViewHelper.screenHeight;
#if UNITY_EDITOR
_screenScale = UniWebViewHelper.screenScale;
CreateTexture(this.insets.left,
this.insets.bottom,
Screen.width - this.insets.left - this.insets.right,
Screen.height - this.insets.top - this.insets.bottom
);
#endif
}
void Start() {
if (loadOnStart) {
Load();
}
}
private void OnDestroy() {
#if UNITY_EDITOR
Clean();
#endif
RemoveAllListeners();
UniWebViewPlugin.Destroy(gameObject.name);
gameObject.name = gameObject.name.Replace(_currentGUID, "");
}
private void RemoveAllListeners() {
this.OnLoadBegin = null;
this.OnLoadComplete = null;
this.OnReceivedMessage = null;
this.OnReceivedKeyCode = null;
this.OnEvalJavaScriptFinished = null;
this.OnWebViewShouldClose = null;
this.InsetsForScreenOreitation = null;
}
#endregion
private void Update() {
#if UNITY_EDITOR
if (Application.platform == RuntimePlatform.OSXEditor) {
if (_webViewId != 0 && !_hidden) {
if (Input.GetKeyDown(KeyCode.Escape)) {
if (UniWebViewPlugin.CanGoBack(gameObject.name)) {
UniWebViewPlugin.GoBack(gameObject.name);
} else {
WebViewDone("");
}
} else {
_inputString += Input.inputString;
}
}
}
#endif
//Handle screen auto orientation.
if (OrientationChanged()) {
ResizeInternal();
}
}
#region UnityEditor Debug
#if UNITY_EDITOR
private Rect _webViewRect;
private Texture2D _texture;
private string _inputString;
private int _webViewId;
private bool _hidden;
private IntPtr _renderCallback;
private int _screenScale;
private void CreateTexture(int x, int y, int width, int height) {
if (Application.platform == RuntimePlatform.OSXEditor) {
_webViewRect = new Rect(x, y, width, height);
_texture = new Texture2D(width * 2, height * 2, TextureFormat.ARGB32, false);
_texture = new Texture2D(width * _screenScale, height * _screenScale, TextureFormat.ARGB32, false);
}
}
private void Clean() {
if (Application.platform == RuntimePlatform.OSXEditor) {
Destroy(_texture);
_webViewId = 0;
_texture = null;
}
}
private void OnGUI()
{
if (Application.platform == RuntimePlatform.OSXEditor) {
if (_webViewId != 0 && !_hidden) {
Vector3 pos = Input.mousePosition;
bool down = Input.GetMouseButton(0);
bool press = Input.GetMouseButtonDown(0);
bool release = Input.GetMouseButtonUp(0);
float deltaY = Input.GetAxis("Mouse ScrollWheel");
bool keyPress = false;
string keyChars = "";
short keyCode = 0;
if (!string.IsNullOrEmpty(_inputString)) {
keyPress = true;
keyChars = _inputString.Substring(0, 1);
keyCode = (short)_inputString[0];
_inputString = _inputString.Substring(1);
}
var id = _texture.GetNativeTexturePtr().ToInt32();
UniWebViewPlugin.InputEvent(gameObject.name,
(int)(pos.x - _webViewRect.x), (int)(pos.y - _webViewRect.y), deltaY,
down, press, release, keyPress, keyCode, keyChars,
id);
#if UNITY_5_0 || UNITY_5_1 || UNITY_5_2_0
GL.IssuePluginEvent(_webViewId);
#else
GL.IssuePluginEvent(UniWebViewPlugin.GetRenderEventFunc(), _webViewId);
#endif
Matrix4x4 m = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(new Vector3(0, Screen.height, 0),
Quaternion.identity, new Vector3(1, -1, 1));
GUI.DrawTexture(_webViewRect, _texture);
GUI.matrix = m;
}
}
}
#endif
#endregion
}
#endif //UNITY_IOS || UNITY_ANDROID
|