46
40

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 5 years have passed since last update.

画面の端点の座標を取得する【Unity, 2D】

Last updated at Posted at 2015-07-16

Unity2Dで画面端のワールド座標を取得したいことがあったので、メモ。
Z座標はカメラの座標になってしまうので、注意が必要です。

private Camera _mainCamera;

void Start () {
	// カメラオブジェクトを取得します
	GameObject obj = GameObject.Find ("Main Camera");
	_mainCamera = obj.GetComponent<Camera> ();
	
	// 座標値を出力
	Debug.Log (getScreenTopLeft ().x + ", " + getScreenTopLeft ().y);
	Debug.Log (getScreenBottomRight ().x + ", " + getScreenBottomRight ().y);
}

private Vector3 getScreenTopLeft() {
	// 画面の左上を取得
	Vector3 topLeft = _mainCamera.ScreenToWorldPoint (Vector3.zero);
	// 上下反転させる
	topLeft.Scale(new Vector3(1f, -1f, 1f));
	return topLeft;
}

private Vector3 getScreenBottomRight() {
	// 画面の右下を取得
	Vector3 bottomRight = _mainCamera.ScreenToWorldPoint (new Vector3(Screen.width, Screen.height, 0.0f));
	// 上下反転させる
	bottomRight.Scale(new Vector3(1f, -1f, 1f));
	return bottomRight;
}
46
40
1

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
46
40

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?