Skip to content

Instantly share code, notes, and snippets.

@allfake
Created August 4, 2015 11:10
Show Gist options
  • Save allfake/286a6feb81e3459283e2 to your computer and use it in GitHub Desktop.
Save allfake/286a6feb81e3459283e2 to your computer and use it in GitHub Desktop.
From http://answers.unity3d.com/questions/119918/transitions-between-changing-scenes.html change to only fade, add hook function, you need to add image in gameObject.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Reflection;
using System;
public class FadeInOut : MonoBehaviour {
[Serializable]
public class EventTrigger : UnityEngine.Events.UnityEvent {}
public Entry onStart = new Entry();
public Entry onZeroAlpha = new Entry();
public Entry onFinish = new Entry();
public float fadeIn = 1.0f;
public float fadeOut = 1.0f;
public float fadeStay = 0.5f;
public Color fadeColor = Color.black;
[System.Serializable]
public class ToggleMobileController {
public bool hideOnStart = true;
public bool showOnFinish = true;
}
public ToggleMobileController mobileController;
[Serializable]
public class Entry {
public EventTrigger callback = new EventTrigger();
}
private Image image;
void Awake() {
image = GetComponent<Image>();
}
private void ChanageAlpha(Color aColor,float aAlpha) {
aColor.a = aAlpha;
image.color = aColor;
}
private IEnumerator Fade(float aFadeOutTime, float aFadeInTime, float aFadeStayTime, Color aColor) {
if (onStart.callback.GetPersistentEventCount () > 0) {
onStart.callback.Invoke();
}
float t = 0.0f;
while (t < 1.0f)
{
yield return new WaitForEndOfFrame();
t = Mathf.Clamp01(t + Time.deltaTime / aFadeOutTime);
ChanageAlpha(aColor,t);
}
yield return new WaitForSeconds(aFadeStayTime / 2.0f);
if (onZeroAlpha.callback.GetPersistentEventCount() > 0) {
onZeroAlpha.callback.Invoke();
}
yield return new WaitForSeconds(aFadeStayTime / 2.0f);
while (t > 0.0f)
{
yield return new WaitForEndOfFrame();
t = Mathf.Clamp01(t - Time.deltaTime / aFadeInTime);
ChanageAlpha(aColor,t);
}
if (onFinish.callback.GetPersistentEventCount() > 0) {
onFinish.callback.Invoke();
}
}
private void StartFade(float aFadeOutTime, float aFadeInTime, float aFadeStayTime, Color aColor) {
StartCoroutine(Fade(aFadeOutTime, aFadeInTime, aFadeStayTime, aColor));
}
public void FadeNow() {
StartFade(fadeOut, fadeIn, fadeStay, fadeColor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment