Camera distance to fit a given GameObject
this is my first post here. I've been searching for a solution to a problem which has been debated many times here and in the forums, but seemingly without a direct answer to my version of it.
So, what I want to do in Unity with C# is to calculate the minimum distance that my perspective camera has to be from a given GameObject (a procedurally generated mesh), so this object is fully framed by the camera and leaving the least space possible around it in the screen. In other words, fully framed by the camera means that there is no part of the object outside the view area.
To make things easier, my camera does not need to rotate or move - it's fully static, with the exception of one axis to zoom in or out in order to frame the target object. So, in fact, we could summarize my problem as a zoom problem (not using FOV to zoom) where I need to frame a procedurally generated mesh whose size varies considerably.
Would anyone be kind enough to point me in the right direction with code snippets, suggestions, etc?
Thanks in advance for your time.