Skip to content

Commit c3b30ad

Browse files
committed
updated binaries.
1 parent 951e89b commit c3b30ad

14 files changed

+390
-10
lines changed
Binary file not shown.

dist/package-nofragment/Assets/Plugins/Editor/UnityWebViewPostprocessBuild.cs

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public void OnPostGenerateGradleAndroidProject(string basePath) {
7676
}
7777
}
7878
changed = (androidManifest.SetExported(true) || changed);
79+
changed = (androidManifest.SetApplicationTheme("@style/UnityThemeSelector") || changed);
80+
changed = (androidManifest.SetActivityTheme("@style/UnityThemeSelector.Translucent") || changed);
7981
changed = (androidManifest.SetHardwareAccelerated(true) || changed);
8082
#if UNITYWEBVIEW_ANDROID_USES_CLEARTEXT_TRAFFIC
8183
changed = (androidManifest.SetUsesCleartextTraffic(true) || changed);
@@ -87,6 +89,9 @@ public void OnPostGenerateGradleAndroidProject(string basePath) {
8789
#if UNITYWEBVIEW_ANDROID_ENABLE_MICROPHONE
8890
changed = (androidManifest.AddMicrophone() || changed);
8991
#endif
92+
//#if UNITY_5_6_0 || UNITY_5_6_1
93+
changed = (androidManifest.SetActivityName("net.gree.unitywebview.CUnityPlayerActivity") || changed);
94+
//#endif
9095
if (changed) {
9196
androidManifest.Save();
9297
Debug.Log("unitywebview: adjusted AndroidManifest.xml.");
@@ -174,9 +179,9 @@ public static void OnPostprocessBuild(BuildTarget buildTarget, string path) {
174179
#if UNITYWEBVIEW_ANDROID_ENABLE_MICROPHONE
175180
changed = (androidManifest.AddMicrophone() || changed);
176181
#endif
177-
#if UNITY_5_6_0 || UNITY_5_6_1
182+
//#if UNITY_5_6_0 || UNITY_5_6_1
178183
changed = (androidManifest.SetActivityName("net.gree.unitywebview.CUnityPlayerActivity") || changed);
179-
#endif
184+
//#endif
180185
if (changed) {
181186
androidManifest.Save();
182187
Debug.LogError("unitywebview: adjusted AndroidManifest.xml. Please rebuild the app.");
@@ -238,6 +243,118 @@ public static void OnPostprocessBuild(BuildTarget buildTarget, string path) {
238243
dst = (string)method.Invoke(proj, null);
239244
}
240245
File.WriteAllText(projPath, dst);
246+
247+
// Classes/UI/UnityView.h
248+
{
249+
var lines0 = File.ReadAllText(path + "/Classes/UI/UnityView.h").Split('\n');
250+
var lines = new List<string>();
251+
var phase = 0;
252+
foreach (var line in lines0) {
253+
switch (phase) {
254+
case 0:
255+
lines.Add(line);
256+
if (line.StartsWith("@interface UnityView : UnityRenderingView")) {
257+
phase++;
258+
}
259+
break;
260+
case 1:
261+
lines.Add(line);
262+
if (line.StartsWith("}")) {
263+
phase++;
264+
lines.Add("");
265+
lines.Add("- (void)clearMasks;");
266+
lines.Add("- (void)addMask:(CGRect)r;");
267+
}
268+
break;
269+
default:
270+
lines.Add(line);
271+
break;
272+
}
273+
}
274+
File.WriteAllText(path + "/Classes/UI/UnityView.h", string.Join("\n", lines));
275+
}
276+
// Classes/UI/UnityView.mm
277+
{
278+
var lines0 = File.ReadAllText(path + "/Classes/UI/UnityView.mm").Split('\n');
279+
var lines = new List<string>();
280+
var phase = 0;
281+
foreach (var line in lines0) {
282+
switch (phase) {
283+
case 0:
284+
lines.Add(line);
285+
if (line.StartsWith("@implementation UnityView")) {
286+
phase++;
287+
}
288+
break;
289+
case 1:
290+
if (line.StartsWith("}")) {
291+
phase++;
292+
lines.Add(" NSMutableArray<NSValue *> *_masks;");
293+
lines.Add(line);
294+
lines.Add(@"
295+
- (void)clearMasks
296+
{
297+
if (_masks == nil) {
298+
_masks = [[NSMutableArray<NSValue *> alloc] init];
299+
}
300+
[_masks removeAllObjects];
301+
}
302+
303+
- (void)addMask:(CGRect)r
304+
{
305+
if (_masks == nil) {
306+
_masks = [[NSMutableArray<NSValue *> alloc] init];
307+
}
308+
[_masks addObject:[NSValue valueWithCGRect:r]];
309+
}
310+
311+
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
312+
{
313+
//CGRect mask = CGRectMake(0, 0, 1334, 100);
314+
//return CGRectContainsPoint(mask, point);
315+
for (NSValue *v in _masks) {
316+
if (CGRectContainsPoint([v CGRectValue], point)) {
317+
return TRUE;
318+
}
319+
}
320+
return FALSE;
321+
}
322+
");
323+
} else {
324+
lines.Add(line);
325+
}
326+
break;
327+
default:
328+
lines.Add(line);
329+
break;
330+
}
331+
}
332+
lines.Add(@"
333+
extern ""C"" {
334+
UIView *UnityGetGLView();
335+
void CWebViewPlugin_ClearMasks();
336+
void CWebViewPlugin_AddMask(int x, int y, int w, int h);
337+
}
338+
339+
void CWebViewPlugin_ClearMasks()
340+
{
341+
[(UnityView *)UnityGetGLView() clearMasks];
342+
}
343+
344+
void CWebViewPlugin_AddMask(int x, int y, int w, int h)
345+
{
346+
UIView *view = UnityGetGLViewController().view;
347+
CGFloat scale = 1.0f;
348+
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
349+
scale = view.window.screen.nativeScale;
350+
} else {
351+
scale = view.contentScaleFactor;
352+
}
353+
[(UnityView *)UnityGetGLView() addMask:CGRectMake(x / scale, y / scale, w / scale, h / scale)];
354+
}
355+
");
356+
File.WriteAllText(path + "/Classes/UI/UnityView.mm", string.Join("\n", lines));
357+
}
241358
}
242359
}
243360
}
@@ -314,6 +431,25 @@ internal bool SetExported(bool enabled) {
314431
return changed;
315432
}
316433

434+
internal bool SetApplicationTheme(string theme) {
435+
bool changed = false;
436+
if (ApplicationElement.GetAttribute("theme", AndroidXmlNamespace) != theme) {
437+
ApplicationElement.SetAttribute("theme", AndroidXmlNamespace, theme);
438+
changed = true;
439+
}
440+
return changed;
441+
}
442+
443+
internal bool SetActivityTheme(string theme) {
444+
bool changed = false;
445+
var activity = GetActivityWithLaunchIntent() as XmlElement;
446+
if (activity.GetAttribute("theme", AndroidXmlNamespace) != theme) {
447+
activity.SetAttribute("theme", AndroidXmlNamespace, theme);
448+
changed = true;
449+
}
450+
return changed;
451+
}
452+
317453
internal bool SetHardwareAccelerated(bool enabled) {
318454
bool changed = false;
319455
var activity = GetActivityWithLaunchIntent() as XmlElement;

dist/package-nofragment/Assets/Plugins/WebViewObject.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class WebViewObject : MonoBehaviour
5959
Callback onLoaded;
6060
Callback onHooked;
6161
Callback onCookies;
62+
bool paused;
6263
bool visibility;
6364
bool alertDialogEnabled;
6465
bool scrollBounceEnabled;
@@ -95,6 +96,7 @@ public class WebViewObject : MonoBehaviour
9596

9697
void OnApplicationPause(bool paused)
9798
{
99+
this.paused = paused;
98100
if (webView == null)
99101
return;
100102
// if (!paused && mKeyboardVisibleHeight > 0)
@@ -107,6 +109,28 @@ void OnApplicationPause(bool paused)
107109

108110
void Update()
109111
{
112+
// NOTE:
113+
//
114+
// When OnApplicationPause(true) is called and the app is in closing, webView.Call(...)
115+
// after that could cause crashes because underlying java instances were closed.
116+
//
117+
// This has not been cleary confirmed yet. However, as Update() is called once after
118+
// OnApplicationPause(true), it is likely correct.
119+
//
120+
// Base on this assumption, we do nothing here if the app is paused.
121+
//
122+
// cf. https://github.com/gree/unity-webview/issues/991#issuecomment-1776628648
123+
// cf. https://docs.unity3d.com/2020.3/Documentation/Manual/ExecutionOrder.html
124+
//
125+
// In between frames
126+
//
127+
// * OnApplicationPause: This is called at the end of the frame where the pause is detected,
128+
// effectively between the normal frame updates. One extra frame will be issued after
129+
// OnApplicationPause is called to allow the game to show graphics that indicate the
130+
// paused state.
131+
//
132+
if (paused)
133+
return;
110134
if (webView == null)
111135
return;
112136
#if UNITYWEBVIEW_ANDROID_ENABLE_NAVIGATOR_ONLINE
@@ -468,6 +492,10 @@ private static extern void _CWebViewPlugin_Reload(
468492
[DllImport("WebView")]
469493
private static extern string _CWebViewPlugin_GetMessage(IntPtr instance);
470494
#elif UNITY_IPHONE
495+
[DllImport("__Internal")]
496+
private static extern void CWebViewPlugin_ClearMasks();
497+
[DllImport("__Internal")]
498+
private static extern void CWebViewPlugin_AddMask(int x, int y, int w, int h);
471499
[DllImport("__Internal")]
472500
private static extern IntPtr _CWebViewPlugin_Init(string gameObject, bool transparent, bool zoom, string ua, bool enableWKWebView, int wkContentMode, bool wkAllowsLinkPreview, bool wkAllowsBackForwardNavigationGestures, int radius);
473501
[DllImport("__Internal")]
@@ -564,6 +592,32 @@ public static bool IsWebViewAvailable()
564592
#endif
565593
}
566594

595+
public static void ClearMasks()
596+
{
597+
#if !UNITY_EDITOR && UNITY_ANDROID
598+
using(AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
599+
{
600+
var activity = UnityClass.GetStatic<AndroidJavaObject>("currentActivity");
601+
activity.Call("clearMasks");
602+
}
603+
#elif !UNITY_EDITOR && UNITY_IPHONE
604+
CWebViewPlugin_ClearMasks();
605+
#endif
606+
}
607+
608+
public static void AddMask(int x, int y, int w, int h)
609+
{
610+
#if !UNITY_EDITOR && UNITY_ANDROID
611+
using(AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
612+
{
613+
var activity = UnityClass.GetStatic<AndroidJavaObject>("currentActivity");
614+
activity.Call("addMask", x, y, w, h);
615+
}
616+
#elif !UNITY_EDITOR && UNITY_IPHONE
617+
CWebViewPlugin_AddMask(x, y, w, h);
618+
#endif
619+
}
620+
567621
public void Init(
568622
Callback cb = null,
569623
Callback err = null,
@@ -639,7 +693,7 @@ public void Init(
639693
webView = _CWebViewPlugin_Init(name, transparent, zoom, ua, enableWKWebView, wkContentMode, wkAllowsLinkPreview, wkAllowsBackForwardNavigationGestures, radius);
640694
#elif UNITY_ANDROID
641695
webView = new AndroidJavaObject("net.gree.unitywebview.CWebViewPlugin");
642-
#if UNITY_2022_1_OR_NEWER
696+
#if UNITY_2021_1_OR_NEWER
643697
webView.SetStatic<bool>("forceBringToFront", true);
644698
#endif
645699
webView.Call("Init", name, transparent, zoom, androidForceDarkMode, ua, radius);

dist/package-nofragment/Assets/Plugins/iOS/WebView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ - (id)initWithGameObjectName:(const char *)gameObjectName_ transparent:(BOOL)tra
260260

261261
[webView addObserver:self forKeyPath: @"loading" options: NSKeyValueObservingOptionNew context:nil];
262262

263-
[view addSubview:webView];
263+
[view.superview insertSubview:webView atIndex:0];
264264

265265
return self;
266266
}

dist/package-nofragment/Assets/Plugins/iOS/WebViewWithUIWebView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ - (id)initWithGameObjectName:(const char *)gameObjectName_ transparent:(BOOL)tra
320320

321321
[webView addObserver:self forKeyPath: @"loading" options: NSKeyValueObservingOptionNew context:nil];
322322

323-
[view addSubview:webView];
323+
[view.superview insertSubview:webView atIndex:0];
324324

325325
return self;
326326
}
Binary file not shown.

0 commit comments

Comments
 (0)