- Home /
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();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612144458im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change position using custom and native editor 1 Answer
Create inspector drop-down button based on the content of a list in editor mode 1 Answer
Assign texture to shader bypassing material (in Unity Editor) 1 Answer
Get if inspector field labels are on separate line 1 Answer
Unity inspector scripting - scriptable objects - card game 0 Answers