- Home /
How can I start fading out text upon setting it?
Is there a way to subclass UnityEngine.UI.Text
(or any game object or UI element for that matter) and add an instance of that to the scene? That way I can override the setter for text
property and start fading it out (or do something else) when value changed.
What I've read here suggests attaching a script to the object and using SetActive
to start fading: http://answers.unity3d.com/questions/418853/fading-gui-text.html
Is this the common way of doing it?
Answer by Scribe · Jun 25, 2015 at 12:02 PM
You can inherit from the UI.Text component and then add some functions and variables, for example you could create a class type FadeText like so:
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class FadeText : Text {
[MenuItem ("Component/UI/Fade Text", true, 15)]
static bool ValidateAddComponent(){
if(Selection.activeTransform.gameObject == null){
return false;
}
return true;
}
[MenuItem ("Component/UI/Fade Text", false, 15)]
static void AddComponent(){
Selection.activeTransform.gameObject.AddComponent<FadeText>();
}
[MenuItem ("GameObject/UI/Fade Text")]
static void AddText(){
new GameObject("Fade Text", typeof(FadeText));
}
void StartFade(){
Debug.Log("fading");
}
public string fadingText{
get { return this.text; }
set { this.text = value; StartFade(); }
}
}
and then use the class elsewhere as normal:
public FadeText thisText;
void Start () {
Debug.Log(thisText.fadingText);
thisText.fadingText = "test";
Debug.Log(thisText.fadingText);
}
This will set your FadeText.text value and also call your StartFade function, I'm pretty sure Text inherits from monobehaviour so your FadeText will also, and therefore you should be able to begin a coroutine within the class!
Hope that helps,
Scribe
Answer by kmgr · Jun 25, 2015 at 12:39 PM
To be honest there are a million ways of doing this. The easiest way I can think of is to attach a canvas group component and a second script that checks if Text value has changed. If it has, it starts a coroutine to set canvasGroup.alpha to zero over time. Or you can add an animator and an animation for the canvas group alpha and just set animator state when the value changes.
You can also do it as in the link you provided. Or you can subclass the UI.Text, or mix all of the above. There is no one proper way of doing this, it all depends on your code structure, scene hierarchy, your skills and whether you prefer doing things mostly in code or by utilizing the editor.
Answer by Marceta · Jun 25, 2015 at 12:38 PM
Something like this will do. Put this inside coroutine or update function.
do
{
yourTextObject.color.a -= 0.1 * Time.deltaTime * 0.05;
}
while (yourTextObject.color.a > 0);
Thanks but that's not what I'm looking for. Notes for your code snippet: color.a is read-only so you cannot do that. Plus, CrossFadeAlpha can do this (with some possible optimizations).
Your answer
Follow this Question
Related Questions
Using GetComponent with a subclass 3 Answers
Does Unity support Inheritance and Subclassing? 9 Answers
Does Unity support multiple Inheritance? 3 Answers
Fading a text gui style 3 Answers
Is Instance Of 3 Answers