Created
May 10, 2017 10:56
-
-
Save GeorgiyRyaposov/1585f78d0987d1d8c5c0fee3eda5d737 to your computer and use it in GitHub Desktop.
EmbedText
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
using UnityEngine.UI; | |
namespace Scripts.Utilities | |
{ | |
public class EmbedText : MonoBehaviour | |
{ | |
[SerializeField] | |
private Text textLabel; | |
[SerializeField] | |
private Text textShadowLabel; | |
[HideInInspector] | |
public RectTransform rectTransform; | |
private Color defaultTextColor, defaultShadowTextColor, defaultTextColorFadeout, defaultShadowTextColorFadeout; | |
private RectTransform textShadowRectTransform; | |
private Vector3 defaultPosition; | |
private void Awake() | |
{ | |
rectTransform = GetComponent<RectTransform>(); | |
defaultTextColor = textLabel.color; | |
defaultShadowTextColor = textShadowLabel.color; | |
defaultTextColorFadeout = defaultTextColor; | |
defaultShadowTextColorFadeout = defaultShadowTextColor; | |
defaultTextColorFadeout.a = 0f; | |
defaultShadowTextColorFadeout.a = 0f; | |
textShadowRectTransform = textShadowLabel.rectTransform; | |
defaultPosition = textShadowRectTransform.localPosition; | |
} | |
public void SetText(string text) | |
{ | |
textLabel.text = text; | |
textShadowLabel.text = text; | |
} | |
public void FloatAndFadeOut() | |
{ | |
StartCoroutine(FloatAndFadeOutCoroutine()); | |
} | |
private IEnumerator FloatAndFadeOutCoroutine() | |
{ | |
var progress = 0f; | |
var startPosition = textShadowRectTransform.position; | |
var targetPosition = startPosition; | |
targetPosition.y += 3f; | |
while (progress <= 1f) | |
{ | |
textLabel.color = Color.Lerp(defaultTextColor, defaultTextColorFadeout, progress); | |
textShadowLabel.color = Color.Lerp(defaultShadowTextColor, defaultShadowTextColorFadeout, progress); | |
textShadowRectTransform.position = Vector3.Lerp(startPosition, targetPosition, progress); | |
progress += Time.unscaledDeltaTime * 0.4f; | |
yield return null; | |
} | |
textShadowRectTransform.localPosition = defaultPosition; | |
this.gameObject.Recycle(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment