Skip to content

Commit

Permalink
updated binaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
KojiNakamaru committed Oct 25, 2023
1 parent 951e89b commit c3b30ad
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 10 deletions.
Binary file modified dist/package-nofragment/Assets/Plugins/Android/WebViewPlugin.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public void OnPostGenerateGradleAndroidProject(string basePath) {
}
}
changed = (androidManifest.SetExported(true) || changed);
changed = (androidManifest.SetApplicationTheme("@style/UnityThemeSelector") || changed);
changed = (androidManifest.SetActivityTheme("@style/UnityThemeSelector.Translucent") || changed);
changed = (androidManifest.SetHardwareAccelerated(true) || changed);
#if UNITYWEBVIEW_ANDROID_USES_CLEARTEXT_TRAFFIC
changed = (androidManifest.SetUsesCleartextTraffic(true) || changed);
Expand All @@ -87,6 +89,9 @@ public void OnPostGenerateGradleAndroidProject(string basePath) {
#if UNITYWEBVIEW_ANDROID_ENABLE_MICROPHONE
changed = (androidManifest.AddMicrophone() || changed);
#endif
//#if UNITY_5_6_0 || UNITY_5_6_1
changed = (androidManifest.SetActivityName("net.gree.unitywebview.CUnityPlayerActivity") || changed);
//#endif
if (changed) {
androidManifest.Save();
Debug.Log("unitywebview: adjusted AndroidManifest.xml.");
Expand Down Expand Up @@ -174,9 +179,9 @@ public static void OnPostprocessBuild(BuildTarget buildTarget, string path) {
#if UNITYWEBVIEW_ANDROID_ENABLE_MICROPHONE
changed = (androidManifest.AddMicrophone() || changed);
#endif
#if UNITY_5_6_0 || UNITY_5_6_1
//#if UNITY_5_6_0 || UNITY_5_6_1
changed = (androidManifest.SetActivityName("net.gree.unitywebview.CUnityPlayerActivity") || changed);
#endif
//#endif
if (changed) {
androidManifest.Save();
Debug.LogError("unitywebview: adjusted AndroidManifest.xml. Please rebuild the app.");
Expand Down Expand Up @@ -238,6 +243,118 @@ public static void OnPostprocessBuild(BuildTarget buildTarget, string path) {
dst = (string)method.Invoke(proj, null);
}
File.WriteAllText(projPath, dst);

// Classes/UI/UnityView.h
{
var lines0 = File.ReadAllText(path + "/Classes/UI/UnityView.h").Split('\n');
var lines = new List<string>();
var phase = 0;
foreach (var line in lines0) {
switch (phase) {
case 0:
lines.Add(line);
if (line.StartsWith("@interface UnityView : UnityRenderingView")) {
phase++;
}
break;
case 1:
lines.Add(line);
if (line.StartsWith("}")) {
phase++;
lines.Add("");
lines.Add("- (void)clearMasks;");
lines.Add("- (void)addMask:(CGRect)r;");
}
break;
default:
lines.Add(line);
break;
}
}
File.WriteAllText(path + "/Classes/UI/UnityView.h", string.Join("\n", lines));
}
// Classes/UI/UnityView.mm
{
var lines0 = File.ReadAllText(path + "/Classes/UI/UnityView.mm").Split('\n');
var lines = new List<string>();
var phase = 0;
foreach (var line in lines0) {
switch (phase) {
case 0:
lines.Add(line);
if (line.StartsWith("@implementation UnityView")) {
phase++;
}
break;
case 1:
if (line.StartsWith("}")) {
phase++;
lines.Add(" NSMutableArray<NSValue *> *_masks;");
lines.Add(line);
lines.Add(@"
- (void)clearMasks
{
if (_masks == nil) {
_masks = [[NSMutableArray<NSValue *> alloc] init];
}
[_masks removeAllObjects];
}
- (void)addMask:(CGRect)r
{
if (_masks == nil) {
_masks = [[NSMutableArray<NSValue *> alloc] init];
}
[_masks addObject:[NSValue valueWithCGRect:r]];
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
//CGRect mask = CGRectMake(0, 0, 1334, 100);
//return CGRectContainsPoint(mask, point);
for (NSValue *v in _masks) {
if (CGRectContainsPoint([v CGRectValue], point)) {
return TRUE;
}
}
return FALSE;
}
");
} else {
lines.Add(line);
}
break;
default:
lines.Add(line);
break;
}
}
lines.Add(@"
extern ""C"" {
UIView *UnityGetGLView();
void CWebViewPlugin_ClearMasks();
void CWebViewPlugin_AddMask(int x, int y, int w, int h);
}
void CWebViewPlugin_ClearMasks()
{
[(UnityView *)UnityGetGLView() clearMasks];
}
void CWebViewPlugin_AddMask(int x, int y, int w, int h)
{
UIView *view = UnityGetGLViewController().view;
CGFloat scale = 1.0f;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
scale = view.window.screen.nativeScale;
} else {
scale = view.contentScaleFactor;
}
[(UnityView *)UnityGetGLView() addMask:CGRectMake(x / scale, y / scale, w / scale, h / scale)];
}
");
File.WriteAllText(path + "/Classes/UI/UnityView.mm", string.Join("\n", lines));
}
}
}
}
Expand Down Expand Up @@ -314,6 +431,25 @@ internal bool SetExported(bool enabled) {
return changed;
}

internal bool SetApplicationTheme(string theme) {
bool changed = false;
if (ApplicationElement.GetAttribute("theme", AndroidXmlNamespace) != theme) {
ApplicationElement.SetAttribute("theme", AndroidXmlNamespace, theme);
changed = true;
}
return changed;
}

internal bool SetActivityTheme(string theme) {
bool changed = false;
var activity = GetActivityWithLaunchIntent() as XmlElement;
if (activity.GetAttribute("theme", AndroidXmlNamespace) != theme) {
activity.SetAttribute("theme", AndroidXmlNamespace, theme);
changed = true;
}
return changed;
}

internal bool SetHardwareAccelerated(bool enabled) {
bool changed = false;
var activity = GetActivityWithLaunchIntent() as XmlElement;
Expand Down
56 changes: 55 additions & 1 deletion dist/package-nofragment/Assets/Plugins/WebViewObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class WebViewObject : MonoBehaviour
Callback onLoaded;
Callback onHooked;
Callback onCookies;
bool paused;
bool visibility;
bool alertDialogEnabled;
bool scrollBounceEnabled;
Expand Down Expand Up @@ -95,6 +96,7 @@ public class WebViewObject : MonoBehaviour

void OnApplicationPause(bool paused)
{
this.paused = paused;
if (webView == null)
return;
// if (!paused && mKeyboardVisibleHeight > 0)
Expand All @@ -107,6 +109,28 @@ void OnApplicationPause(bool paused)

void Update()
{
// NOTE:
//
// When OnApplicationPause(true) is called and the app is in closing, webView.Call(...)
// after that could cause crashes because underlying java instances were closed.
//
// This has not been cleary confirmed yet. However, as Update() is called once after
// OnApplicationPause(true), it is likely correct.
//
// Base on this assumption, we do nothing here if the app is paused.
//
// cf. https://github.com/gree/unity-webview/issues/991#issuecomment-1776628648
// cf. https://docs.unity3d.com/2020.3/Documentation/Manual/ExecutionOrder.html
//
// In between frames
//
// * OnApplicationPause: This is called at the end of the frame where the pause is detected,
// effectively between the normal frame updates. One extra frame will be issued after
// OnApplicationPause is called to allow the game to show graphics that indicate the
// paused state.
//
if (paused)
return;
if (webView == null)
return;
#if UNITYWEBVIEW_ANDROID_ENABLE_NAVIGATOR_ONLINE
Expand Down Expand Up @@ -468,6 +492,10 @@ private static extern void _CWebViewPlugin_Reload(
[DllImport("WebView")]
private static extern string _CWebViewPlugin_GetMessage(IntPtr instance);
#elif UNITY_IPHONE
[DllImport("__Internal")]
private static extern void CWebViewPlugin_ClearMasks();
[DllImport("__Internal")]
private static extern void CWebViewPlugin_AddMask(int x, int y, int w, int h);
[DllImport("__Internal")]
private static extern IntPtr _CWebViewPlugin_Init(string gameObject, bool transparent, bool zoom, string ua, bool enableWKWebView, int wkContentMode, bool wkAllowsLinkPreview, bool wkAllowsBackForwardNavigationGestures, int radius);
[DllImport("__Internal")]
Expand Down Expand Up @@ -564,6 +592,32 @@ public static bool IsWebViewAvailable()
#endif
}

public static void ClearMasks()
{
#if !UNITY_EDITOR && UNITY_ANDROID
using(AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
var activity = UnityClass.GetStatic<AndroidJavaObject>("currentActivity");
activity.Call("clearMasks");
}
#elif !UNITY_EDITOR && UNITY_IPHONE
CWebViewPlugin_ClearMasks();
#endif
}

public static void AddMask(int x, int y, int w, int h)
{
#if !UNITY_EDITOR && UNITY_ANDROID
using(AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
var activity = UnityClass.GetStatic<AndroidJavaObject>("currentActivity");
activity.Call("addMask", x, y, w, h);
}
#elif !UNITY_EDITOR && UNITY_IPHONE
CWebViewPlugin_AddMask(x, y, w, h);
#endif
}

public void Init(
Callback cb = null,
Callback err = null,
Expand Down Expand Up @@ -639,7 +693,7 @@ public void Init(
webView = _CWebViewPlugin_Init(name, transparent, zoom, ua, enableWKWebView, wkContentMode, wkAllowsLinkPreview, wkAllowsBackForwardNavigationGestures, radius);
#elif UNITY_ANDROID
webView = new AndroidJavaObject("net.gree.unitywebview.CWebViewPlugin");
#if UNITY_2022_1_OR_NEWER
#if UNITY_2021_1_OR_NEWER
webView.SetStatic<bool>("forceBringToFront", true);
#endif
webView.Call("Init", name, transparent, zoom, androidForceDarkMode, ua, radius);
Expand Down
2 changes: 1 addition & 1 deletion dist/package-nofragment/Assets/Plugins/iOS/WebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ - (id)initWithGameObjectName:(const char *)gameObjectName_ transparent:(BOOL)tra

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

[view addSubview:webView];
[view.superview insertSubview:webView atIndex:0];

return self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ - (id)initWithGameObjectName:(const char *)gameObjectName_ transparent:(BOOL)tra

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

[view addSubview:webView];
[view.superview insertSubview:webView atIndex:0];

return self;
}
Expand Down
Binary file modified dist/package/Assets/Plugins/Android/WebViewPlugin.aar
Binary file not shown.
Loading

0 comments on commit c3b30ad

Please sign in to comment.