2
2

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.

Unity2D 画面の大きさや四隅の座標をワールド座標で取得する方法

Posted at

##0.0 はじめに
画面端の四隅のポイント(画面左下や右上の位置)や画面のサイズをワールド座標で取得するための方法を記載しています。

##1.0 画面四隅の座標の取得方法

####1.1 CameraのViewportToWorldPointの利用
Camera.main.ViewportToWorldPoint()を使うと画面端をワールド座標で取得できます。引数が(0,0)で左下、(1,1)で右上を指定できます。

👍ポイント
Viewport(ビューポート)はカメラで表示されている画面の領域をx座標、y座標を0から1で表しています。左下が(0,0)、右上が(1,1)となります。

Test.cs
Debug.Log("画面の左下の座標は " + Camera.main.ViewportToWorldPoint(Vector2.zero) + " です");
Debug.Log("画面の右上の座標は " + Camera.main.ViewportToWorldPoint(Vector2.one) + " です");

####1.2 Screen.WidthとScreen.Heightの利用
Screen.WidthとScreen.Heightを使用して画面四隅のスクリーン座標を取得できます。
その座標をScreenToWorldPointメソッドを使ってワールド座標に変換します。
Camera.main.ScreenToWorldPoint()は引数にスクリーン座標をとり、これをワールド座標に変換するメソッドです。

👍ポイント
画面四隅のスクリーン座標はそれぞれ、
左下 (0,0)
右下 (Screen.Width, 0)
左上 (0, Screen.Height)
右上 (Screen.Width, Screen.Height)
となります。

Test.cs
Debug.Log("画面の左下の座標は " + Camera.main.ScreenToWorldPoint(new Vector2(0,0)));
Debug.Log("画面の右上の座標は " + Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height)));

👍ポイント
Camera.main.ScreenToWorldPoint()を使って取得したワールド座標は

##2.0 画面サイズの取得方法

####2.1 Orthographicの利用
カメラのProjectionがOrthographicのときはCamera.main.orthographicSizeで画面の縦方向のサイズの半分の長さをワールド座標で取得できます。縦サイズが取得できればアスペクト比から横サイズは取得できます。

👍ポイント
カメラのProjectionがOrthographicのときはカメラからの奥行きに関わらずワールド座標は一定となります。

Test.cs
Debug.Log("画面のアスペクト比(横幅 / 縦幅)は " + 2160 / 1080 + " です");
Debug.Log("画面の縦のサイズは " + Camera.main.orthographicSize * 2 + " です");
Debug.Log("画面の横のサイズは " + Camera.main.orthographicSize * 2 * 2160 / 1080 + " です");
2
2
0

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?