12345678910111213141516171819202122 |
- using System;
- using UnityEngine;
- namespace TriLib
- {
- public static class CameraExtensions
- {
- public static void FitToBounds(this Camera camera, Transform transform, float distance)
- {
- Bounds bounds = transform.EncapsulateBounds();
- float magnitude = bounds.extents.magnitude;
- float num = magnitude / (2f * Mathf.Tan(0.5f * camera.fieldOfView * 0.0174532924f)) * distance;
- if (float.IsNaN(num))
- {
- return;
- }
- camera.farClipPlane = num * 2f;
- camera.transform.position = new Vector3(bounds.center.x, bounds.center.y, bounds.center.z + num);
- camera.transform.LookAt(bounds.center);
- }
- }
- }
|