Last active
August 19, 2023 13:19
-
-
Save ditzel/d3dde9d60df08e714f9c571db81f3937 to your computer and use it in GitHub Desktop.
ResponsiveCamera
This file contains 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; | |
/// <summary> | |
/// Responsive Camera Scaler | |
/// </summary> | |
public class CameraAspectRatioScaler : MonoBehaviour { | |
/// <summary> | |
/// Reference Resolution like 1920x1080 | |
/// </summary> | |
public Vector2 ReferenceResolution; | |
/// <summary> | |
/// Zoom factor to fit different aspect ratios | |
/// </summary> | |
public Vector3 ZoomFactor = Vector3.one; | |
/// <summary> | |
/// Design time position | |
/// </summary> | |
[HideInInspector] | |
public Vector3 OriginPosition; | |
/// <summary> | |
/// Start | |
/// </summary> | |
void Start () { | |
OriginPosition = transform.position; | |
} | |
/// <summary> | |
/// Update per Frame | |
/// </summary> | |
void Update () { | |
if (ReferenceResolution.y == 0 || ReferenceResolution.x == 0) | |
return; | |
var refRatio = ReferenceResolution.x / ReferenceResolution.y; | |
var ratio = (float)Screen.width / (float)Screen.height; | |
transform.position = OriginPosition + transform.forward * (1f - refRatio / ratio) * ZoomFactor.z | |
+ transform.right * (1f - refRatio / ratio) * ZoomFactor.x | |
+ transform.up * (1f - refRatio / ratio) * ZoomFactor.y; | |
} | |
} |
This file contains 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class ResponsiveCamera : MonoBehaviour { | |
public Camera PortraitCamera; | |
public Camera LandscapeCamera; | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
PortraitCamera.enabled = Screen.width <= Screen.height; | |
PortraitCamera.GetComponent<AudioListener>().enabled = PortraitCamera.enabled; | |
LandscapeCamera.enabled = Screen.width > Screen.height; | |
LandscapeCamera.GetComponent<AudioListener>().enabled = LandscapeCamera.enabled; | |
} | |
} |
What to do with the ResponsiveCamera.cs script?
watch https://www.youtube.com/watch?v=So-UJYoOPr8.
This is where the script came from
Thank bro
video not examine the second script
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What to do with the ResponsiveCamera.cs script?