Question by
Positive7 · Mar 03, 2018 at 11:56 AM ·
custom editor
Recttransform CustomEditor extension
Hi,
Since Text component can't be easily extended I decided to add a Button to Recttransform instead. The script works fine for a while, but sometimes it floods with Debug errors :
NullReferenceException: (null)
UnityEditor.SerializedObject..ctor (UnityEngine.Object[] objs, UnityEngine.Object context) (at C:/buildslave/unity/build/artifacts/generated/common/editor/SerializedPropertyBindings.gen.cs:87)
UnityEditor.Editor.GetSerializedObjectInternal () (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorBindings.gen.cs:193)
UnityEditor.Editor.get_serializedObject () (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorBindings.gen.cs:185)
UnityEditor.RectTransformEditor.OnEnable () (at C:/buildslave/unity/build/Editor/Mono/Inspector/RectTransformEditor.cs:100)
Restarting Unity solves the problem, but maybe someone can point me out what could be wrong with this script :
namespace Editor
{
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
[CustomEditor(typeof(RectTransform))]
public class RectTransformTest : Editor
{
private Editor editorInstance;
private void OnEnable()
{
var assembly = Assembly.GetAssembly(typeof(Editor));
if (assembly == null) return;
var rtEditor = assembly.GetType("UnityEditor.RectTransformEditor");
if (rtEditor == null) return;
if (target != null) editorInstance = CreateEditor(target, rtEditor);
}
public override void OnInspectorGUI()
{
if (editorInstance != null) editorInstance.OnInspectorGUI();
if (target == null) return;
var t = target as RectTransform;
if (t == null || !t.GetComponent<Text>()) return;
if (!GUILayout.Button("Color Uppercase")) return;
Color color;
t.GetComponent<Text>().text = t.GetComponent<Text>().text.ColorUppercase("FFFFFF");
if (ColorUtility.TryParseHtmlString("#FFA300FF", out color))
{
t.GetComponent<Text>().color = color;
}
if (t.GetComponent<Outline>() == null) t.gameObject.AddComponent<Outline>();
SceneView.RepaintAll();
}
}
}
Comment
Answer by Fugo_Games · Sep 22, 2019 at 09:16 PM
If you use Editor.CreateCachedEditor method while creating your 'static' editor instance (instead of Editor.CreateEditor for a non-static editor), these exceptions disappear, also use DestroyImmediate at OnDisable for your static editor.
Your answer