Search Unity

Can I add an abstract property to Unity's Inspector?

Discussion in 'Scripting' started by highlyinteractive, Aug 14, 2014.

Thread Status:
Not open for further replies.
  1. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    116
    I have a base class, for the sake of simplicity something like this:

    Code (CSharp):
    1. public abstract class Base : MonoBehaviour
    2. {
    3.     public abstract Rect MyRect { get; set; }
    4.  
    5.     void OnGUI ()
    6.     {
    7.        GUI.Box(MyRect, "Hello");
    8.     }
    9. }
    Which I am extending like this:

    Code (CSharp):
    1. public class RectangleOne : Base
    2. {
    3.     public override Rect MyRect {
    4.       get {
    5.          return new Rect(0, 0, 100, 100);
    6.       }
    7.       set {
    8.          MyRect = value;
    9.       }
    10.     }
    11. }
    I would like to be able to set RectangleOne.MyRect in the Inspector

    Is this possible?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
  3. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    116
    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 ;) )
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
  5. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
  6. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    116
    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)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(Base), true)]
    6. public class BaseInspector : Editor
    7. {
    8.     public override void OnInspectorGUI()
    9.     {
    10.         Base myTarget = (Base) target;
    11.        
    12.         myTarget.MyRect = EditorGUILayout.RectField("MyRect", myTarget.MyRect);
    13.     }
    14. }
    This is in 4.5.2 so I'm going to update & try again
     
  7. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    116
    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.
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Does it still crash if Base is not abstract?
     
  9. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    116
    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
     
Thread Status:
Not open for further replies.