- Home /
Custom Editor for Interface/Child classes not displaying
Hello everyone! I am currently trying to make a custom editor that all of my AI types will see. I have an AI interface called IAI which several classes derive from. Now, I did try setting the "bool editorForChildClasses to true but this did not work either. The inspector for all of my AI types remained unchanged. My child classes are Monobehaviour classes as well.
Now, I do know that interfaces are generally a big pain in the butt on Unity, so if that is the case I might have to try something else. Any help on getting an editor that would work for all of my AI types would be fantastic.
Thanks everyone!
My AI Interface:
public interface IAI
{
ReadOnlyCollection<AIStates.IAIState> AIStateCollection
{
get;
}
float MaxSpeed
{
get;
}
Transform PathingTarget
{
get; set;
}
Pathfinding.TargetMover TargetMover
{
get; set;
}
AILerp AILerp
{
get; set;
}
AIStates.IAIState CurrentAIState
{
get; set;
}
AITypes AIType
{
get;
}
void SetNewAIState(AIStates.AIStates state);
}
My Editor:
[CustomEditor(typeof(AI.AITypes.IAI), true)]
public class AIEditor : Editor
{
private SerializedObject m_Object;
public void OnEnable()
{
m_Object = new SerializedObject(target);
}
public override void OnInspectorGUI()
{
Rect textBox = GUILayoutUtility.GetRect(0.0f, 35.0f, GUILayout.ExpandWidth(true));
GUI.Box(textBox, "The Inspector will enable you to edit individual states.");
}
}
Example child class
public class Chef : MonoBehaviour, IAI
{
// Code here
}
Answer by Adam-Mechtley · Sep 24, 2016 at 06:50 PM
Hi @Spartiate0033! The problem is that the CustomEditor attribute only works with, MonoBehaviour- or ScriptableObject-derived classes. You cannot specify an interface or an open generic type. You would need to have something like:
class AIBehaviour : MonoBehaviour, AI.AITypes.IAI
{
// your code here
}
and correspondingly,
[CustomEditor(typeof(AIBehaviour), true)]
class AIEditor : Editor
{
// your code here
}
Hey! Thanks for the response. Unfortunately this does not work (I realize now I did not post a child class so I'll edit my question).
Right now, this is a child class
public class Chef : $$anonymous$$onoBehaviour, IAI
{
// Code
}
So I am wondering if it has to do with the IAI being an interface because I know interfaces and Unity's editor don't often mix well.
Sorry if it got missed. The relevant point I was trying to make is that you cannot do this:
[CustomEditor(typeof(AI.AITypes.IAI))]
You could only do this:
[CustomEditor(typeof(Chef))]
As such, depending what you need your class hierarchy to look like, it may be easiest to have:
public abstract class AIBehaviour : $$anonymous$$onoBehaviour, AI.AITypes.IAI {}
public class Chef : AIBehaviour {}
I figured as much. I'll go back to the drawing board and see what I can do about making my AI editor. Thank you very much! I marked this as the answer.
Answer by OndrejP · Feb 14, 2018 at 10:52 PM
For displaying additional info for my interfaces, I'm using following code.
It's not ideal, but works well when I don't want to create separate abstract class for each interface combination.
public class MyBehaviour : MonoBehaviour, IMyInterface
{
public float SomeValue;
protected MyInterfaceInspectorInfo m_info;
}
[Serializable]
public struct MyInterfaceInspectorInfo
{
}
[CustomPropertyDrawer(typeof(MyInterfaceInspectorInfo))]
public class MyInterfaceInspectorInfoDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var target = property.serializedObject.targetObject as IMyInterface;
// TODO: Your drawing code here
}
}
Your answer
Follow this Question
Related Questions
Empty space at the top of a custom property drawer layout? 2 Answers
Custom inspector for AnimatorStateTransition 0 Answers
Project view thumbnails (Prefabs/Materials/etc...) 0 Answers
CustomAttribute on Class or Method ( EDIT: Button to call methods) 2 Answers
Unity Custom Editor Texture Warning 2 Answers