-
Notifications
You must be signed in to change notification settings - Fork 696
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
I can't make it fit the RectTransform. #567
Comments
Here is the final version of the margin finder. The one you described a bow does not calculate it right. ` Rect newSafeArea = Screen.safeArea;
|
Thanks, in my version, at least top/bottom margins should be flipped. Here is correct one: diff --git a/sample/Assets/Scripts/SampleWebView.cs b/sample/Assets/Scripts/SampleWebView.cs
index 1380fe8..63b9fcb 100644
--- a/sample/Assets/Scripts/SampleWebView.cs
+++ b/sample/Assets/Scripts/SampleWebView.cs
@@ -31,6 +31,19 @@ public class SampleWebView : MonoBehaviour
public Text status;
WebViewObject webViewObject;
+ // cf. https://answers.unity.com/questions/1013011/convert-recttransform-rect-to-screen-space.html?childToView=1628573#answer-1628573
+ public static Bounds GetRectTransformBounds(RectTransform transform)
+ {
+ var corners = new Vector3[4];
+ transform.GetWorldCorners(corners);
+ var bounds = new Bounds(corners[0], Vector3.zero);
+ for (var i = 1; i < 4; i++)
+ {
+ bounds.Encapsulate(corners[i]);
+ }
+ return bounds;
+ }
+
IEnumerator Start()
{
webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>();
@@ -122,7 +135,16 @@ public class SampleWebView : MonoBehaviour
// Add BASIC authentication feature (Android and iOS with WKWebView only) by takeh1k0 · Pull Request #570 · gree/unity-webview
//webViewObject.SetBasicAuthInfo("id", "password");
- webViewObject.SetMargins(5, 100, 5, Screen.height / 4);
+ // webViewObject.SetMargins(5, 100, 5, Screen.height / 4);
+ var bounds = GetRectTransformBounds(GameObject.Find("Image").GetComponent<RectTransform>());
+ Debug.Log(Screen.width);
+ Debug.Log(Screen.height);
+ Debug.Log(bounds);
+ webViewObject.SetMargins(
+ (int)bounds.min.x,
+ (int)(Screen.height - bounds.max.y),
+ (int)(Screen.width - bounds.max.x),
+ (int)bounds.min.y);
webViewObject.SetVisibility(true);
#if !UNITY_WEBPLAYER && !UNITY_WEBGL As you did, another tweak might be necessary depending on the setting of |
I have questions about fitting theRecttransform. Here the SetMargins() method seem to be accepting values in pixel unit. I check the unity documentation and confirm that Screen.height and Screen.width are in pixel unit. However, the values inside Bounds value is in point unit (I think the inside unity there are some metric definding point unit), so the substraction from pixel unit and point unit doesn't seem to be right. Shouldn't we change both value into pixel unit before doing math and assign the values to SetMargins() method. |
cf. #816 (comment) |
Set margins is not relative to the canvas so based on screen size it does not fit in my ui.
The text was updated successfully, but these errors were encountered: