Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SoraMahiro · Jan 30, 2017 at 07:25 AM · textalphaguitextienumeratorcolor.lerp

Fade in text one character at a time

I have written two enumerators, which I will provide. One Prints the character one at a time, the other changes the alpha of the text. However, I can't seem to wrap my head around how these two can be used in each other, and so for that I ask How can I implement the given FadeTo enumerator into the PrintText enumerator so that the characters not only print one at a time, but also fade in one character at a time?

 private IEnumerator FadeTo(float aValue, float tValue)
 {
     float alpha = GameText.GetComponent<Text>().color.a;
     for (float i = 0; i < 1.0f; i += Time.deltaTime / tValue)
     {
         Color alphaChange = new Color(1, 1, 1, Mathf.Lerp(alpha, aValue, i));
         GameText.GetComponent<Text>().color = alphaChange;
         yield return null;
     } 
 }
 private IEnumerator CharXChar(string text)
 {
     for (int i = 0; i < text.Length; i++)
     {
         yield return new WaitForSeconds(0.1f);
         GameText.GetComponent<Text>().text = GameText.GetComponent<Text>().text + text[i];
     }
 }
   
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SoraMahiro · Feb 04, 2017 at 06:42 AM

I can't take credit for this so I will link to the place I received the answer, but here is the gist. While this isn't very efficient, it works for what I'm doing:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;

 [RequireComponent(typeof(Text))]
 public class TextFade : MonoBehaviour
 {

     [Tooltip("Number of seconds each character should take to fade up")]
     public float fadeDuration = 2f;

     [Tooltip("Speed the reveal travels along the text, in characters per second")]
     public float travelSpeed = 8f;

     // Cached reference to our Text object.
     Text _text;

     Coroutine _fade;

     // Lookup table for hex characters.
     static readonly char[] NIBBLE_TO_HEX = new char[] {
         '0', '1', '2', '3',
         '4', '5', '6', '7',
         '8', '9', 'A', 'B',
         'C', 'D', 'E', 'F'};

     // Use this for initialization
     void Start()
     {
         _text = GetComponent<Text>();

         // If you don't want the text to fade right away, skip this line.
         FadeTo(_text.text);
     }

     public void FadeTo(string text)
     {
         // Abort a fade in progress, if any.
         StopFade();

         // Start fading, and keep track of the coroutine so we can interrupt if needed.
         _fade = StartCoroutine(FadeText(text));
     }

     public void StopFade()
     {
         if (_fade != null)
         StopCoroutine(_fade);
     }

     // Currently this expects a string of plain text,
     // and will not correctly handle rich text tags etc.
     IEnumerator FadeText(string text)
     {

         int length = text.Length;

         // Build a character buffer of our desired text,
         // with a rich text "color" tag around every character.
         var builder = new System.Text.StringBuilder(length * 26);
         Color32 color = _text.color;
         for (int i = 0; i < length; i++)
         {
             builder.Append("<color=#");
             builder.Append(NIBBLE_TO_HEX[color.r >> 4]);
             builder.Append(NIBBLE_TO_HEX[color.r & 0xF]);
             builder.Append(NIBBLE_TO_HEX[color.g >> 4]);
             builder.Append(NIBBLE_TO_HEX[color.g & 0xF]);
             builder.Append(NIBBLE_TO_HEX[color.b >> 4]);
             builder.Append(NIBBLE_TO_HEX[color.b & 0xF]);
             builder.Append("00>");
             builder.Append(text[i]);
             builder.Append("</color>");
         }

         // Each frame, update the alpha values along the fading frontier.
         float fadingProgress = 0f;
         int opaqueChars = -1;
         while (opaqueChars < length - 1)
         {
             yield return null;

             fadingProgress += Time.deltaTime;

             float leadingEdge = fadingProgress * travelSpeed;

             int lastChar = Mathf.Min(length - 1, Mathf.FloorToInt(leadingEdge));

             int newOpaque = opaqueChars;

             for (int i = lastChar; i > opaqueChars; i--)
             {
                 byte fade = (byte)(255f * Mathf.Clamp01((leadingEdge - i) / (travelSpeed * fadeDuration)));
                 builder[i * 26 + 14] = NIBBLE_TO_HEX[fade >> 4];
                 builder[i * 26 + 15] = NIBBLE_TO_HEX[fade & 0xF];

                 if (fade == 255)
                     newOpaque = Mathf.Max(newOpaque, i);
             }

             opaqueChars = newOpaque;

             // This allocates a new string.
             _text.text = builder.ToString();
         }

         // Once all the characters are opaque, 
         // ditch the unnecessary markup and end the routine.
         _text.text = text;

         // Mark the fade transition as finished.
         // This can also fire an event/message if you want to signal UI.
         _fade = null;
     }
 }

