Unity C #: How can I create a GameObject on the screen?

Unity is so complicated compared to delphi. In delphi you can just say:

Rectangle1.Position.Y := 0;
Rectangle1.Position.X := screen.Width - Rectangle1.Width;

This will simply change the position of the rectangle in the upper right corner of the screen. But there is so much about Unity to learn how to just do it, and I don’t know where to start. Can someone please simplify this for me? I just want to create an object in the upper right corner of the screen, both on smartphones and on PC.

I am using Unity2D

EDIT: this is the inspector of the object I want to create: enter image description here

And this is the code that I use to create objects on the screen:

Instantiate(objectName, new Vector3(0, 0, 0), Quaternion.identity);

EDIT will be more clear: enter image description here

+4
source share
2 answers

( 2 , - 3. ).

Camera.ScreenToWorldPoint.

Qouting docs:

Camera.ScreenToWorldPoint(position: Vector3) .

. (0,0); (pixelWidth, pixelHeight). z .

, , ( # mix):

Vector3 p = camera.ScreenToWorldPoint(new Vector3(0, Screen.height, HowFarFromCamera));
Rectangle1.position = new Vector3(p.x + Rectangle1.Width/2,p.y - Rectangle1.Height/2,p.z);

p - ( ). Width/2 Height/2, , .

. Bounds.size, ( , SpriteRenderer)

+4

, , , , 2D ( ), - Unity UI system ( 4.6: 4.6):

  • Canvas .
  • , .

, Canvas: , / ( ).

+1

Source: https://habr.com/ru/post/1665466/


All Articles