Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 6ugra · Jun 25, 2015 at 10:46 AM · textinheritancefadingsubclassing

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?

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
1
Best Answer

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

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
avatar image
1

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.

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
avatar image
0

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);
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 6ugra · Jun 26, 2015 at 03:57 PM 0
Share

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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