http://gamedev.stackexchange.com/questions/136588/fading-text-in-one-character-at-a-time?noredirect=1#comment240031_136588

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SoraMahiro · Feb 04, 2017 at 06:45 AM 0
Share

P.S. $$anonymous$$ake sure you create a new Canvas and attach this directly. It will automatically create a text component from which it will use. Don't modify anything except, I believe you can change the color (except alpha channel) and it will still work.

Edit

You can change everything except do not un-check RichText and don't change the alpha.

avatar image
1

Answer by Astiolo · Jan 31, 2017 at 04:12 AM

You're going to need to make a new text object for each new letter, in order to adjust its color separately to the rest of the string. Currently the CharXChar function is just adding characters to the same text object.

Then the FadeTo function should take the text object as an input and can then be called within the CharXChar function after the creation of each new character.

If you don't want to end up with an object for each character at the end, then you will need to combine the letters once they have finished fading in, and destroy the old objects.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SoraMahiro · Jan 31, 2017 at 10:01 PM 0
Share

Is, there any other way to do this? If not could you kindly provide and example? I work better seeing examples, even if they are pseudo.

avatar image Astiolo SoraMahiro · Feb 01, 2017 at 02:50 AM 0
Share

I don't know of any other way that wouldn't be more complicated. Unless you can find someone elses code or asset on the asset store.

Some untested and unfinished code would look something like this:

  private IEnumerator FadeTo(float aValue, float tValue, Text character)
  {
      float alpha = character.color.a;
      for (float i = 0; i < 1.0f; i += Time.deltaTime / tValue)
      {
          Color alphaChange = new Color(1, 1, 1, $$anonymous$$athf.Lerp(alpha, aValue, i));
          character.color = alphaChange;
          yield return null;
      } 
  }
  private IEnumerator CharXChar(string text)
  {
      for (int i = 0; i < text.Length; i++)
      {
          yield return new WaitForSeconds(0.1f);
 
          //create the character object
          GameObject character = Instantiate(Resources.Load("Single Character") as GameObject);
 
 
          //set the position of the character object here (this may be a little difficult once you start considering kerning)
 
          Text characterText = GameText.GetComponent<Text>();
          characterText.text + text[i];
          StartCoroutine(FadeTo(aValue, tValue, characterText));
      }
  }
avatar image
0

Answer by sysmaya · Feb 01, 2017 at 09:11 AM

Add this Class toObjects with Renderer component.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class BlinkColor : MonoBehaviour {
     public Color colorIni = Color.white;
     public Color colorFin = Color.red;
     public float duration = 3f;
 
     private Color lerpedColor = Color.white;
     private float t=0;
     private bool flag; 
     private Renderer _renderer;
 
     void Start () {
         _renderer=GetComponent<Renderer>(); 
     }
     
     void Update() {
         lerpedColor = Color.Lerp (colorIni, colorFin, t);
         _renderer.material.color = lerpedColor;
 
         if (flag == true) {
             t -= Time.deltaTime/duration;
             if (t < 0.01f)
                 flag = false;
         } else {
             t += Time.deltaTime/duration;
             if (t > 0.99f)
                 flag = true;
         }
     }
 }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

94 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fade In/out issue about a gameobject 0 Answers

Guitext hides PHP text on iphone 0 Answers

Find the position of text on the screen 1 Answer

How do I create a gui text component using c# script? 1 Answer

How to display EULA from Doc in Unity? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges