Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EXPERIMENTAL] render over native ui #584

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
943e8c4
adjusted ios code.
KojiNakamaru Aug 20, 2020
6fb7ed2
adjusted android code. (cf. c7b487e5eee7de485437f30dc70186802462d844 )
KojiNakamaru Mar 11, 2023
675d8db
modified UnityWebViewPostprocessBuild.cs to fix unity's incorrect the…
KojiNakamaru Mar 11, 2023
8a6197f
commented out layout.bringToFront() in CWebViewPlugin.java.
KojiNakamaru Oct 10, 2023
40e71b8
modified to pass touch events (filtered with rectangular masks) to we…
KojiNakamaru Oct 13, 2023
97e6205
updated SampleWebView.cs to call ClearMasks/AddMask.
KojiNakamaru Oct 13, 2023
7b45aee
declared ClearMasks/AddMasks as static methods.
KojiNakamaru Oct 17, 2023
b99aaa1
updated webViewObject.{ClearMasks,AddMask} -> WebViewObject.{ClearMas…
KojiNakamaru Oct 17, 2023
5265fe3
fixed to consider the device pixel ratio.
KojiNakamaru Oct 18, 2023
cc55ac3
made CUnityAndroidActivity opaque and fixed about layout.setBackgroun…
KojiNakamaru Nov 10, 2023
8e55ce6
fixed CUnityActivity.java to make the activity opaque (the former imp…
KojiNakamaru Nov 17, 2023
99caa99
fixed iOS code to allow select tag menu, etc.
KojiNakamaru Nov 30, 2023
a7339e4
fixed to set autoresizingMask.
KojiNakamaru Dec 4, 2023
a312f01
fixed to make mVideoView receive events.
KojiNakamaru Dec 5, 2023
7231233
updated binaries.
KojiNakamaru Dec 5, 2023
c472653
Merge branch 'master' into experimental/unity-over-native-ui
KojiNakamaru Dec 23, 2023
d0e0f3e
updated binaries.
KojiNakamaru Dec 23, 2023
c6c5aa1
Merge branch 'master' into experimental/unity-over-native-ui
KojiNakamaru Jul 11, 2024
163012e
added --zorderpatch option (as this branch requires CUnityPlayer*.java).
KojiNakamaru Jul 11, 2024
4426a65
updated binaries.
KojiNakamaru Jul 11, 2024
be7c7c4
Merge branch 'master' into experimental/unity-over-native-ui
KojiNakamaru Dec 20, 2024
61ade6a
fixed to disable bringToFront() (could be further polished by 340bb8a…
KojiNakamaru Dec 20, 2024
3ac1b6d
updated binaries.
KojiNakamaru Dec 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ task :build do
end
["Android"].each do |t|
Dir.chdir("#{SRCDIR[0]}/#{t}") do
sh "git clean -dxf .; ./install.sh"
sh "git clean -dxf .; ./install.sh --development"
sh "git clean -dxf .; ./install.sh --zorderpatch"
sh "git clean -dxf .; ./install.sh --zorderpatch --development"
end
end
["Mac"].each do |t|
Expand Down Expand Up @@ -102,8 +102,8 @@ task :buildnofragment do
end
["Android"].each do |t|
Dir.chdir("#{SRCDIR[0]}/#{t}") do
sh "git clean -dxf .; ./install.sh --nofragment"
sh "git clean -dxf .; ./install.sh --nofragment --development"
sh "git clean -dxf .; ./install.sh --zorderpatch --nofragment"
sh "git clean -dxf .; ./install.sh --zorderpatch --nofragment --development"
end
end
["Mac"].each do |t|
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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.SetWindowSoftInputMode("adjustPan") || changed);
changed = (androidManifest.SetHardwareAccelerated(true) || changed);
#if UNITYWEBVIEW_ANDROID_USES_CLEARTEXT_TRAFFIC
Expand All @@ -107,6 +109,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 @@ -217,9 +222,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 and/or WebView.aar. Please rebuild the app.");
Expand Down Expand Up @@ -281,6 +286,133 @@ public static void OnPostprocessBuild(BuildTarget buildTarget, string path) {
dst = (string)method.Invoke(proj, null);
}
File.WriteAllText(projPath, dst);

// Classes/UI/UnityAppController+ViewHandling.mm
{
var text = File.ReadAllText(path + "/Classes/UI/UnityAppController+ViewHandling.mm");
text = text.Replace(
@"
_rootController.view = _rootView = _unityView;
",
@"
UIView *view = [[UIView alloc] initWithFrame:controller.view.bounds];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[view addSubview:_unityView];
_rootController.view = _rootView = view;
");
File.WriteAllText(path + "/Classes/UI/UnityAppController+ViewHandling.mm", text);
}
// 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 @@ -357,6 +489,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 SetWindowSoftInputMode(string mode) {
bool changed = false;
var activity = GetActivityWithLaunchIntent() as XmlElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>BuildMachineOSBuild</key>
<string>23H124</string>
<string>23H311</string>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
Expand All @@ -29,19 +29,19 @@
<key>DTCompiler</key>
<string>com.apple.compilers.llvm.clang.1_0</string>
<key>DTPlatformBuild</key>
<string></string>
<string>24B75</string>
<key>DTPlatformName</key>
<string>macosx</string>
<key>DTPlatformVersion</key>
<string>14.5</string>
<string>15.1</string>
<key>DTSDKBuild</key>
<string>23F73</string>
<string>24B75</string>
<key>DTSDKName</key>
<string>macosx14.5</string>
<string>macosx15.1</string>
<key>DTXcode</key>
<string>1540</string>
<string>1610</string>
<key>DTXcodeBuild</key>
<string>15F31d</string>
<string>16B40</string>
<key>LSMinimumSystemVersion</key>
<string>10.13</string>
</dict>
Expand Down
Binary file not shown.
29 changes: 29 additions & 0 deletions dist/package-nofragment/Assets/Plugins/WebViewObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ private static extern void _CWebViewPlugin_Reload(
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);
private static extern bool _CWebViewPlugin_IsInitialized(
IntPtr instance);
[DllImport("__Internal")]
Expand Down Expand Up @@ -611,6 +614,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 bool IsInitialized()
{
#if UNITY_WEBPLAYER || UNITY_WEBGL
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 @@ -265,7 +265,7 @@ - (id)initWithGameObjectName:(const char *)gameObjectName_ transparent:(BOOL)tra

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

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

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

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

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

return self;
}
Expand Down
Binary file not shown.
Binary file not shown.
Loading