If you want to do this with UI objects, then it's pretty straight forward using anchors.
Since your case seems to be with 3d objects, it all depends where you want to place the object. Here's some math:
// This is the value on the Z axis, where the object can be placed
// You can populate it with something like `posZ = myObj.position.z;`
float posZ;
// This is your scene's camera, based on its field of view and aspect, it will calculate positions
Camera cam;
// This is the width and height of the "plane" that is visible on the screen on position `posZ`
float height = (Mathf.Tan(cam.fov *Mathf.Deg2Rad*0.5f) *posZ *2f) /10f;
float width = height *cam.aspect;
Credits for this code go to this answer on stackoverflow
Based on this, you can use the width
and height
to calculate the screen's edges and place objects there. For example placing a cube on position (width/2, 0, 10)
(assuming posZ = 10;
) then the center of the cube would be placed on the right part of the screen, making it partially visible.
If you want to place the cube in such a way that it is touching the right part of the screen, and is almost fully visible, then you can assign its position to something like (width -cubeWidth/2, 0, 10)
.
You can get a Mesh
's size with myMesh.GetComponent<MeshRenderer>().mesh.bounds.size
which is a Vector3
with the object's dimensions. Note that this shows the bounding box of the mesh, keep that in mind when handling complex meshes.