Modify Built-In Class Variables Get/Set functions
Hello,
I was wondering whether it might be possible to change the Get/Set functions of variables of built-in unity classes. More precisely I want to change the get/set properties of Text components to easily implement language localisation.
If I go to the declaration of the Text component I get the following:
using System;
namespace UnityEngine.UI
{
[AddComponentMenu ("UI/Text", 10)]
public class Text : MaskableGraphic, ILayoutElement
{
public virtual string text {
get;
set;
}
So the way I see it, I should be able to modify the get part to smth like this:
get{LocalizationScript.Localize(value)}
But practically Visual Studio does not let me edit those variables. Therefore I was wondering if someone has any suggestions on how to do that (I would like to avoid Extension Methods).
Thanks, Fred
Answer by Vicarian · Jul 05, 2018 at 10:59 PM
Since the property is virtual, you can override it in a class that derives Text:
using UnityEngine.UI;
public class LocalizedText : Text {
public override string text {
get { return LocalizationScript.Localize(base.text); }
set { base.text = value; }
}
Yes I tried that, but from my understanding that would mean that I have to replace every existing Text component by a new LocalizedText (which I would prefer to avoid) or do I have that wrong?
It wouldn't take much time. Just search the Hierarchy for Text, remove component and add the LocalizedText component.
I just have to port all the properties over to the new text. $$anonymous$$y own fault for not thinking about that in advance. But thanks :)