Skip to content

Instantly share code, notes, and snippets.

@Josef212
Last active February 13, 2020 14:42
Show Gist options
  • Save Josef212/aacd28f13f62be7362b8a81e71e839bb to your computer and use it in GitHub Desktop.
Save Josef212/aacd28f13f62be7362b8a81e71e839bb to your computer and use it in GitHub Desktop.
using UnityEngine;
public static class CameraExtensions
{
public static float GetOrthographicSizeFromWorldWidth(this Camera camera, float width)
{
return width / camera.aspect * 0.5f;
}
public static float GetOrthographicSizeFromWorldHeight(this Camera camera, float height)
{
return height * 0.5f;
}
public static float GetOrthographicSizeFromWorldSize(this Camera camera, Vector2 size)
{
return GetOrthographicSizeFromWorldSize(camera, size.x, size.y);
}
public static float GetOrthographicSizeFromWorldSize(this Camera camera, float width, float height)
{
return Mathf.Max(GetOrthographicSizeFromWorldWidth(camera, width), GetOrthographicSizeFromWorldHeight(camera, height));
}
public static void SetOrthographicSizeToFitSize(this Camera camera, Vector2 size)
{
camera.orthographicSize = GetOrthographicSizeFromWorldSize(camera, size.x, size.y);
}
public static void SetOrthographicSizeToFitSize(this Camera camera, float width, float height)
{
camera.orthographicSize = GetOrthographicSizeFromWorldSize(camera, width, height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment