- Home /
Custom editor for Text / Button for externalising texts
We're externalising all texts in our game, and want an easy and efficient way to set it all via the GUI. Basically when anyone creates a button, or label, or anything with text in, we want them to be able to put in the text ID of that component, "TEXT_ID_OK" for instance, and the text associated with that ID show up on it.
I've tried the below script in "ButtonEditor.cs" in the Assets/Editor, but it's done nothing at all, same with a near identical "TextEditor.cs" file. The Inspector editor stays exactly the same with no change.
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
[CustomEditor(typeof(Button))]
public class ButtonEditor : Editor
{
public override void OnInspectorGUI()
{
Button button = (Button)target;
Text text = button.GetComponentsInChildren<Text>()[0];
text.text = GetTextFromId(EditorGUILayout.TextField("Button TextID", text.text));
DrawDefaultInspector();
}
}
Can anyone point out what I'm doing wrong? Can you not set custom editors for default components? Am I referencing the incorrect components? When I reference "CustomButton" (which does nothing but inherit from Button) instead of just "Button" it works as expected, but but nothing happens when just "Button" or "Text".
Answer by Nik-Space · Jan 30, 2015 at 05:36 PM
I know this quastion is old but ...
I faced the same problem and i did some digging on the UnityEditor.UI and i find out that this is not possible to be done that easy.
At least not like that [CustomEditor(typeof(Button))]
Because the new UI Inspectors are based form multiple classes.
What do i mean with that. Lets see Tranform for example. the Class "Tranform" has as Editor Inspector the Class "TransformInspector" and that has as base the Editor.
So the we can use that:
[CustomEditor(typeof(Transform))]
public class TransformCustomInspector: Editor
But on the new UI is more complex. The Class that is called as Inspector for the "Text" is named "TextEditor" and has as base the "GraphicEditor"
and at the OnInspectorGUI it calls the "PropertyField"s of a "Text" and a "FontData"
And in the FontDataDrawer i find all the Editor GUI is on the Default Inspector of a Text. Without the textarea that is on the top.
I try this then
[CustomEditor(typeof(Text), true)]
public class TextInspector : UnityEditor.UI.TextEditor
But still i get nothing.
I realy can't find an anwser to that too.
So i think the best way is to make a "CustomButton" Class as you said.
Your answer
Follow this Question
Related Questions
Changing Text Based off of a Prefab 0 Answers
Edit Default Text Mesh Pro Component 0 Answers
gui text button dont work 1 Answer
GUI Button appears when Paused 1 Answer