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
2
Question by LilGames · Sep 29, 2017 at 09:09 PM · c#textoverrideextend

Can you override/extend text property?

I'm looking for a way to override or extend the text property of a Text component.

What I mean by this is, every time I set my Text component like this:

myText.text = "hello world";

I want that to trigger another set of code, without explicitly calling those other functions.

Back in the Flash Actionscript days, I would extend the main class and keep its original intended behaviour using Super(). Is there something similar in Unity C# and how would I do it?

Comment
Add comment · Show 2
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 MaxGuernseyIII · Sep 29, 2017 at 09:50 PM 0
Share

Yeah. @$$anonymous$$acDX and @FortisVenaliter are pointing you in the right direction. If at all possible, encapsulate framework-y stuff and proxy it to intercept events like this. I'll give you a third option for spice but, I warn you, theirs are the better road. $$anonymous$$ine is the "if you can't do it that way, then I guess you have to do it this way" way and it won't be instant.

avatar image LilGames MaxGuernseyIII · Sep 30, 2017 at 02:07 AM 0
Share

I appreciate your and others' replies. It's going to take me a while to dig through the 3 or so flavours of solution here before marking an answer. Thanks!

3 Replies

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

Answer by MacDx · Sep 29, 2017 at 09:25 PM

Properties are basically just get and set methods for a field. You can't really make it "trigger" another set of code without explicitly calling it, any code or language feature that does it, will eventually call a function or a delegate, event, etc. under the hood, after the modification of the value.

My suggestion is to create a property inside the script that contains the myText field that calls that other set of code right after modifying the value of myText.text, something along these lines:

 //This is the property
 public string MyText
 {
     get
     {
         return myText.text;
     }
     set
     {
         myText.text = value;
         //Here call your other set of code, I would suggest a method like this
         //maybe if you want to do something with the new value
         OnTextValueChanged(value);
     }
 }

This allows you to use it like this:

 MyText = "newStringValue";

so that would update the value of myText.text and also "automatically" call your other set of code, passing "newStringValue" as a parameter to it.

Hope this helps!

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 LilGames · Oct 27, 2017 at 06:36 PM 0
Share

In the end, this is what I went with. The obvious and simple. Since my code is already setting text, there is not much harm in making it a call to a new method, rather than overriding .text .

Honorable mention to @$$anonymous$$axGuernseyIII though for providing the technically correct anwer to "can you override/extend the text property" (option 2 in his answer below).

avatar image
1

Answer by FortisVenaliter · Sep 29, 2017 at 09:27 PM

Well, this has nothing to do with Unity really, just C#.

I'm not 100% sure it can be done, but if it can, it would be something like this:

  1. You'd define a new class deriving from Text (if Text is sealed, this won't work).

  2. You use the "new" keyword to redefine the 'text' property's getter and setter, including your code, then applying the values to "base.text". For example:

    public new String text { get { return base.text; } set { MyLogic(); base.text = value; } }

Comment
Add comment · Show 6 · 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 MacDx · Sep 29, 2017 at 09:38 PM 1
Share

The 'text' property from Text is virtual so you could very well override it and apparently Unity will let you use the new class ins$$anonymous$$d of the Text class if you want, however, I$$anonymous$$O it isn't worth it to do if you won't extend the class significantly. All that he wants is just calling his desired behaviour after modifying the value, he can achieve that with a simple Set method or a property like I suggested.

avatar image FortisVenaliter MacDx · Sep 29, 2017 at 09:40 PM 1
Share

Yeah, I tend to agree with you. The only problem is that the user could still write to the original text value through the inspector and bypass his logic. This way, it ensures it will always run. Unfortunately, a new Editor class will probably need to be written.

avatar image Bunny83 FortisVenaliter · Sep 29, 2017 at 10:58 PM 1
Share

This is actually irrelevant. The built-in setter code is also bypassed by the TextEditor.

Since the property is virtual the only correct way is to override it in a derived class. Using "new" would result in inconsistent behaviour if you have a "Text" reference to your derived class. Overriding ensures that the actual code of the derived class is executed no matter if you have a reference to the base class or the derived class.

Since the whole source code is open source you can also modify the source directly and recompile your own version. Though for compatibility it's better to create a derived class if you really need additional functionality.

Show more comments
Show more comments
avatar image
1

Answer by MaxGuernseyIII · Sep 29, 2017 at 10:18 PM

Okay. Here are two more options but both carry serious drawbacks. Their only advantage is that they allow you to sneak behavior into a mature system with a lot of coupling to Text.text.

Option 1: Observe and report

 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.UI;
 
 public class WatchTextAndRaiseEvent : MonoBehaviour
 {
   public Text ToWatch;
   public UnityEvent OnChanged;
   private string lastText;
 
   void Start()
   {
     lastText = ToWatch.text;
   }
 
   void Update()
   {
     var newText = ToWatch.text;
     if (newText != lastText)
       OnChanged.Invoke();
 
     lastText = newText;
   }
 }

Drawback: Imperfect timing. Depending on when Update is called, it will either raise the event in the same frame as the text change or the next one but it will never be in the call stack of the actual change. This could be obnoxious if you need to trace down why something isn't working the way you want.

Option 2: Override text property

 using UnityEditor;
 using UnityEngine.Events;
 using UnityEngine.UI;
 
 public class InterceptingText : Text
 {
   public UnityEvent OnChanged;
 
   public override string text
   {
     get
     {
       return base.text;
     }
     set
     {
       OnChanged.Invoke();
       base.text = value;
     }
   }
 }

Drawback: Coupling and effort to make it work in the editor. You're inviting yourself to couple even-more-heavily to something you don't control. In the case of certain framework-y things, like this property, it may be that you can get away with that but it's a bad habit... like bending down to tie your shoes at the bus stop. It's usually not going to be a problem but, when it is, it can be a big one.

Furthermore, I couldn't instantly determine a way to make OnChanged editable in the inspector. Text does something funky. I'm afraid you might have to make a custom editor that includes the behavior of the editor for text.

Like I said in my comment, if possible, go with @MacDx's or @FortisVenaliter's approach of "encapsulate and intercept" and go with these if you absolutely must.

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

397 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 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to display text on a cube and how to change it dynamically? 2 Answers

How to read the format "Key: Value" 1 Answer

OnTriggerEnter2D(collidertype coll) C# 1 Answer


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