Screen Space Rectangle Of Given Object.

Hi want to Draw a Rectangle of (GUI.BOX) in screen By given Object (Ex : BOX). Simply What want is Take Screen Shot of Given Object. I already know how to take screen shot of given object. But what i need to Know is How to Draw Screen bounding Box of Given Object. I came up with sample solution and that is work fine with Orthographic camera but when i change the camera to Perspective its not given correct result for some reason.

public Rect ScrRect;

Update()
{

    Bounds b = GObj.renderer.bounds; // reference object ex Simple
		points[0] = new Vector3( b.min.x, b.min.y, b.min.z );
		points[1] = new Vector3( b.max.x, b.min.y, b.min.z );
		points[2] = new Vector3( b.max.x, b.max.y, b.min.z );
		points[3] = new Vector3( b.min.x, b.max.y, b.min.z );
		points[4] = new Vector3( b.min.x, b.min.y, b.max.z );
		points[5] = new Vector3( b.max.x, b.min.y, b.max.z );
		points[6] = new Vector3( b.max.x, b.max.y, b.max.z );
		points[7] = new Vector3( b.min.x, b.max.y, b.max.z );
		
		Bounds screenBounds = new Bounds();
		for( int i = 0; i < 8; i++ )
		{
			screenPos _= Camera.main.WorldToScreenPoint( points *);*_

* if( i == 0 )*
* screenBounds = new Bounds( screenPos[0], Vector3.zero); *

_ screenBounds.Encapsulate( screenPos );
* }*_

* Debug.Log(screenBounds.ToString());*

* ScrRect.xMin = screenBounds.min.x;*
* ScrRect.yMin = screenBounds.min.y;*
* ScrRect.xMax = screenBounds.max.x;*
* ScrRect.yMax = screenBounds.max.y;*
}

OnGUI()
{
GUI.Box(ScrRect,“Area”);
}
I think there is some thing wrong with Screen space Conversation. When I convert to Viewpot position seems ok.

I guess it’s drawn wrong on the y axis? For some reason unity usually has its zero position in the bottom left corner, but for OnGUI functions they decided to go with top left. Try

OnGUI()
{
   ScrRect.y = Screen.height - (ScrRect.y+ScrRect.height);
   GUI.Box(ScrRect,"Area");
}

@Alex Zeitler - thanks for Reply but that wasnt the Problem. I tried what you have suggest
:frowning:

@Yasantha

I’m not sure if you ended up solving this problem but I managed to do something similar and had the issue of the y co-ordinate being reversed for OnGUI. So I changed the code that you had:

     ScrRect.yMin = screenBounds.min.y;
     ScrRect.yMax = screenBounds.max.y;

to

     ScrRect.yMin = Screen.height - screenBounds.max.y;
     ScrRect.yMax = Screen.height - screenBounds.min.y;

which allowed for the GUI.Box code to render the screen bounds in the correct location.