Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class TestTextController : MonoBehaviour {
- public Text text;
- private enum States { test, next};
- private States myState;
- private float waitSystem;
- private bool skipWaiting = false;
- private bool fadingWorks = false;
- private bool fadeIn = false;
- private bool fadeOut = false;
- private bool test = true;
- // Use this for initialization
- void Start() {
- myState = States.test;
- text.color = new Color(text.color.r, text.color.g, text.color.b, 0);
- text.text = "TITLE EXAMPLE";
- }
- // Update is called once per frame
- void Update() {
- print("myState: " + myState);
- if (test) {
- StartCoroutine(TextFadeInAndOut(text, States.next));
- test = false;
- }
- }
- //=============================================
- IEnumerator WaitOrTap(float seconds) {
- skipWaiting = false;
- waitSystem = seconds;
- while (waitSystem > 0) {
- if (Input.GetKeyDown(KeyCode.Return)) {
- waitSystem = 0;
- skipWaiting = true;
- }
- else
- waitSystem -= Time.deltaTime;
- yield return null;
- }
- }
- void SetTargetAlpha(Text target, float alpha) {
- target.color = new Color(target.color.r, target.color.g, target.color.b, alpha);
- }
- void TextFadeIn(Text ttext) {
- float alpha = ttext.color.a * 255;
- alpha += 100 * Time.deltaTime;
- SetTargetAlpha(ttext, alpha / 255);
- }
- void TextFadeOut(Text ttext) {
- float alpha = ttext.color.a * 255;
- alpha -= 100 * Time.deltaTime;
- SetTargetAlpha(ttext, alpha / 255);
- }
- IEnumerator TextFadeInAndOut(Text targetText, States nextState) {
- fadingWorks = true;
- fadeIn = true;
- fadeOut = false;
- while (fadingWorks) {
- // Fading In
- yield return (TextFadeIn(targetText, nextState));
- fadeOut = true;
- // Text displayed with full alpha for WaitOrTap(seconds) time
- yield return WaitOrTap(2f);
- if (skipWaiting) {
- SetTargetAlpha(targetText, 0);
- myState = nextState;
- fadingWorks = false;
- fadeIn = false;
- skipWaiting = false;
- yield break;
- }
- //Fading Out
- yield return (TextFadeOut(targetText, nextState));
- fadingWorks = false;
- }
- myState = nextState;
- }
- IEnumerator TextFadeIn(Text targetText, States nextState) {
- while (fadeIn == true) {
- TextFadeIn(targetText);
- if (targetText.color.a >= 1) {
- fadeIn = false;
- }
- if (Input.GetKeyDown(KeyCode.Return)) {
- SetTargetAlpha(targetText, 0);
- myState = nextState;
- fadingWorks = false;
- fadeIn = false;
- yield break;
- }
- yield return null;
- }
- }
- IEnumerator TextFadeOut(Text targetText, States nextState) {
- while (fadeOut == true) {
- TextFadeOut(targetText);
- if (targetText.color.a <= 0) {
- fadeOut = false;
- }
- if (Input.GetKeyDown(KeyCode.Return)) {
- SetTargetAlpha(targetText, 0);
- myState = nextState;
- fadingWorks = false;
- fadeOut = false;
- yield break;
- }
- yield return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement