Hello, this is a follow-up of this question.
So after I got the cutting right, I’m facing a problem where I can’t re-calculate the Camera’s new viewport each time I drag my panel (I’m using NGUI). I can calculate it well the first time, but every time I drag the panel around, I see its screen coords go way out of screen, and gives me “NAN”. I read this and found it to be similar to my problem, but the thing is, in that solution they’re using a unity GUI function which I can’t use in NGUI. How can I resolve that? what’s the equivalent of GUI.ScreenToGUIPoint in NGUI?
Here’s my UI setup just in case you think I should approach this differently.
Basically, for the item examining I have two cameras, one for the whole UI, and another just to render the weapon/3dobject inside the “ExamineArea” (which is the grey shaded background you see in the middle of my UI)
Note that, the UIDragObject script is attached to the title bar, and the target of it is the whole panel, so when the panel moves, everything inside moves.
Here’s my code of calculating the viewport:
public void CalcViewport()
{
// we need to convert the bottom left point of the examine area to its normalized viewport equivalent
// first take the top left
var vPort = _camera.WorldToViewportPoint(examineArea.transform.position);
// then subtract the height
vPort.y -= examineArea.transform.localScale.y / Screen.height;
// take the dims along the way
var dims = new Dimension2D(examineArea.transform.localScale.x / Screen.width, examineArea.transform.localScale.y / Screen.height);
// set our new screen viewport rect
_camera.rect = new Rect(vPort.x, vPort.y, dims.x, dims.y);
}
I also tried converting first from world to screen point, subtracting the height and then to viewport, but also had problems with that… I also tried pixelRect.
To recap one last time: I’m trying to re-calculate my viewing camera’s viewport everytime I drag my UI panel, I can’t seem to get it right, any help would be appreciated. Thanks.