How to resize a camera's orthoSize for an object to fit inside its rect viewport?

Hello, this is another follow-up of this series of questions. So I’m still working on the Examine feature of Resident Evil.

Short summary of my setup once again:

  1. I’m using NGUI, I have a
    “Examine_UI” UI, which has its main
    camera (which you can see in the
    left image below). This camera can
    only see the UI layer, which is
    “2D_Examine”
  2. Going down you got “ExamineCamera”
    which only sees the “ExamineArea”
    (grey window in the middle of the
    UI, left image) and its children.

14616-re5.png

So after I got the weapon to appear only in the desired area, and re-calculating the examine camera viewport every time the examine window changes position, I’m stuck with a problem:

Weapons/objects could have different scales upon examining them, which means, they could either appear big in the examine window (large scale) or small.
What I would like, is to have the weapon fit nicely inside the examine window no matter how big or small it was.

I can think of 2 ways of achieving this:

  1. If the gun was bigger/smaller, I
    would keep increasing/decreasing the
    size of my orthographic camera, till
    the gun fits. The problem is I don’t
    know what ‘till the gun fits’ means!
    I mean what would that imply? To
    answer, I’d have to find a
    connection between: The gun’s size
    and/or scale, the camera’s orthoSize
    and its rect viewport. What is the
    connection between those?
  2. Instead of changing the camera’s
    size, I could maybe scale down the
    gun till it fits. Here, I think I
    might be able to figure out what
    ‘till it fits’ means. I could maybe
    take the bounds of the gun’s
    boxCollider, and compare it with the
    camera’s rect viewport, keep scaling
    down, till they’re about the same
    width and height. However that
    requires me to save the gun’s
    previous scale, so that after I’m
    done examining I take it back to
    that scale.

I would really like to apply the first method, it sounds more efficient and faster, and don’t mess with the scale at all.

So, how can I achieve it? change the cam’s orthoSize till the gun fits the window? Is this possible or am I gonna have to go with the 2nd method, which I’m also having trouble with :confused: - I just want ideas, thoughts, not code.

Please notice that I only want to do this thing upon the beginning of examining an item.

If you’re interested in my current code of examining:

public void ExamineItem(Item item)
{
	// if it's the same item, don't do anything
	if (item.gameObject == examineInfo.itemObj)
		return;

	// if there's already an item we're examining, let it go
	if (!doneExamining)
		Notify_DoneExaminingItem();

	doneExamining = false;

	// activiting the item object if its disabled
	examineInfo.wasActive = item.gameObject.active;
	if(!examineInfo.wasActive)
		item.gameObject.SetActive(true);

	// store item's layer and parent, to get back to it when we're done examining
	examineInfo.previousLayer = item.gameObject.layer;
	examineInfo.previousParent = item.transform.parent;
	
	// change item layer to the examine pivot point layer
	MiscOps.ChangeObjectLayer(item.gameObject, examinePivot.gameObject.layer);

	// adding it as a child to our examine pivot
	item.transform.parent = examinePivot.transform;
	examineInfo.itemObj = item.gameObject;

	// zeroing out its local pos and resettins the scale
	examineInfo.itemObj.transform.localPosition = Vector3.zero;

	// nuke gravity, if any
	examineInfo.rigid = examineInfo.itemObj.rigidbody;
	if (examineInfo.rigid != null)
		examineInfo.rigid.useGravity = false;

	// set strings
	examineStrings = item.examineStrings;
	currentStringIndex = 0;
	UpdateText();

	// finally show the examine window
	Show = true;
	examineArea.examineCamera.RefreshCamera();
}

public void Notify_DoneExaminingItem()
{
	Show = false;

	if (examineInfo.itemObj == null)
		return;

	// if it was active, activiate it
	if (examineInfo.wasActive)
		examineInfo.itemObj.SetActive(true);

	// get its previous layer back
	MiscOps.ChangeObjectLayer(examineInfo.itemObj, examineInfo.previousLayer);
	
	// if it has a rigid body, get its gravity back
	if (examineInfo.rigid != null)
		examineInfo.rigid.useGravity = true;

	// free it, get its parent back
	examineInfo.itemObj.transform.parent = examineInfo.previousParent;

	// done
	examineInfo.itemObj = null;
	doneExamining = true;
}

public bool Show
{
	get { return show; }
	set 
	{ 
		show = value;
		examineGuiRoot.SetActive(show);
	}
}

public class ItemToExamineInfo
{
	public GameObject itemObj;
	public Rigidbody rigid;
	public bool wasActive;
	public int previousLayer;
	public Transform previousParent;
}

Thanks all. I hope this is the last problem in this series.

I think you have to determine camera view height with:

currentCamera.orthographicSize = rendererBounds.extents.y + margin;

if you look at it from front.
And then determine the viewport proportions according to the object proportions. At least that has worked for me.