A
A
Almost2020-07-10 15:51:39
Unity
Almost, 2020-07-10 15:51:39

How to display all fields of inherited classes using Odin Inspector in Unity?

Hello.

There is such a simple example:

using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Examples
{
  [CreateAssetMenu(fileName = "Enemies", menuName = "Enemies", order = 0)]
  public class Enemies : ScriptableObject
  {
    public List<Enemy> list = new List<Enemy>();
    
    [Serializable]
    public class Enemy
    {
      public int id;
    }

    [Serializable]
    public class Dragon : Enemy
    {
      public string name;
    }
    
    [Serializable]
    public class Robot : Enemy
    {
      public double damage;
    }
    
    [Button]
    public void AddRobot()
    {
      list.Add(new Robot());
    }
    
    [Button]
    public void AddDragon()
    {
      list.Add(new Dragon());
    }
  }
}


Items added to the list only display class ids.
How can I display the fields of inherited classes (robot damage and dragon name)?
Alternatively, you can add the [SerializeReference] attribute to the list and it will display correctly (without the Odin Inspector).
But if Odin Inspector is used (an extension that allows you to customize the display of objects in the inspector in a very diverse way), then the fields are not displayed. I would like to see the display in the style of Odin, so that it is possible to customize the editor.

5f086364de677559026136.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question