-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathWebMediator.js
171 lines (137 loc) · 5.07 KB
/
WebMediator.js
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
#pragma strict
// WebView-Unity mediator plugin script.
import System.Runtime.InteropServices;
// Message container class.
class WebMediatorMessage {
var path : String; // Message path
var args : Hashtable; // Argument table
function WebMediatorMessage(rawMessage : String) {
// Retrieve a path.
var split = rawMessage.Split("?"[0]);
path = split[0];
// Parse arguments.
args = new Hashtable();
if (split.Length > 1) {
for (var pair in split[1].Split("&"[0])) {
var elems = pair.Split("="[0]);
args[elems[0]] = WWW.UnEscapeURL(elems[1]);
}
}
}
}
private static var instance : WebMediator;
private static var isClearCache : boolean;
private var lastRequestedUrl : String;
private var loadRequest : boolean;
private var visibility : boolean;
private var leftMargin : int;
private var topMargin : int;
private var rightMargin : int;
private var bottomMargin : int;
// Install the plugin.
// Call this at least once before using the plugin.
static function Install() {
if (instance == null) {
var master = new GameObject("WebMediator");
DontDestroyOnLoad(master);
instance = master.AddComponent.<WebMediator>();
InstallPlatform();
}
}
// Set margins around the web view.
static function SetMargin(left : int, top: int, right : int, bottom : int) {
instance.leftMargin = left;
instance.topMargin = top;
instance.rightMargin = right;
instance.bottomMargin = bottom;
ApplyMarginsPlatform();
}
// Visibility functions.
static function Show() {
instance.visibility = true;
}
static function Hide() {
instance.visibility = false;
}
static function IsVisible() {
return instance.visibility;
}
static function SetClearCache()
{
isClearCache = true;
}
static function SetCache()
{
isClearCache = false;
}
// Load the page at the URL.
static function LoadUrl(url : String) {
instance.lastRequestedUrl = url;
instance.loadRequest = true;
}
function Update() {
UpdatePlatform();
instance.loadRequest = false;
}
#if UNITY_EDITOR
// Unity Editor implementation.
private static function InstallPlatform() { }
private static function UpdatePlatform() { }
private static function ApplyMarginsPlatform() { }
static function PollMessage() : WebMediatorMessage { return null; }
static function MakeTransparentWebViewBackground() { }
#elif UNITY_IPHONE
// iOS platform implementation.
@DllImportAttribute("__Internal") static private function _WebViewPluginInstall() {}
@DllImportAttribute("__Internal") static private function _WebViewPluginLoadUrl(url : String, isClearCache : boolean) {}
@DllImportAttribute("__Internal") static private function _WebViewPluginSetVisibility(visibility : boolean) {}
@DllImportAttribute("__Internal") static private function _WebViewPluginSetMargins(left : int, top : int, right : int, bottom : int) {}
@DllImportAttribute("__Internal") static private function _WebViewPluginPollMessage() : String {}
@DllImportAttribute("__Internal") static private function _WebViewPluginMakeTransparentBackground() {}
private static var viewVisibility : boolean;
private static function InstallPlatform() {
_WebViewPluginInstall();
}
private static function ApplyMarginsPlatform() {
_WebViewPluginSetMargins(instance.leftMargin, instance.topMargin, instance.rightMargin, instance.bottomMargin);
}
private static function UpdatePlatform() {
if (viewVisibility != instance.visibility) {
viewVisibility = instance.visibility;
_WebViewPluginSetVisibility(viewVisibility);
}
if (instance.loadRequest) {
instance.loadRequest = false;
_WebViewPluginLoadUrl(instance.lastRequestedUrl, isClearCache);
}
}
static function PollMessage() : WebMediatorMessage {
var message = _WebViewPluginPollMessage();
return message ? new WebMediatorMessage(message) : null;
}
static function MakeTransparentWebViewBackground()
{
_WebViewPluginMakeTransparentBackground();
}
#elif UNITY_ANDROID
// Android platform implementation.
private static var unityPlayerClass : AndroidJavaClass;
private static function InstallPlatform() {
unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
}
private static function ApplyMarginsPlatform() { }
private static function UpdatePlatform() {
var activity = unityPlayerClass.GetStatic.<AndroidJavaObject>("currentActivity");
activity.Call("updateWebView", instance.lastRequestedUrl ? instance.lastRequestedUrl : "", instance.loadRequest, instance.visibility, instance.leftMargin, instance.topMargin, instance.rightMargin, instance.bottomMargin);
}
static function PollMessage() : WebMediatorMessage {
var activity = unityPlayerClass.GetStatic.<AndroidJavaObject>("currentActivity");
var message = activity.Call.<String>("pollWebViewMessage");
return message ? new WebMediatorMessage(message) : null;
}
static function MakeTransparentWebViewBackground()
{
var activity = unityPlayerClass.GetStatic.<AndroidJavaObject>("currentActivity");
activity.Call("makeTransparentWebViewBackground");
}
#endif