1

Let's say I have this setup

public class SomeClassMadeByUnity
{
    public int someUnneededVariable;
    public UnityEvent someEvent { get; set; }
}

public class MyChild : SomeClassMadeByUnity
{
    
}

SomeClassMadeByUnity has a Custom Editor under closed source, but I want to expose someEvent in a Custom Editor myself.

How would I go about this?

3
  • Does it need to be a property? Properties are not serialized by Unity Commented Jan 4, 2021 at 13:47
  • As the class is provided by Unity (XRBaseInteractable btw) there's really not much I can do. Commented Jan 4, 2021 at 14:20
  • 1
    Then we probably can't do much from here either. For a custom editor you need to at least know the field names (not the public property, and not the one displayed in the Inspector but the actual field names). In case they are private you could of course brute force until you have the correct name but that's not something we can help here ;) Commented Jan 4, 2021 at 14:46

2 Answers 2

0

There is not really a need/use for a custom editor here.

The "issue" is that properties are not serialized by Unity. So a custom editor wouldn't help much. Even if you somehow manage to expose such a field in the Inspector any changes in it wouldn't get stored anyway

All you need actually would be a serialzed backing field like

[Serializable]
public class SomeClass
{
    public int someUnneededVariable;

    // You probably wouldn't need the setter anyway 
    public UnityEvent someEvent { get => _someEvent; set => _someEvent = value; }

    [SerializeField] private UnityEvent _someEvent;
}

[Serializable]
public class MyChild : SomeClass
{
    
}

Or if you already have a custom editor for the parent type you could simply make sure that you pass in true in CustomEditor

editorForChildClasses

If true, child classes of inspectedType will also show this editor. Defaults to false.

Sign up to request clarification or add additional context in comments.

2 Comments

Unity provides a custom editor for this class (XRBaseInteractable), but I don't want to show every property on the inspector, as I don't need most of them.
Maybe you should rather post your actual use case ..
-1

You can write a custom editor for your MyChild class instead, extending custom editor of SomeClassMadeByUnity. But that if custom editor of SomeClassMadeByUnity is exposed as public. So let's say SomeClassMadeByUnityEditor is editor script for SomeClassMadeByUnity, than you could do something like:

[CustomEditor(typeof(MyChild))]
public class MyChildEditor : SomeClassMadeByUnityEditor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();    // To execute OnInspectorGUI logic of parent class.
        // Do some exposing SomeEvent logic.
    }
    
    // Or if parent class uses CreateInspectorGUI instead
    public override VisualElement CreateInspectorGUI()
    {
        var root = base.CreateInspectorGUI();
        // Do some exposing SomeEvent logic.
        return root;
    }
}

2 Comments

If I understand that correctly this still would show every property of SomeClassMadeByUnity and I don't need the majority of those.
@MarekLegris Ah, I probably didn't get correctly your intention. So you can just inherit your MyChildEditor from Editor as you would normally make a custom editor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.