Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question [Solved] Custom inspector for class that inherited from Toggle class.

Discussion in 'Scripting' started by Toficor, Feb 21, 2023.

  1. Toficor

    Toficor

    Joined:
    May 6, 2015
    Posts:
    33
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,882
    The
    UnityEditor.Editor
    (or whatever internal System unity uses for build in types) for the toggle class will be overriding the inspector for your derived type, most likely.

    As you've noted you need to make a custom inspector for your derived type. You might be able to use
    UnityEditor.Editor.CreateEditor
    to create and draw the default editor for the Toggle class, and then draw your fields before/after it.

    But it's probably easier to take advantage of Unity's component structure and just make a component that hooks into the Toggle component instead.
     
    Toficor likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,527
    Well, almost all (if not all) UI components do have their own custom editors which are automatically used for derived classes. However since those custom editors only show fields that are implemented in the class they were written for, when you add more fields they would not show up automatically. When you implement your own custom inspector (which you can do) you would loose any of specific functionality that Unity implemented in its own custom editor.

    Fortunately the ToggleEditor class is also available and you can also derive your own custom editor from
    UnityEditor.UI.ToggleEditor
    . Your custom editor would simply override the OnInspectorGUI callback, call base implementation and just add your relevant fields afterwards.

    Since you kept your question very generic:
    it wouldn't make any sense to provide an example that doesn't actually fit your usecase. Keep in mind that the source of uGUI and its Toggle class is available on github. the same is true for the ToggleEditor class. Though you should not modify or copy any of that code. If you really need to extend those classes, create a subclass. The source code can help to better understand the innerworkings of the class and can help to decide which methods you may want to override / extend.
     
    spiney199 likes this.
  4. Toficor

    Toficor

    Joined:
    May 6, 2015
    Posts:
    33

    Thank you so much for links to github and explanation, that is what I was to looking for.