Skip to content

Instantly share code, notes, and snippets.

@joonjoonjoon
Created May 13, 2016 10:12
Show Gist options
  • Select an option

  • Save joonjoonjoon/2bb0f4d215e071af2201a633a678838d to your computer and use it in GitHub Desktop.

Select an option

Save joonjoonjoon/2bb0f4d215e071af2201a633a678838d to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
public class FadeAssistant {
public static float duration = 0.3f;
public static Dictionary<int, float> alphaDictionary;
public static void Init()
{
if (alphaDictionary == null) alphaDictionary = new Dictionary<int, float>();
}
public static Sequence FadeInCascade(GameObject g, float modifier = 1f)
{
return FadeInCascade(g.transform, modifier);
}
public static Sequence FadeInCascade(Transform t, float modifier=1f)
{
var s = new Sequence();
s.Append(FadeCascade(t, 1, false, modifier));
return s;
}
public static Sequence FadeOutCascade(GameObject g, float modifier = 1f)
{
return FadeOutCascade(g.transform, modifier);
}
public static Sequence FadeOutCascade(Transform t, float modifier = 1f)
{
var s = new Sequence();
s.Append(FadeCascade(t, 0, true, modifier));
return s;
}
private static Sequence FadeCascade(Transform t, float alpha, bool disableButtons, float modifier = 1f)
{
var s = new Sequence();
s.Insert(0,Fade(t, alpha, disableButtons, modifier));
// Don't cascade if it's a canvas group plz!
if (t.GetComponent<CanvasGroup>() != null) return s;
foreach (Transform child in t)
{
s.Insert(0, FadeCascade(child, alpha, disableButtons, modifier));
}
return s;
}
public static Sequence FadeIn(Object obj, float modifier=1f)
{
var s = new Sequence();
s.Append(Fade(obj, 1, false, modifier));
return s;
}
public static Sequence FadeOut(Object obj, float modifier = 1f)
{
var s = new Sequence();
s.Append(Fade(obj, 0, true, modifier));
return s;
}
private static Sequence Fade(Object obj, float alpha, bool disableButtons, float modifier = 1f)
{
Init();
float alphaMax = 1;
bool found = alphaDictionary.TryGetValue(obj.GetHashCode(), out alphaMax);
var s = new Sequence();
if (obj is CanvasGroup)
{
var group = obj as CanvasGroup;
if (!found) alphaDictionary.Add(obj.GetHashCode(), group.alpha);
else alpha = Mathf.Clamp(alpha, 0, alphaMax);
s.Insert(0,
HOTween.To(group, duration * modifier, "alpha", alpha)
);
if (disableButtons)
{
group.interactable = false;
group.blocksRaycasts = false;
}
else
{
group.interactable = true;
group.blocksRaycasts = true;
}
}
else if(obj is TMPro.TextMeshProUGUI)
{
var text = obj as TMPro.TextMeshProUGUI;
if (!found) alphaDictionary.Add(obj.GetHashCode(), text.color.a);
else alpha = Mathf.Clamp(alpha, 0, alphaMax);
s.Insert(0,
HOTween.To(text, duration * modifier, "color", text.color.SetA(alpha))
);
}
else if (obj is Text)
{
var text = obj as Text;
if (!found) alphaDictionary.Add(obj.GetHashCode(), text.color.a);
else alpha = Mathf.Clamp(alpha, 0, alphaMax);
s.Insert(0,
HOTween.To(text, duration * modifier, "color", text.color.SetA(alpha))
);
}
else if (obj is Image)
{
var img = obj as Image;
if (!found) alphaDictionary.Add(obj.GetHashCode(), img.color.a);
else alpha = Mathf.Clamp(alpha, 0, alphaMax);
s.Insert(0,
HOTween.To(img, duration * modifier, "color", img.color.SetA(alpha))
);
}
else if (obj is Button)
{
var button = obj as Button;
if(disableButtons)
{
button.interactable = false;
var img = button.GetComponent<Image>();
if (img != null) img.raycastTarget = false;
}
else
{
button.interactable = true;
var img = button.GetComponent<Image>();
if (img != null) img.raycastTarget = true;
}
}
else if (obj is Toggle)
{
var button = obj as Toggle;
if (disableButtons)
{
button.interactable = false;
var img = button.GetComponent<Image>();
if (img != null) img.raycastTarget = false;
}
else
{
button.interactable = true;
var img = button.GetComponent<Image>();
if (img != null) img.raycastTarget = true;
}
}
else if (obj is Transform)
{
var t = obj as Transform;
// fade if canvas group
var group = t.GetComponent<CanvasGroup>();
if (group != null)
{
s.Insert(0,
Fade(group, alpha, disableButtons, modifier)
);
return s; // we don't want to cascade when we meet a canvas group, since that's what it's fore
}
// buttons and toggles
var button = t.GetComponent<Button>();
if (button != null)
{
s.Insert(0,
Fade(button, alpha, disableButtons, modifier)
);
}
var toggle = t.GetComponent<Toggle>();
if (toggle != null)
{
s.Insert(0,
Fade(toggle, alpha, disableButtons, modifier)
);
}
// fade if anythng else
var tmtext = t.GetComponent<TMPro.TextMeshProUGUI>();
if (tmtext != null)
{
s.Insert(0,
Fade(tmtext, alpha, disableButtons, modifier)
);
}
var text = t.GetComponent<Text>();
if (text != null)
{
s.Insert(0,
Fade(text, alpha, disableButtons, modifier)
);
}
var img = t.GetComponent<Image>();
if (img != null)
{
s.Insert(0,
Fade(img, alpha, disableButtons, modifier)
);
}
}
else if (obj is GameObject)
{
var g = obj as GameObject;
s.Append(Fade(g.transform, alpha, disableButtons, modifier));
}
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment