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

I can't make it fit the RectTransform. #567

Open
AzadNezamdoust opened this issue Jun 24, 2020 · 5 comments
Open

I can't make it fit the RectTransform. #567

AzadNezamdoust opened this issue Jun 24, 2020 · 5 comments

Comments

@AzadNezamdoust
Copy link

Set margins is not relative to the canvas so based on screen size it does not fit in my ui.

@KojiNakamaru
Copy link
Member

In the sample app, if you place an Image as below,
image
You can compute the bounds and get corresponding margins as below:

diff --git a/sample/Assets/Scripts/SampleWebView.cs b/sample/Assets/Scripts/SampleWebView.cs
index 5cc08c5..beea224 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>();
@@ -109,7 +122,13 @@ public class SampleWebView : MonoBehaviour
 #endif
         //webViewObject.SetAlertDialogEnabled(false);
         //webViewObject.SetURLPattern("", "^https://.*youtube.com", "^https://.*google.com");
-        webViewObject.SetMargins(5, 100, 5, Screen.height / 4);
+        var bounds = GetRectTransformBounds(GameObject.Find("Image").GetComponent<RectTransform>());
+        Debug.Log(bounds);
+        webViewObject.SetMargins(
+            (int)bounds.min.x,
+            (int)bounds.min.y,
+            (int)(Screen.width - bounds.max.x),
+            (int)(Screen.height - bounds.max.y));
         webViewObject.SetVisibility(true);
 
 #if !UNITY_WEBPLAYER && !UNITY_WEBGL

image

@AzadNezamdoust
Copy link
Author

Here is the final version of the margin finder. The one you described a bow does not calculate it right.

` Rect newSafeArea = Screen.safeArea;

        Vector2 _anchorMin = newSafeArea.position;
        Vector2 _anchorMax = newSafeArea.position + newSafeArea.size;

        if (_safeAreaX)
        {
            _anchorMin.x /= Screen.width;
            _anchorMax.x /= Screen.width;
        }
        if (_safeAreaY)
        {
            _anchorMin.y /= Screen.height;
            _anchorMax.y /= Screen.height;
        }

        _webViewObject.SetMargins(new Vector4(
            _anchorMin.x * Screen.width,
            (1 - _anchorMax.y) * Screen.height,
            (1 - _anchorMax.x) * Screen.width,
            _anchorMin.y * Screen.height));`

@KojiNakamaru
Copy link
Member

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 Render outside safe area.

@xinbot
Copy link

xinbot commented Jul 9, 2022

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.

@KojiNakamaru
Copy link
Member

cf. #816 (comment)

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

No branches or pull requests

3 participants