using System.Collections;
using System.Collections.Generic;
#if TEXTMESHPRO_INSTALLED
using TMPro;
#endif
using UnityEngine;
using System.Threading;
using UnityEngine.UI;
namespace EasyTalk.Animation
{
///
/// The UIAnimator can be used to animate UI components with fading or sliding animations.
///
public class UIAnimator
{
///
/// A mapping of component instance IDs to the original alpha values of various UI elements.
///
private static Dictionary storedElementAlphas = new Dictionary();
///
/// Mapping used to keep track of the number of animations running for a given UI element.
///
public static Dictionary itemsAnimationCounter = new Dictionary();
///
/// Returns true if the specified UI element is currently being animated.
///
/// The instance ID of the UI element to check.
/// Whether the specified UI element is animating.
public static bool IsItemAnimating(int id)
{
if (itemsAnimationCounter.ContainsKey(id))
{
return true;
}
return false;
}
///
/// Returns true if the specified GameObject is currently being animated.
///
/// The GameObject to check.
/// Whether the specified GameObject is animating.
public static bool IsItemAnimating(GameObject gameObject)
{
if (itemsAnimationCounter.ContainsKey(gameObject.GetInstanceID()))
{
return true;
}
return false;
}
///
/// Increments the running animation counter for the specified ID.
///
/// The instance ID of the object an animation is starting on.
private static void StartingItemAnimation(int id)
{
if (itemsAnimationCounter.ContainsKey(id))
{
itemsAnimationCounter[id]++;
}
else
{
itemsAnimationCounter.Add(id, 1);
}
}
///
/// Decrements the running animation counter for the specified ID.
///
/// The isntance ID of the object an animation is ending on.
private static void FinishedItemAnimation(int id)
{
if (itemsAnimationCounter.ContainsKey(id))
{
itemsAnimationCounter[id]--;
if (itemsAnimationCounter[id] == 0)
{
itemsAnimationCounter.Remove(id);
}
}
}
///
/// Fades text to a specified alpha value over the specified time period.
///
/// The text element to animate the alpha value of.
/// The duration, in seconds, to spend animating the text.
/// The final alpha value desired for the text.
/// An animation curve controlling how quickly the text is faded.
///
#if TEXTMESHPRO_INSTALLED
public static IEnumerator FadeText(TMP_Text text, float duration, float finalAlpha, AnimationCurve curve = null)
#else
public static IEnumerator FadeText(Text text, float duration, float finalAlpha, AnimationCurve curve = null)
#endif
{
StartingItemAnimation(text.GetInstanceID());
float startTime = Time.time;
Color oldColor = text.color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, finalAlpha);
while (Thread.CurrentThread.IsAlive && (Time.time - startTime) < duration)
{
float progress = (Time.time - startTime) / duration;
if (curve != null) { progress = curve.Evaluate(progress); }
text.color = Color.Lerp(oldColor, newColor, progress);
yield return new WaitForEndOfFrame();
}
text.color = newColor;
FinishedItemAnimation(text.GetInstanceID());
}
///
/// Fade text to a specified alpha value immediately.
///
/// The text element to fade.
/// The final alpha value.
#if TEXTMESHPRO_INSTALLED
public static void FadeTextImmediately(TMP_Text text, float finalAlpha)
#else
public static void FadeTextImmediately(Text text, float finalAlpha)
#endif
{
text.color = new Color(text.color.r, text.color.g, text.color.b, finalAlpha);
}
///
/// Fade text in to its original alpha value. (This assumes that the original alpha value is stored)
///
/// The text element to fade in.
/// The duration of the fade in animation in seconds.
/// The animation curve used to control the animation timing.
///
#if TEXTMESHPRO_INSTALLED
public static IEnumerator FadeTextIn(TMP_Text text, float duration, AnimationCurve curve = null)
#else
public static IEnumerator FadeTextIn(Text text, float duration, AnimationCurve curve = null)
#endif
{
yield return FadeText(text, duration, GetOriginalOpacity(text.GetInstanceID()), curve);
}
///
/// Fades text in to its original alpha value immediately.
///
/// The text element to fade in.
#if TEXTMESHPRO_INSTALLED
public static void FadeTextInImmediately(TMP_Text text)
#else
public static void FadeTextInImmediately(Text text)
#endif
{
FadeTextImmediately(text, GetOriginalOpacity(text.GetInstanceID()));
}
///
/// Fades text out to be completely transparent over a specified duration.
///
/// The text element to fade out.
/// The duration of the animation, in seconds.
/// The animation curve used to control the animation timing.
///
#if TEXTMESHPRO_INSTALLED
public static IEnumerator FadeTextOut(TMP_Text text, float duration, AnimationCurve curve = null)
#else
public static IEnumerator FadeTextOut(Text text, float duration, AnimationCurve curve = null)
#endif
{
StoreOriginalOpacity(text, text.color.a);
yield return FadeText(text, duration, 0.0f, curve);
}
///
/// Fades text out to be completely transparent immediately.
///
/// The text element to fade out.
#if TEXTMESHPRO_INSTALLED
public static void FadeTextOutImmediately(TMP_Text text)
#else
public static void FadeTextOutImmediately(Text text)
#endif
{
StoreOriginalOpacity(text, text.color.a);
FadeTextImmediately(text, 0.0f);
}
///
/// Fades an image to a specified alpha value over time.
///
/// The image to animate.
/// The duration of the fade animation, in seconds.
/// The final alpha value to fade to.
/// An animation curve used to control the animation timing.
///
public static IEnumerator FadeImage(Image image, float duration, float finalAlpha, AnimationCurve curve = null)
{
StartingItemAnimation(image.GetInstanceID());
float startTime = Time.time;
Color oldColor = image.color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, finalAlpha);
while (Thread.CurrentThread.IsAlive && (Time.time - startTime) < duration)
{
float progress = (Time.time - startTime) / duration;
if (curve != null) { progress = curve.Evaluate(progress); }
image.color = Color.Lerp(oldColor, newColor, progress);
yield return new WaitForEndOfFrame();
}
image.color = newColor;
FinishedItemAnimation(image.GetInstanceID());
}
///
/// Fades an image to the specified alpha value immediately.
///
/// The image to fade.
/// The final alpha value to use.
public static void FadeImageImmediately(Image image, float finalAlpha)
{
image.color = new Color(image.color.r, image.color.g, image.color.b, finalAlpha);
}
///
/// Fades an image in to its original alpha value. (This assumes that the original alpha value is stored)
///
/// The image to fade in.
/// The duration of the fade in animation, in seconds.
/// The animation curve used to control the animation timing.
///
public static IEnumerator FadeImageIn(Image image, float duration, AnimationCurve curve = null)
{
yield return FadeImage(image, duration, GetOriginalOpacity(image.GetInstanceID()), curve);
}
///
/// Fades an image in to its stored alpha value immediately.
///
/// The image to fade in.
public static void FadeImageInImmediately(Image image)
{
FadeImageImmediately(image, GetOriginalOpacity(image.GetInstanceID()));
}
///
/// Fades an image out to be completely transparent over a specified duration.
///
/// The image to fade out.
/// The duration of the animation in seconds.
/// An animation curve used to control the animation timing.
///
public static IEnumerator FadeImageOut(Image image, float duration, AnimationCurve curve = null)
{
StoreOriginalOpacity(image, image.color.a);
yield return FadeImage(image, duration, 0.0f, curve);
}
///
/// Fades an image out to be completely transparent immediately.
///
/// The image to make transparent.
public static void FadeImageOutImmediately(Image image)
{
StoreOriginalOpacity(image, image.color.a);
FadeImageImmediately(image, 0.0f);
}
///
/// Fades all text and image components which are components of, or child components of the specified GameObject to an alpha value over a specified duration.
///
/// The GameObject to fade.
/// The duration of the fade animation, in seconds.
/// The final desired alpha value for the components being faded.
/// An animation curve used to control the animation timing.
///
public static IEnumerator Fade(GameObject gameObject, float duration, float finalAlpha, AnimationCurve curve = null)
{
StartingItemAnimation(gameObject.GetInstanceID());
Image[] images = gameObject.GetComponentsInChildren();
#if TEXTMESHPRO_INSTALLED
TMP_Text[] texts = gameObject.GetComponentsInChildren();
#else
Text[] texts = gameObject.GetComponentsInChildren();
#endif
Dictionary oldElementAlphas = new Dictionary();
float startTime = Time.time;
while (Thread.CurrentThread.IsAlive && (Time.time - startTime) < duration)
{
float progress = (Time.time - startTime) / duration;
if (curve != null) { progress = curve.Evaluate(progress); }
foreach (Image image in images)
{
if (image.GetComponent() != null) { continue; }
float oldOpacity;
if (!oldElementAlphas.ContainsKey(image.GetInstanceID()))
{
oldElementAlphas.Add(image.GetInstanceID(), image.color.a);
}
oldOpacity = oldElementAlphas[image.GetInstanceID()];
Color oldColor = new Color(image.color.r, image.color.g, image.color.b, oldOpacity);
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, finalAlpha);
image.color = Color.Lerp(oldColor, newColor, progress);
}
#if TEXTMESHPRO_INSTALLED
foreach (TMP_Text text in texts)
#else
foreach (Text text in texts)
#endif
{
float oldOpacity;
if (!oldElementAlphas.ContainsKey(text.GetInstanceID()))
{
oldElementAlphas.Add(text.GetInstanceID(), text.color.a);
}
oldOpacity = oldElementAlphas[text.GetInstanceID()];
Color oldColor = new Color(text.color.r, text.color.g, text.color.b, oldOpacity);
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, finalAlpha);
text.color = Color.Lerp(oldColor, newColor, progress);
}
yield return new WaitForEndOfFrame();
}
foreach (Image image in images)
{
if (image.GetComponent() != null) { continue; }
image.color = new Color(image.color.r, image.color.g, image.color.b, finalAlpha);
}
#if TEXTMESHPRO_INSTALLED
foreach (TMP_Text text in texts)
#else
foreach (Text text in texts)
#endif
{
text.color = new Color(text.color.r, text.color.g, text.color.b, finalAlpha);
}
oldElementAlphas.Clear();
FinishedItemAnimation(gameObject.GetInstanceID());
}
///
/// Immediately fades all text and image elements which are components of, or child components of the specified GameObject to the specified alpha value.
///
/// The GameObject to fade.
/// The final desired alpha value.
public static void FadeImmediately(GameObject gameObject, float finalAlpha)
{
Image[] images = gameObject.GetComponentsInChildren();
#if TEXTMESHPRO_INSTALLED
TMP_Text[] texts = gameObject.GetComponentsInChildren();
#else
Text[] texts = gameObject.GetComponentsInChildren();
#endif
foreach (Image image in images)
{
if (image.GetComponent() != null) { continue; }
image.color = new Color(image.color.r, image.color.g, image.color.b, finalAlpha);
}
#if TEXTMESHPRO_INSTALLED
foreach (TMP_Text text in texts)
#else
foreach (Text text in texts)
#endif
{
text.color = new Color(text.color.r, text.color.g, text.color.b, finalAlpha);
}
}
///
/// Clears the stored alpha values for the specified instance ID.
///
/// The instance ID of the component or GameObject to clear.
public static void ClearStoredAlpha(int instanceID)
{
if (storedElementAlphas.ContainsKey(instanceID))
{
storedElementAlphas.Remove(instanceID);
}
}
///
/// Clears the stored alpha values for all components and child components of the specified GameObject.
///
/// The GameObject to clear stored alpha values for.
public static void ClearStoredAlphas(GameObject gameObject)
{
Image[] images = gameObject.GetComponentsInChildren();
#if TEXTMESHPRO_INSTALLED
TMP_Text[] texts = gameObject.GetComponentsInChildren();
#else
Text[] texts = gameObject.GetComponentsInChildren();
#endif
foreach (Image image in images)
{
ClearStoredAlpha(image.GetInstanceID());
}
#if TEXTMESHPRO_INSTALLED
foreach (TMP_Text text in texts)
#else
foreach (Text text in texts)
#endif
{
ClearStoredAlpha(text.GetInstanceID());
}
}
///
/// Clears all stored alpha values.
///
public static void ClearAllStoredAlphas()
{
storedElementAlphas.Clear();
}
///
/// Fades in the components and child components of the specified GameObject to their original stored alpha values over the specified duration.
///
/// The GameObject to fade in.
/// The duration of the fade in animation, in seconds.
/// An animation curve used to control the animation timing.
///
public static IEnumerator FadeIn(GameObject gameObject, float duration, AnimationCurve curve = null)
{
StartingItemAnimation(gameObject.GetInstanceID());
Image[] images = gameObject.GetComponentsInChildren();
#if TEXTMESHPRO_INSTALLED
TMP_Text[] texts = gameObject.GetComponentsInChildren();
#else
Text[] texts = gameObject.GetComponentsInChildren();
#endif
Dictionary oldElementAlphas = new Dictionary();
float startTime = Time.time;
while (Thread.CurrentThread.IsAlive && (Time.time - startTime) < duration)
{
float progress = (Time.time - startTime) / duration;
if (curve != null) { progress = curve.Evaluate(progress); }
foreach (Image image in images)
{
if (image.GetComponent() != null) { continue; }
if (!oldElementAlphas.ContainsKey(image.GetInstanceID()))
{
oldElementAlphas.Add(image.GetInstanceID(), image.color.a);
}
float oldOpacity = oldElementAlphas[image.GetInstanceID()];
Color oldColor = new Color(image.color.r, image.color.g, image.color.b, oldOpacity);
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, GetOriginalOpacity(image.GetInstanceID()));
image.color = Color.Lerp(oldColor, newColor, progress);
}
#if TEXTMESHPRO_INSTALLED
foreach (TMP_Text text in texts)
#else
foreach (Text text in texts)
#endif
{
if (!oldElementAlphas.ContainsKey(text.GetInstanceID()))
{
oldElementAlphas.Add(text.GetInstanceID(), text.color.a);
}
float oldOpacity = oldElementAlphas[text.GetInstanceID()];
Color oldColor = new Color(text.color.r, text.color.g, text.color.b, oldOpacity);
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, GetOriginalOpacity(text.GetInstanceID()));
text.color = Color.Lerp(oldColor, newColor, progress);
}
yield return new WaitForEndOfFrame();
}
foreach (Image image in images)
{
if (image.GetComponent() != null) { continue; }
image.color = new Color(image.color.r, image.color.g, image.color.b, GetOriginalOpacity(image.GetInstanceID()));
}
#if TEXTMESHPRO_INSTALLED
foreach (TMP_Text text in texts)
#else
foreach (Text text in texts)
#endif
{
text.color = new Color(text.color.r, text.color.g, text.color.b, GetOriginalOpacity(text.GetInstanceID()));
}
oldElementAlphas.Clear();
FinishedItemAnimation(gameObject.GetInstanceID());
}
///
/// Fades in all text and image components and child components of the specified GameObject to their original stored alpha values immediately.
///
/// The GameObject to fade in.
public static void FadeInImmediately(GameObject gameObject)
{
Image[] images = gameObject.GetComponentsInChildren();
#if TEXTMESHPRO_INSTALLED
TMP_Text[] texts = gameObject.GetComponentsInChildren();
#else
Text[] texts = gameObject.GetComponentsInChildren();
#endif
foreach (Image image in images)
{
if (image.GetComponent() != null) { continue; }
image.color = new Color(image.color.r, image.color.g, image.color.b, GetOriginalOpacity(image.GetInstanceID()));
}
#if TEXTMESHPRO_INSTALLED
foreach (TMP_Text text in texts)
#else
foreach (Text text in texts)
#endif
{
text.color = new Color(text.color.r, text.color.g, text.color.b, GetOriginalOpacity(text.GetInstanceID()));
}
}
///
/// Fades out all text and image components and child components of the specified GameObject to be completely transparent over the specified duration.
///
/// The GameObject to fade out.
/// The duration of the fade out animation, in seconds.
/// An animation curve used to control the animation timing.
///
public static IEnumerator FadeOut(GameObject gameObject, float duration, AnimationCurve curve = null)
{
StoreOriginalOpacities(gameObject);
yield return Fade(gameObject, duration, 0.0f, curve);
}
///
/// Fades out all text and image components and child components of the specified GameObject to be completely transparent immediately.
///
/// The GameObject to fade out.
public static void FadeOutImmediately(GameObject gameObject)
{
StoreOriginalOpacities(gameObject);
FadeImmediately(gameObject, 0.0f);
}
///
/// Returns the original opacity value which is stored for the specified instance ID, or a value of 1.0 if no stored value is found.
///
/// The instance ID to retrieve a stored opacity value for.
/// The original opacity value of the specified instance ID, or 1.0 if no value is stored.
private static float GetOriginalOpacity(int instanceId)
{
if (storedElementAlphas.ContainsKey(instanceId))
{
return storedElementAlphas[instanceId];
}
return 1.0f;
}
///
/// Stores an opacity value for the specified object instance.
///
/// The object to store an opacity value of.
/// The opacity value to store.
private static void StoreOriginalOpacity(Object obj, float opacity)
{
if (!storedElementAlphas.ContainsKey(obj.GetInstanceID()))
{
storedElementAlphas.Add(obj.GetInstanceID(), opacity);
}
}
///
/// Stores opacity values for each text and image component in the specified GameObject.
///
/// The GameObject to store opacity values of.
public static void StoreOriginalOpacities(GameObject gameObject)
{
Image[] images = gameObject.GetComponentsInChildren();
#if TEXTMESHPRO_INSTALLED
TMP_Text[] texts = gameObject.GetComponentsInChildren();
#else
Text[] texts = gameObject.GetComponentsInChildren();
#endif
foreach (Image image in images)
{
StoreOriginalOpacity(image, image.color.a);
}
#if TEXTMESHPRO_INSTALLED
foreach (TMP_Text text in texts)
#else
foreach (Text text in texts)
#endif
{
StoreOriginalOpacity(text, text.color.a);
}
}
///
/// Slides the specified RectTransform into a Canvas' bounds over a specified period of time.
///
/// The Canvas which the transform should slide into.
/// The RectTransform of the component to slide in.
/// The duration of the slide animation.
/// An animation curve used to control the animation timing.
/// An optional additional margin offset for the final slide in position.
///
public static IEnumerator SlideInComponent(Canvas canvas, RectTransform transform, float duration, AnimationCurve curve = null, float margin = 0.0f)
{
StartingItemAnimation(transform.gameObject.GetInstanceID());
Vector3 originalPos = transform.localPosition;
Vector3 finalPos = FindPositionToShow(canvas, transform, margin);
float startTime = Time.time;
while ((Time.time - startTime) < duration)
{
float progress = (Time.time - startTime) / duration;
if (curve != null) { progress = curve.Evaluate(progress); }
transform.localPosition = Vector3.Lerp(originalPos, finalPos, progress);
yield return new WaitForEndOfFrame();
}
transform.localPosition = finalPos;
FinishedItemAnimation(transform.gameObject.GetInstanceID());
}
///
/// Slides the specified RectTransform into a Canvas' bounds immediately.
///
/// The Canvas which the transform should slide into.
/// The RectTransform of the component to slide in.
/// An optional additional margin offset for the final slide in position.
public static void SlideInComponentImmediately(Canvas canvas, RectTransform transform, float margin = 0.0f)
{
transform.localPosition = FindPositionToShow(canvas, transform, margin);
}
///
/// Slides the specified RectTransform out of a Canvas' bounds over a specified period of time.
///
/// The Canvas which the transform should slide out of.
/// The RectTransform of the component to slide out.
/// The direction that the component should slide out.
/// The duration of the animation, in seconds.
/// An animation curve used to control the animation timing.
///
public static IEnumerator SlideOutComponent(Canvas canvas, RectTransform transform, SlideDirection direction, float duration, AnimationCurve curve = null)
{
StartingItemAnimation(transform.gameObject.GetInstanceID());
Vector3 originalPos = transform.localPosition;
Vector3 finalPos = FindPositionToHide(canvas, transform, direction);
float startTime = Time.time;
while ((Time.time - startTime) < duration)
{
float progress = (Time.time - startTime) / duration;
if (curve != null) { progress = curve.Evaluate(progress); }
transform.localPosition = Vector3.Lerp(originalPos, finalPos, progress);
yield return new WaitForEndOfFrame();
}
transform.localPosition = finalPos;
FinishedItemAnimation(transform.gameObject.GetInstanceID());
}
///
/// Slides the specified RectTransform out of a Canvas' bounds immediately.
///
/// The Canvas which the transform should slide out of.
/// The RectTransform of the component to slide out.
/// The direction that the component should slide out.
public static void SlideOutComponentImmediately(Canvas canvas, RectTransform transform, SlideDirection direction)
{
transform.localPosition = FindPositionToHide(canvas, transform, direction);
}
///
/// Calculates the position to move the specified RectTransform to in order for it to be hidden and outside of the Canvas provided.
///
/// The Canvas which the RectTransform should be hidden in.
/// The RectTransform to hide.
/// The direction to slide the RectTransform in when hiding it.
/// The position to move the RectTransform to so that it is hidden.
private static Vector3 FindPositionToHide(Canvas canvas, RectTransform transform, SlideDirection direction)
{
Vector3[] objectCorners = new Vector3[4];
transform.GetLocalCorners(objectCorners);
RectTransform canvasTransform = canvas.GetComponent();
Vector3[] canvasCorners = new Vector3[4];
canvasTransform.GetWorldCorners(canvasCorners);
canvasCorners[0] = transform.InverseTransformPoint(canvasCorners[0]);
canvasCorners[1] = transform.InverseTransformPoint(canvasCorners[1]);
canvasCorners[2] = transform.InverseTransformPoint(canvasCorners[2]);
canvasCorners[3] = transform.InverseTransformPoint(canvasCorners[3]);
switch (direction)
{
case SlideDirection.LEFT:
return transform.localPosition - new Vector3(objectCorners[2].x - canvasCorners[0].x, 0.0f, 0.0f);
case SlideDirection.RIGHT:
return transform.localPosition + new Vector3(canvasCorners[2].x - objectCorners[0].x, 0.0f, 0.0f);
case SlideDirection.UP:
return transform.localPosition + new Vector3(0.0f, canvasCorners[2].y - objectCorners[0].y, 0.0f);
case SlideDirection.DOWN:
return transform.localPosition - new Vector3(0.0f, objectCorners[2].y - canvasCorners[0].y, 0.0f);
}
return transform.localPosition;
}
///
/// Calculates the position to move the specified RectTransform to in order for it to be shown inside the Canvas provided.
///
/// The Canvas which the RectTransform should be shown in.
/// The RectTransform to show.
/// An option margin offset.
/// The position which the RectTransform should be moved to in order to show it.
private static Vector3 FindPositionToShow(Canvas canvas, RectTransform transform, float margin = 0.0f)
{
Vector3[] optionCorners = new Vector3[4];
transform.GetLocalCorners(optionCorners);
RectTransform canvasTransform = canvas.GetComponent();
Vector3[] canvasCorners = new Vector3[4];
canvasTransform.GetWorldCorners(canvasCorners);
canvasCorners[0] = transform.InverseTransformPoint(canvasCorners[0]);
canvasCorners[1] = transform.InverseTransformPoint(canvasCorners[1]);
canvasCorners[2] = transform.InverseTransformPoint(canvasCorners[2]);
canvasCorners[3] = transform.InverseTransformPoint(canvasCorners[3]);
if (optionCorners[2].x > canvasCorners[2].x)
{
//Top right is to the right of the screen, so we have to slide left.
return transform.localPosition - new Vector3((optionCorners[2].x - canvasCorners[2].x) + margin, 0.0f, 0.0f);
}
else if (optionCorners[0].x < canvasCorners[0].x)
{
//Bottom left is to the left of the screen, so we have to slide right.
return transform.localPosition + new Vector3((canvasCorners[0].x - optionCorners[0].x) + margin, 0.0f, 0.0f);
}
else if (optionCorners[0].y < canvasCorners[0].y)
{
//Bottom left is below the bottom of the screen, so we have to slide up.
return transform.localPosition + new Vector3(0.0f, (canvasCorners[0].y - optionCorners[0].y) + margin, 0.0f);
}
else if (optionCorners[1].y > canvasCorners[1].y)
{
//Top left is above the top of the screen, so we have to slide down.
return transform.localPosition - new Vector3(0.0f, (optionCorners[2].y - canvasCorners[2].y) + margin, 0.0f);
}
else return transform.localPosition;
}
///
/// Slides the specified RectTransform to a position over the specified duration.
///
/// The RectTransform to slide.
/// The position to move the RectTransform to.
/// The duration of the animation.
/// An animation curve used to control the animation timing.
///
public static IEnumerator SlideComponentToPosition(RectTransform transform, Vector3 newPos, float duration, AnimationCurve curve = null)
{
StartingItemAnimation(transform.gameObject.GetInstanceID());
float startTime = Time.time;
Vector3 oldPos = transform.localPosition;
while (Thread.CurrentThread.IsAlive && (Time.time - startTime) < duration)
{
float progress = (Time.time - startTime) / duration;
if (curve != null) { progress = curve.Evaluate(progress); }
transform.localPosition = Vector3.Lerp(oldPos, newPos, progress);
yield return new WaitForEndOfFrame();
}
transform.localPosition = newPos;
FinishedItemAnimation(transform.gameObject.GetInstanceID());
}
///
/// Slides the specified RectTransform to a position immediately.
///
/// The RectTransform to slide.
/// The position to move the RectTransform to.
public static void SlideComponentToPositionImmediately(RectTransform transform, Vector3 newPos)
{
transform.localPosition = newPos;
}
///
/// Returns the corresponding SlideDirection associated with the specified Animation type.
///
/// The animation type to get a slide direction for.
/// The associated slide direction for the specified animation, or NONE if there is none.
public static UIAnimator.SlideDirection GetSlideDirection(UIAnimator.Animation animation)
{
switch (animation)
{
case Animation.SLIDE_LEFT: return SlideDirection.LEFT;
case Animation.SLIDE_RIGHT: return SlideDirection.RIGHT;
case Animation.SLIDE_UP: return SlideDirection.UP;
case Animation.SLIDE_DOWN: return SlideDirection.DOWN;
}
return SlideDirection.NONE;
}
///
/// An enum of possible slide animation directions.
///
public enum SlideDirection { NONE, LEFT, RIGHT, UP, DOWN }
///
/// An enum of supported animation types.
///
public enum Animation { NONE, SLIDE_LEFT, SLIDE_RIGHT, SLIDE_UP, SLIDE_DOWN, FADE }
}
}