x


Possible to extend class derived from Editor?

Hi,

I have a script called Actions which other actions are extended from. Actions holds non-static variables that are common to all actions. What I would like to do is have an ActionsEditor class that will expose the common values in the editor for each action that extends from editor then extend this editor for other actions. My problem is I cant seem to find a way of not re-declaring the variables in the extended ActionsEditor. It kind of defeats the object if theres not a way. If I remove declarations from the extended ActionEditor it says the variables I am trying to access through target are not implemented on Action. It seems like the problem lies in the fact that the target has to be assigned in OnEnable. Can anybody think of any solutions?

Action : MonoBehaviour
SomeAction : Action - derives from action and inherits its values
ActionEditor : Editor - exposes actions' values in the inspector
SomeActionEditor: Editor - exposes its own values in the inspector aswell as Actions values

ActionsEditor

[CustomEditor(typeof(Action))]
public class ActionEditor : Editor
{
    public Action _target;
    protected Boolean _showTags = false;
    protected int _tagsArraySize;
    protected List<String> _tagsTemp;

    public virtual void OnEnable()
    {
        _target = (Action)target;
        _tagsTemp = _target.tags;
        _tagsArraySize = _target.tags.Count;
    }


    public override void OnInspectorGUI()
    {
        GUILayout.Label("INPUT");
        _target.useKeyboardButtonTrigger = EditorGUILayout.Toggle("Use Keyboard Trigger", _target.useKeyboardButtonTrigger);
        if (_target.useKeyboardButtonTrigger == true)
        {
            _target.keyboardButtonTrigger = EditorGUILayout.TextField("Keyboard Button Trigger", _target.keyboardButtonTrigger);
        }
        _target.useMouseButtonTrigger = EditorGUILayout.Toggle("Use Mouse Trigger", _target.useMouseButtonTrigger);
        if (_target.useMouseButtonTrigger == true)
        {
            _target.mouseButtonTrigger = EditorGUILayout.IntField("Mouse Button Trigger", _target.mouseButtonTrigger);
        }
        _target.useGestureTrigger = EditorGUILayout.Toggle("Use Gesture Trigger", _target.useGestureTrigger);
        if (_target.useGestureTrigger == true)
        {
        }
        GUILayout.Label("TARGET FINDING");
        _showTags = EditorGUILayout.Foldout(_showTags, "Target Tags");
        if (_showTags)
        {
            _tagsArraySize = EditorGUILayout.IntField("Tag Count", _tagsArraySize);
            Event e = Event.current;
            if (e.isKey == true && e.type == EventType.keyUp && e.keyCode == KeyCode.Return)
            {
                if (_tagsArraySize != _target.tags.Count)
                {
                    _tagsTemp = new List<String>(_tagsArraySize);
                    for (int tagIndex = 0; tagIndex < _tagsArraySize; tagIndex++)
                    {
                        _tagsTemp.Add("");
                    }

                    _target.tags = new List<String>(_tagsTemp);
                    EditorUtility.SetDirty(_target);
                }
            }

            for (int tagIndex = 0; tagIndex < _target.tags.Count; tagIndex++)
            {
                _target.tags[tagIndex] = EditorGUILayout.TagField("Target tag " + (tagIndex + 1).ToString() + ":", _target.tags[tagIndex]);
                EditorUtility.SetDirty(_target);
            }
        }

        GUILayout.Label("View Settings");
        _target.viewRestriction = (TargetFinding.ViewRestrictions)EditorGUILayout.EnumPopup("View Restrictions",
            _target.viewRestriction);
        if (_target.viewRestriction != TargetFinding.ViewRestrictions.InAndOut)
        {
            _target.targetFindingCam = (Camera)EditorGUILayout.ObjectField("Camera", _target.targetFindingCam, typeof(Camera));
            _target.debugCam = EditorGUILayout.Toggle("Debug Camera", _target.debugCam);
        }

        GUILayout.Label("Area Settings");
        _target.areaRestriction = (TargetFinding.AreaRestrictions)EditorGUILayout.EnumPopup("Area Restriction",
            _target.areaRestriction);
        switch (_target.areaRestriction)
        {
            case TargetFinding.AreaRestrictions.None:
                break;
            case TargetFinding.AreaRestrictions.Sphere:
                _target.sphereRadius = EditorGUILayout.Slider("Sphere Radius", _target.sphereRadius, 0.0f, 250.0f);
                _target.debugSphere = EditorGUILayout.Toggle("Debug Sphere", _target.debugSphere);
                break;
            case TargetFinding.AreaRestrictions.Cone:
                _target.coneDistance = EditorGUILayout.Slider("Cone Distance", _target.coneDistance, 0.0f, 250.0f);
                _target.coneViewAngle = EditorGUILayout.Slider("Cone View Angle", _target.coneViewAngle, 1.0f, 179.99f);
                _target.debugCone = EditorGUILayout.Toggle("Debug Cone", _target.debugCone);
                break;
            case TargetFinding.AreaRestrictions.InFront:
                break;
            case TargetFinding.AreaRestrictions.Behind:
                break;
        }
        GUILayout.Label("GUI");
        _target.isHidden = EditorGUILayout.Toggle("Hidden", _target.isHidden);
        _target.isEnabled = EditorGUILayout.Toggle("Enabled", _target.isEnabled);
        GUILayout.Label("ACTION SETTINGS");
        _target.coolDownTime = EditorGUILayout.FloatField("Cool Down Time", _target.coolDownTime);
        _target.globalCoolDownTime = EditorGUILayout.FloatField("Global Cool Down Time", _target.globalCoolDownTime);
        //DrawDefaultInspector();
    }
}

DerivedActionEditor

[CustomEditor(typeof(DropAction))] public class DropActionEditor : ActionEditor { public DropAction _target; protected Boolean _showTags = false; protected int _tagsArraySize; protected List _tagsTemp;

public override void OnEnable()
{
    base.OnEnable();

    _target = (DropAction)target;
    _tagsTemp = _target.tags;
    _tagsArraySize = _target.tags.Count;
}


public override void OnInspectorGUI()
{
    base.OnInspectorGUI();

    _target.peakHeight = EditorGUILayout.Slider("Peak Height", _target.peakHeight, 0.0f, 50.0f);
    _target.peakTime = EditorGUILayout.Slider("Peak Time", _target.peakTime, 0.0f, 50.0f);
    _target.rotationAngle = EditorGUILayout.Slider("Rotation Angle", _target.rotationAngle, 0.0f, 359.99f);

    //DrawDefaultInspector();
}

}

more ▼

asked Jul 15 '11 at 04:16 PM

Blankzz gravatar image

Blankzz
246 19 20 23

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1675
x467
x22

asked: Jul 15 '11 at 04:16 PM

Seen: 652 times

Last Updated: Jul 15 '11 at 04:16 PM