Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
Anonymous's avatar
Anonymous
7 years ago

Camera Fade Color.

Using the resources found on the Unity Wiki, we have changed the color of the camera for a while or blinked.
However, it does not work in Ouclus environment. What is the cause and what is the solution?


CameraFade.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class CameraFade : MonoBehaviour
{
private static CameraFade mInstance = null;

private static CameraFade instance
{
get
{
if (mInstance == null)
{
mInstance = GameObject.FindObjectOfType(typeof(CameraFade)) as CameraFade;

if (mInstance == null)
{
mInstance = new GameObject("CameraFade").AddComponent<CameraFade>();
}
}

return mInstance;
}
}

void Awake()
{
if (mInstance == null)
{
mInstance = this as CameraFade;
instance.init();
}
}

public GUIStyle m_BackgroundStyle = new GUIStyle(); // Style for background tiling
public Texture2D m_FadeTexture; // 1x1 pixel texture used for fading
public Color m_CurrentScreenOverlayColor = new Color(0, 0, 0, 0); // default starting color: black and fully transparrent
public Color m_TargetScreenOverlayColor = new Color(0, 0, 0, 0); // default target color: black and fully transparrent
public Color m_DeltaColor = new Color(0, 0, 0, 0); // the delta-color is basically the "speed / second" at which the current color should change
public int m_FadeGUIDepth = -1000; // make sure this texture is drawn on top of everything

public float m_FadeDelay = 0;
public Action m_OnFadeFinish = null;

// Initialize the texture, background-style and initial color:
public void init()
{
instance.m_FadeTexture = new Texture2D(1, 1);
instance.m_BackgroundStyle.normal.background = instance.m_FadeTexture;
}

// Draw the texture and perform the fade:
void OnGUI()
{
// If delay is over...
if (Time.time > instance.m_FadeDelay)
{
// If the current color of the screen is not equal to the desired color: keep fading!
if (instance.m_CurrentScreenOverlayColor != instance.m_TargetScreenOverlayColor)
{
// If the difference between the current alpha and the desired alpha is smaller than delta-alpha * deltaTime, then we're pretty much done fading:
if (Mathf.Abs(instance.m_CurrentScreenOverlayColor.a - instance.m_TargetScreenOverlayColor.a) < Mathf.Abs(instance.m_DeltaColor.a) * Time.deltaTime)
{
instance.m_CurrentScreenOverlayColor = instance.m_TargetScreenOverlayColor;
SetScreenOverlayColor(instance.m_CurrentScreenOverlayColor);
instance.m_DeltaColor = new Color(0, 0, 0, 0);

if (instance.m_OnFadeFinish != null)
instance.m_OnFadeFinish();

Die();
}
else
{
// Fade!
SetScreenOverlayColor(instance.m_CurrentScreenOverlayColor + instance.m_DeltaColor * Time.deltaTime);
}
}
}
// Only draw the texture when the alpha value is greater than 0:
if (m_CurrentScreenOverlayColor.a > 0)
{
GUI.depth = instance.m_FadeGUIDepth;
GUI.Label(new Rect(-10, -10, Screen.width + 10, Screen.height + 10), instance.m_FadeTexture, instance.m_BackgroundStyle);
}
}

private static void SetScreenOverlayColor(Color newScreenOverlayColor)
{
instance.m_CurrentScreenOverlayColor = newScreenOverlayColor;
instance.m_FadeTexture.SetPixel(0, 0, instance.m_CurrentScreenOverlayColor);
instance.m_FadeTexture.Apply();
}

public static void StartAlphaFade(Color newScreenOverlayColor, bool isFadeIn, float fadeDuration)
{
if (fadeDuration <= 0.0f)
{
SetScreenOverlayColor(newScreenOverlayColor);
}
else
{
if (isFadeIn)
{
instance.m_TargetScreenOverlayColor = new Color(newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0);
SetScreenOverlayColor(newScreenOverlayColor);
}
else
{
instance.m_TargetScreenOverlayColor = newScreenOverlayColor;
SetScreenOverlayColor(new Color(newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0));
}

instance.m_DeltaColor = (instance.m_TargetScreenOverlayColor - instance.m_CurrentScreenOverlayColor) / fadeDuration;
}
}

public static void StartAlphaFade(Color newScreenOverlayColor, bool isFadeIn, float fadeDuration, float fadeDelay)
{
if (fadeDuration <= 0.0f)
{
SetScreenOverlayColor(newScreenOverlayColor);
}
else
{
instance.m_FadeDelay = Time.time + fadeDelay;

if (isFadeIn)
{
instance.m_TargetScreenOverlayColor = new Color(newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0);
SetScreenOverlayColor(newScreenOverlayColor);
}
else
{
instance.m_TargetScreenOverlayColor = newScreenOverlayColor;
SetScreenOverlayColor(new Color(newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0));
}

instance.m_DeltaColor = (instance.m_TargetScreenOverlayColor - instance.m_CurrentScreenOverlayColor) / fadeDuration;
}
}

public static void StartAlphaFade(Color newScreenOverlayColor, bool isFadeIn, float fadeDuration, float fadeDelay, Action OnFadeFinish)
{
if (fadeDuration <= 0.0f)
{
SetScreenOverlayColor(newScreenOverlayColor);
}
else
{
instance.m_OnFadeFinish = OnFadeFinish;
instance.m_FadeDelay = Time.time + fadeDelay;

if (isFadeIn)
{
instance.m_TargetScreenOverlayColor = new Color(newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0);
SetScreenOverlayColor(newScreenOverlayColor);
}
else
{
instance.m_TargetScreenOverlayColor = newScreenOverlayColor;
SetScreenOverlayColor(new Color(newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0));
}
instance.m_DeltaColor = (instance.m_TargetScreenOverlayColor - instance.m_CurrentScreenOverlayColor) / fadeDuration;
}
}
public static void BlinkingFade(Color newScreenOverlayColor, float fadeDuration,float delay)
{
instance.StartCoroutine(Blinking(newScreenOverlayColor, fadeDuration,delay));
}
static IEnumerator Blinking(Color newScreenOverlayColor, float fadeDuration,float delay)
{
float timer = 0.0f;
float Alltimer = 0.0f;
bool flug = false;
Color currentColor=instance.m_CurrentScreenOverlayColor;
while (Alltimer < fadeDuration)
{
timer += Time.deltaTime;
if (delay < timer)
{
Alltimer += timer;
timer = 0;
flug = !flug;
if (!flug)
{
SetScreenOverlayColor(newScreenOverlayColor);
}
else
{
SetScreenOverlayColor(currentColor);
}
}

yield return null;
}
SetScreenOverlayColor(currentColor);
}
void Die()
{
mInstance = null;
Destroy(gameObject);
}

void OnApplicationQuit()
{
mInstance = null;
}
}

CameraTest.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class CameraTest : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.A))
{
CameraFade.StartAlphaFade(Color.white,false,1f);
}
if (Input.GetKeyDown(KeyCode.S))
{
CameraFade.BlinkingFade(new Color(1,0,0,0.2f), .3f,0.04f);
}
if (Input.GetKeyDown(KeyCode.D))
{
CameraFade.StartAlphaFade(new Color(1, 0, 0, 0.5f),false, 3f);
}
}
}

2 Replies

Replies have been turned off for this discussion