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

An example to show how to align a webview to a uGUI RectTransform #816

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

KojiNakamaru
Copy link
Member

@xinbot
Copy link

xinbot commented Jul 9, 2022

Use Camera.main.WorldToScreenPoint() method to convert world position to screen position.

@xinbot
Copy link

xinbot commented Jul 9, 2022

screenshot-20220709-170110
I think converting world points to screen points will make the calculation correct.

@KojiNakamaru
Copy link
Member Author

Thank you for your comment. It actually depends on Canvas Render Mode. If we set it "Screen Space" as in this example, converting with Camera.main.WorldToScreenPoint() results wrong coordinates. If we set it "World Space", we should use Camera.main.WorldToScreenPoint() as you point.

@KojiNakamaru KojiNakamaru force-pushed the example/align_to_rect_transform branch from 84542f3 to 298e2ff Compare October 4, 2022 03:30
@xinbot
Copy link

xinbot commented Dec 12, 2022

How could I fit my webview in RectTransform, if my Canvas is in "Screen Space-Camera" render mode and my Canvas Scaler is in "Sclae with Screen Size" scale mode?

@KojiNakamaru
Copy link
Member Author

@xinbot
You need to convert retrieved world coordinates to screen coordinates as below:

    public static Bounds GetRectTransformBounds(RectTransform transform)
    {
        var corners = new Vector3[4];
        transform.GetWorldCorners(corners);
        for (var i = 0; i < 4; i++)
        {
            corners[i] = Camera.main.WorldToScreenPoint(corners[i]);
        }
        var bounds = new Bounds(corners[0], Vector3.zero);
        for (var i = 1; i < 4; i++)
        {
            bounds.Encapsulate(corners[i]);
        }
        return bounds;
    }

@Alexzoin
Copy link

Would this work to avoid drawing the webview under a device's notch or hole punch for cameras?

webViewObject.SetMargins(0, (int)(Screen.height - Screen.safeArea.height), 0, 0);

@KojiNakamaru
Copy link
Member Author

Your code should work for the "Portrait" mode, but I guess the following is more general:

        webViewObject.SetMargin(
            Screen.safeArea.xMin,
            Screen.height - Screen.safeArea.yMax,
            Screen.width - Screen.safeArea.xMax,
            Screen.safeArea.yMin);

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.

3 participants