Skip to content

Instantly share code, notes, and snippets.

@MrMatthias
Created January 17, 2016 18:25
Show Gist options
  • Save MrMatthias/d8b5834fc1a0b0aec88b to your computer and use it in GitHub Desktop.
Save MrMatthias/d8b5834fc1a0b0aec88b to your computer and use it in GitHub Desktop.
unity ui shadow box gallery
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ShadowBG : MonoBehaviour
{
protected Image image;
public float fadeTime = 2.0f;
protected float currentTime = 0.0f;
protected float alphaTarget = 0.0f;
public AnimationCurve showCurve = AnimationCurve.Linear (0, 0, 1, 1);
public AnimationCurve hideCurve = AnimationCurve.Linear (0, 0, 1, 1);
protected Image shownImage = null;
protected RectTransform origin;
protected bool visible = false;
protected Vector3 originalPosition;
protected Vector2 originalSizeDelta;
protected Vector3 originalScale;
protected Vector2 originalAnchorMin;
protected Vector2 originalAnchorMax;
protected bool transitioning = false;
public bool Visible {
get {
return visible;
}
}
// Use this for initialization
public void Awake ()
{
image = GetComponent<Image> ();
}
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (transitioning) {
currentTime += Time.deltaTime * 1.0f / fadeTime;
currentTime = Mathf.Clamp01 (currentTime);
float t = 0;
float a = 0;
Vector3 scale = Vector3.one;
Vector3 position = Vector3.zero;
Vector2 sizeDelta = Vector2.zero;
Vector2 anchorMin = Vector2.zero;
Vector2 anchorMax = Vector2.zero;
if (visible) {
t = showCurve.Evaluate (currentTime);
a = Mathf.Lerp (image.color.a, alphaTarget, t);
scale = Vector3.Lerp (shownImage.rectTransform.localScale, Vector3.one, t);
position = Vector3.Lerp (shownImage.rectTransform.localPosition, Vector3.zero, t);
sizeDelta = Vector2.Lerp (shownImage.rectTransform.sizeDelta, Vector2.one, t);
anchorMin = Vector2.Lerp (shownImage.rectTransform.anchorMin, Vector2.zero, t);
anchorMax = Vector2.Lerp (shownImage.rectTransform.anchorMax, Vector2.one, t);
} else {
t = hideCurve.Evaluate (currentTime);
a = Mathf.Lerp (image.color.a, alphaTarget, t);
scale = Vector3.Lerp (shownImage.rectTransform.localScale, originalScale, t);
position = Vector3.Lerp (shownImage.rectTransform.localPosition, originalPosition, t);
sizeDelta = Vector2.Lerp (shownImage.rectTransform.sizeDelta, originalSizeDelta, t);
anchorMin = Vector2.Lerp (shownImage.rectTransform.anchorMin, originalAnchorMin, t);
anchorMax = Vector2.Lerp (shownImage.rectTransform.anchorMax, originalAnchorMax, t);
}
image.color = new Color (image.color.r, image.color.g, image.color.b, a);
shownImage.rectTransform.localScale = scale;
shownImage.rectTransform.localPosition = position;
shownImage.rectTransform.sizeDelta = sizeDelta;
shownImage.rectTransform.anchorMin = anchorMin;
shownImage.rectTransform.anchorMax = anchorMax;
}
}
public void show (Image showImage, RectTransform origin)
{
this.shownImage = showImage;
this.origin = origin;
shownImage.rectTransform.SetParent (this.image.rectTransform);
originalPosition = shownImage.rectTransform.localPosition;
originalScale = shownImage.rectTransform.localScale;
originalSizeDelta = shownImage.rectTransform.sizeDelta;
originalAnchorMax = shownImage.rectTransform.anchorMax;
originalAnchorMin = shownImage.rectTransform.anchorMin;
visible = true;
alphaTarget = 1.0f;
transitioning = true;
currentTime = 0;
}
public void hide ()
{
visible = false;
alphaTarget = 0;
transitioning = true;
currentTime = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment