- Home /
Override TextEditor
Hi,
I created a class derived from Text (UI namespace). For this, wold like to create a custom editor, but leave most of the original editor intact.
according to: https://docs.unity3d.com/ScriptReference/UI.TextEditor.html
"Extend this class to write a custom editor for an Text-derived component."
The problems is, however: if I do this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
[CustomEditor(typeof(StyledText))]
[CanEditMultipleObjects]
public class StyledTextEditor : TextEditor {
public override void OnInspectorGUI()
{
GUILayout.Label("bla");
}
}
I get a "no suitable method to override"
I can change it to derive only from "Editor", but then all the text related controls are gone.
Is there some sample code on how to make your own TextEditor? this seems odd, because "TextEditor", being derived from "Editor" should have the OnInspectorGUI.
Answer by hexagonius · Jul 28, 2018 at 03:57 PM
you're right, a class that can be overriden should provide it's overridable counterpart or be sealed.
I once tried the same thing and came up with just copy and pasting the TextEditor source code you run my own editor. Out can be found here:
https://bitbucket.org/Unity-Technologies/ui/src/a3f89d5f7d145e4b6fa11cf9f2de768fea2c500f/UnityEditor.UI/UI/TextEditor.cs?at=2017.3&fileviewer=file-view-default. fun fact: according to bitbucket, it should work, because the class is neither sealed, nor is the method :/
Answer by Bunny83 · Jul 29, 2018 at 07:17 PM
There is a TextEditor class inside the UnityEngine namespace (which is a class used by the IMGUI system for input fields) as well as in the UnityEditor.UI namespace. You import the UnityEngine namespace and therefore whenever you use TextEditor you will be using the wrong one. Keep in mind that when you import both namespaces (UnityEngine and UnityEditor.UI) you get an ambiguous reference error as the compile does not know which class you want to use. You have to use the full class name like this:
public class StyledTextEditor : UnityEditor.UI.TextEditor
{
Keep in mind when you override a method you completely replace it. So your example code just shows your label, nothing else. You may want to call base.OnInspectorGUI()
inside your overridden method. However you may want to have a look at the actual implementation. Note that the UI system is released under a permissive licence
I've actually written this answer yesterday but missed sending it out ^^. Just stumbled across the tab again ^^.