- Home /
Custom Inspectors for generics and tweaking default inspectors
So, here's basically the gist of what I'm trying to do. I'm trying to change the way List<T>
is displayed in the Inspector view. Currently, List<T>
expands to show Size and then Element 0, Element 1, etc. Then, if you expand one of the Elements, it shows the default inspector for whatever type T is.
Now, let's use a trivial/arbitrary example and say that I wanted to change those headers to say "Item 0" instead of "Element 0" whenever I inspected a List<T>
. Could I easily do that as a Custom Inspector? If so, how?
It seems to me like there are two potential problems here:
1) Associating a Unity inspector with a generic type.
[CustomEditor(typeof(List<T>))]
?
Maybe, this just magically works. I'm not at a place where I can try it and all the examples I've found use non-generic types.
2) Displaying a partial default inspector.
So, assuming that I could get past the first part and associate an Inspector with a generic, then in the OnInspectorGUI I would want to create a line saying "Item 0" and then display the default T inspector when that line is expanded. I only want to make one Inspector class, so being able to draw the default inspector for any arbitrary type is a necessity.
Is any of this possible?
Answer by RC-1290 · Apr 24, 2012 at 09:51 AM
You can use the CustomEditor for a component, not for for an arbitrary type. The code you write in the inspector then decides the way the data of that component is displayed. So if you want to combine existing behavior with new behavior, you have to mix it yourself.
Would this work for you?
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(Cake))]
public class CakeInspector : Editor {
public override void OnInspectorGUI ()
{
Cake c = target as Cake;
System.Type t = c.ThatGenericList.GetType();
if (t == typeof(SpecificType)){
// Custom Inspector code here
}
else {
base.OnInspectorGUI();// DEFAULT GUI
}
}
}
Your answer
Follow this Question
Related Questions
How do I change inspector values of other game objects using a custom Editor Window 0 Answers
Inspector top not working properly 0 Answers
How do I change inspector values of other game objects using a custom Editor Window 0 Answers
Inspector: custom property with custom type use default editor 3 Answers
How do I change inspector values of other game objects using a custom Editor Window 0 Answers