Skip to content

Instantly share code, notes, and snippets.

@jxnblk
Last active October 19, 2023 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jxnblk/296850435694b95569ff09347756ce9e to your computer and use it in GitHub Desktop.
Save jxnblk/296850435694b95569ff09347756ce9e to your computer and use it in GitHub Desktop.
Unity interior scene camera fade script
using UnityEngine;
using UnityEngine.UI;
public class InteriorCamera : MonoBehaviour {
public Camera cam; // Interior layer w/ culling
public RenderTexture texture;
public RawImage output; // UI layer + UI Camera Stack
public float duration = 0.5f;
public LeanTweenType easeIn = LeanTweenType.easeInCubic;
public LeanTweenType easeOut = LeanTweenType.easeOutCubic;
[Header("State")]
public bool active = false;
float aspect = 1f;
int width = 3840;
int height = 2160;
Color color = Color.white;
void Start () {
Init();
}
void Init () {
aspect = Camera.main.aspect;
width = Camera.main.pixelWidth;
height = Camera.main.pixelHeight;
texture = new RenderTexture(width, height, 32);
cam.targetTexture = texture;
output.texture = texture;
}
void LateUpdate () {
if (!active) return;
if (Camera.main.aspect != aspect || Camera.main.pixelWidth != width) {
Init();
}
}
[ContextMenu("Fade In")]
public void FadeIn () {
if (active) return;
cam.enabled = true;
output.enabled = true;
LeanTween.value(gameObject, UpdateFade, 0f, 1f, duration)
.setEase(easeIn);
active = true;
}
[ContextMenu("Fade Out")]
public void FadeOut () {
if (!active) return;
LeanTween.value(gameObject, UpdateFade, 1f, 0f, duration)
.setEase(easeOut)
.setOnComplete(() => {
cam.enabled = false;
output.enabled = false;
});
active = false;
}
void UpdateFade (float n) {
color.a = n;
output.color = color;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment