I have a base class, for the sake of simplicity something like this:
public abstract class Base : MonoBehaviour
{
public abstract Rect MyRect { get; set; }
void OnGUI ()
{
GUI.Box(MyRect, "Hello");
}
}
Which I am extending like this:
public class RectangleOne : Base
{
public override Rect MyRect {
get {
return new Rect(0, 0, 100, 100);
}
set {
MyRect = value;
}
}
}
I would like to be able to set RectangleOne.MyRect in the Inspector
Is this possible?
You can’t add a property, abstract or otherwise. They don’t show up in the default inspector. If you want a property, you need to write a custom inspector.
OK, but I can’t write a custom inspector for the Base class can I?
Would I be correct in thinking that I’d need to write a custom inspector for each class that extends Base?
(hint: please say no
)
I believe that writing an inspector for the Base will “inherit” to its derivative classes. You might have to write special logic to handle the derived classes, though calling DrawDefaultInspector ought to include all the properties from the derived class as well.
Worst case, you can write definitely write inspectors for each derived class and have them call a common set of static functions to handle the heavy lifting of the inspector.
When writing an inspector you can flag if it should draw the inspector for children objects too so yes you can have an inspector for abstract classes.
Well, thanks for the advice so far. Unfortunately it didn’t have the desired effect in that my custom editor script causes Unity to crash!
Here’s the script if you want to try it (used with the two above)
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(Base), true)]
public class BaseInspector : Editor
{
public override void OnInspectorGUI()
{
Base myTarget = (Base) target;
myTarget.MyRect = EditorGUILayout.RectField("MyRect", myTarget.MyRect);
}
}
This is in 4.5.2 so I’m going to update & try again
Nope, that’s a full on crash in 4.5.3 as well - only happens when ‘editorForChildClasses’ is set to true, but that’s exactly what I want.
Have submitted a bug report.
Does it still crash if Base is not abstract?
No it doesn’t
It seems to be the MyRect property in Base that is causing the crash. Marking it as abstract or virtual kills Unity