7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RectTransformの矩形からスクリーン矩形を得る

Last updated at Posted at 2017-09-05

環境

Unity2017.1.0f3

概要

RectTransformの矩形からスクリーン矩形に変換します
主にWebViewの表示領域に使用します

コガネブログさんが作成したのを使用すると良いと思います。
http://baba-s.hatenablog.com/entry/2019/06/21/090000

GreeのWebView

GreeのWebViewをWindowsのUnityEditorである程度動作させるとっかかり

プログラム

using UnityEngine;

public class TestRectTransformToScreenSpaceRect : MonoBehaviour
{
    [SerializeField] RectTransform _rectTrans = null;

    private Rect _screenRect;

    private void Update ()
    {
        _screenRect = CalcurateScreenSpaceRect( _rectTrans );
    }

    private static Rect CalcurateScreenSpaceRect( RectTransform rectTrans )
    {
        var canvas = rectTrans.GetComponentInParent<Canvas>();
        var camera = canvas.worldCamera;
        var corners = new Vector3[ 4 ];
        //左下、左上、右上、右下
        rectTrans.GetWorldCorners( corners );
        var screenCorner1 = RectTransformUtility.WorldToScreenPoint( camera, corners[ 1 ] );
        var screenCorner3 = RectTransformUtility.WorldToScreenPoint( camera, corners[ 3 ] );

        //左下基準
        var screenRect = new Rect();
        screenRect.x = screenCorner1.x;
        screenRect.width = screenCorner3.x - screenRect.x;
        screenRect.y = screenCorner3.y;
        screenRect.height = screenCorner1.y - screenRect.y;
#if false
        //左、上、右、下
        var margin = new Vector4( screenCorner1.x, Screen.height - screenCorner1.y, Screen.width - screenCorner3.x, screenCorner3.y );
        Debug.Log("margin x:" + margin.x + "y:" + margin.y + "z:" + margin.z + "w:" + margin.w );
        _WebView.SetMargins( (int)margin.x, (int)margin.y, (int)margin.z, (int)margin.w ); 
#endif
        return screenRect;
    }

    private static Texture2D _debugTexture = null;
    private void OnGUI()
    {
        if( _debugTexture == null )
        {
            _debugTexture = new Texture2D( 4, 4, TextureFormat.ARGB32, false );
            for (int y = 0; y < _debugTexture.height; y++) {
                for (int x = 0; x < _debugTexture.width; x++) {
                    Color color = new Color( 1.0f, 0.0f, 0.0f, 0.5f );
                    _debugTexture.SetPixel(x, y, color);
                }
            }
            _debugTexture.Apply();
        }
        //こっちは左上基準
        var rect = _screenRect;
        rect.yMax = Screen.height - _screenRect.yMin;
        rect.yMin = rect.yMax - _screenRect.height;
        GUI.DrawTexture( rect, _debugTexture );
    }
}
7
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?