Last active
February 13, 2020 14:42
-
-
Save Josef212/aacd28f13f62be7362b8a81e71e839bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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