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

Fixed a bug in relative margins on Android #1035

Merged
merged 1 commit into from Jan 22, 2024

Conversation

hanachiru
Copy link
Contributor

@hanachiru hanachiru commented Jan 22, 2024

summary

When doing SetMargins with relative true on android, the margins are incorrect.

On Android, Screen.width(height) always returns the same value as Screen.currentResolution.width(height).
So, whether relative is true or false, ml, mt, mr, and mb use the same value.

if (relative)
{
float w = (float)Screen.width;
float h = (float)Screen.height;
int iw = Screen.currentResolution.width;
int ih = Screen.currentResolution.height;
ml = left / w * iw;
mt = top / h * ih;
mr = right / w * iw;
mb = AdjustBottomMargin((int)(bottom / h * ih));
}
else
{
ml = left;
mt = top;
mr = right;
mb = AdjustBottomMargin(bottom);
}

Display.main.systemWidth(systemHeight) can be used to obtain the resolution of the physical screen.(not rendering resolution)
The argument of ViewGroup.MarginLayoutParams.setMargin, which is executed in CWebViewPlugin.java, uses px.(physical screen)
So I think this bug can be fixed by using Display.main.systemWidth(systemHeight).

How to reproduce

You can reproduce the bug by calling WebViewObject.SetMargins after using Screen.SetResolution on Android.
Display.main.systemWidth(systemHeight) instead of Screen.currentResolution.width(height) seems to correctly convert from the value on the rendering resolution to the value on the physical resolution(px).

cf

https://docs.unity3d.com/ScriptReference/Display-systemWidth.html
https://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams#attr_android:layout_margin
https://docs.unity3d.com/ScriptReference/Screen.SetResolution.html

@KojiNakamaru
Copy link
Member

Thank you for the fix with detailed analysis. As I noticed that Display.main.{systemWith,systemHeight} didn't reflect the navigation bar's margin, I'll later extend your code after merging this pull request.

@KojiNakamaru KojiNakamaru merged commit 5cd0ed1 into gree:master Jan 22, 2024
@hanachiru hanachiru deleted the feature/android_relative_margin branch January 22, 2024 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants