Advertisement
Guest User

TestTextController

a guest
Aug 9th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class TestTextController : MonoBehaviour {
  6.  
  7.     public Text text;
  8.  
  9.     private enum States { test, next};
  10.     private States myState;
  11.     private float waitSystem;
  12.  
  13.     private bool skipWaiting = false;
  14.     private bool fadingWorks = false;
  15.     private bool fadeIn = false;
  16.     private bool fadeOut = false;
  17.     private bool test = true;
  18.  
  19.  
  20.     // Use this for initialization
  21.     void Start() {
  22.         myState = States.test;
  23.         text.color = new Color(text.color.r, text.color.g, text.color.b, 0);
  24.         text.text = "TITLE EXAMPLE";
  25.     }
  26.  
  27.     // Update is called once per frame
  28.     void Update() {      
  29.         print("myState: " + myState);
  30.  
  31.         if (test) {
  32.             StartCoroutine(TextFadeInAndOut(text, States.next));
  33.             test = false;
  34.         }
  35.     }
  36.    
  37.     //=============================================
  38.  
  39.     IEnumerator WaitOrTap(float seconds) {
  40.         skipWaiting = false;
  41.         waitSystem = seconds;
  42.         while (waitSystem > 0) {
  43.             if (Input.GetKeyDown(KeyCode.Return)) {
  44.                 waitSystem = 0;
  45.                 skipWaiting = true;
  46.             }
  47.             else
  48.                 waitSystem -= Time.deltaTime;
  49.             yield return null;
  50.         }
  51.     }
  52.  
  53.  
  54.     void SetTargetAlpha(Text target, float alpha) {
  55.         target.color = new Color(target.color.r, target.color.g, target.color.b, alpha);
  56.     }
  57.  
  58.  
  59.     void TextFadeIn(Text ttext) {
  60.         float alpha = ttext.color.a * 255;
  61.         alpha += 100 * Time.deltaTime;
  62.         SetTargetAlpha(ttext, alpha / 255);
  63.     }
  64.  
  65.     void TextFadeOut(Text ttext) {
  66.         float alpha = ttext.color.a * 255;
  67.         alpha -= 100 * Time.deltaTime;
  68.         SetTargetAlpha(ttext, alpha / 255);
  69.     }
  70.  
  71.  
  72.     IEnumerator TextFadeInAndOut(Text targetText, States nextState) {
  73.         fadingWorks = true;
  74.         fadeIn = true;
  75.         fadeOut = false;
  76.  
  77.         while (fadingWorks) {
  78.             // Fading In
  79.             yield return (TextFadeIn(targetText, nextState));
  80.             fadeOut = true;
  81.  
  82.             // Text displayed with full alpha for WaitOrTap(seconds) time            
  83.             yield return WaitOrTap(2f);
  84.  
  85.             if (skipWaiting) {
  86.                 SetTargetAlpha(targetText, 0);
  87.                 myState = nextState;
  88.                 fadingWorks = false;
  89.                 fadeIn = false;
  90.                 skipWaiting = false;
  91.                 yield break;
  92.             }
  93.  
  94.             //Fading Out
  95.             yield return (TextFadeOut(targetText, nextState));
  96.             fadingWorks = false;
  97.         }
  98.         myState = nextState;
  99.     }
  100.  
  101.     IEnumerator TextFadeIn(Text targetText, States nextState) {
  102.         while (fadeIn == true) {
  103.             TextFadeIn(targetText);
  104.  
  105.             if (targetText.color.a >= 1) {
  106.                 fadeIn = false;                
  107.             }
  108.  
  109.             if (Input.GetKeyDown(KeyCode.Return)) {
  110.                 SetTargetAlpha(targetText, 0);
  111.                 myState = nextState;
  112.                 fadingWorks = false;
  113.                 fadeIn = false;
  114.                 yield break;
  115.             }
  116.             yield return null;
  117.         }
  118.     }
  119.  
  120.  
  121.     IEnumerator TextFadeOut(Text targetText, States nextState) {
  122.         while (fadeOut == true) {
  123.             TextFadeOut(targetText);
  124.  
  125.             if (targetText.color.a <= 0) {
  126.                 fadeOut = false;
  127.             }
  128.  
  129.             if (Input.GetKeyDown(KeyCode.Return)) {
  130.                 SetTargetAlpha(targetText, 0);
  131.                 myState = nextState;
  132.                 fadingWorks = false;
  133.                 fadeOut = false;
  134.                 yield break;
  135.             }
  136.             yield return null;
  137.         }
  138.     }        
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement