unityでワールドの位置に対して四角形を描画することはできたのですが、
スクリーン座標に対してどのように座標変換すればよいのかがわかりません。
ワールド座標で四角を描画
c#
1 2 line = this.gameObject.AddComponent<LineRenderer>(); 3 line.SetWidth(0.1f,0.1f); 4 line.SetVertexCount(5); 5 6 var startVec = new Vector3 (0.0f, 0.0f, 0.0f); 7 line.SetPosition (0, startVec); 8 line.SetPosition (1, new Vector3(startVec.x + 2.0f,startVec.y,startVec.z)); 9 line.SetPosition (2, new Vector3(startVec.x + 2.0f,startVec.y + 2.0f ,startVec.z)); 10 line.SetPosition (3, new Vector3(startVec.x,startVec.y + 2.0f ,startVec.z)); 11 line.SetPosition (4, new Vector3(startVec.x,startVec.y,startVec.z)); 12 13
例えば画面の左上を0,0として 右に100px、下に100pxの位置から500pxの四角を描画するにはどのようにすればよいのでしょうか?
一応下記のようにScreenToWorldPointを使用してみたのですが、画面上には何も描画されません
c#
1 2 var x = 100; 3 var y = 100; 4 var width = 500; 5 var height = 500; 6 7 Vector2 vec2pos = new Vector2(x,y); 8 Vector3 vecpos = Camera.main.ScreenToWorldPoint( vec2pos ); 9 line.SetPosition (0, vecpos); 10 11 vec2pos = new Vector2(x + width,y); 12 vecpos = Camera.main.ScreenToWorldPoint( vec2pos ); 13 line.SetPosition (1, vecpos); 14 15 vec2pos = new Vector2(x + width,y + height); 16 vecpos = Camera.main.ScreenToWorldPoint( vec2pos ); 17 line.SetPosition (2, vecpos); 18 19 vec2pos = new Vector2(x ,y + height ); 20 vecpos = Camera.main.ScreenToWorldPoint( vec2pos ); 21 line.SetPosition (3, vecpos); 22 23 vec2pos = new Vector2(x ,y ); 24 vecpos = Camera.main.ScreenToWorldPoint( vec2pos ); 25 line.SetPosition (4, vecpos); 26
回答2件
あなたの回答
tips
プレビュー