Search Unity

Make parameters from inherited classes appear on the inspector

Discussion in 'Scripting' started by MatheusCohen, Jan 11, 2019.

  1. MatheusCohen

    MatheusCohen

    Joined:
    Aug 24, 2017
    Posts:
    57
    Hi,

    I apologize if this question has been answered, but i didn't know how to look for it properly...so:

    I have a class(card) that contains a list of another class(MyResources) that i can free assign in the inspector:

    upload_2019-1-11_20-20-35.png

    my question is, is there a way for me to create this list with classes that inherit from MyResources but have different values, and they still appear in the inspector as above?

    like the element 1 inherits from myResourceSpecial, so it will also appear another field such as a "isSpecial" bool.

    so it would end up like:
    Element 0:
    Rec - as above
    Value - as above
    Element 1:
    Rec - as above
    Value - as above
    Special - new Bool only for this or other elements that are myResourceSpecial



    i tried to play with just the inheritance a bit but couldn't figure out a way to achieve this...

    keep in mind that for my project it doesn't need to be a list of myResources(this was just a example), if i can assign 1 myResource to a single field and manage to create a dropdown with it's parameters it would be fine.

    i now i can do this with some custom editors(i know that i can, don't know how) as i'm reading through some finished projects files.

    Code (CSharp):
    1. public class Card : MonoBehaviour {
    2.     public int ID;
    3.  
    4.     public cardType CardType;
    5.  
    6.  
    7.     public List<myResources> ResourceValues;
    8.     public List<myResources> ResourceCosts;
    9. }
    Code (CSharp):
    1. [CreateAssetMenu(fileName = "Resource", menuName = "Scriptable/Resource")]
    2. public class ResourceBase : ScriptableObject
    3. {
    4.     public Color32 color;
    5. }
    6.  
    7. [System.Serializable]
    8. public class myResources
    9. {
    10.     public ResourceBase rec;
    11.     public int Value;
    12. }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You can inherit from some type that the Unity inspector handles via references, such as MonoBehaviour or ScriptableObject, and then you can assign references in the inspector instead of writing in the values directly.
     
  3. TeagansDad

    TeagansDad

    Joined:
    Nov 17, 2012
    Posts:
    957
    @MatheusCohen - If you add instances of a class that inherits from myResources, Unity will deserialize them as the base type, not the derived type. Unity's serialization model doesn't support inheritance for classes that are not derived from UnityEngine.Object.

    If you don't want myResources to inherit from ScriptableObject or MonoBehaviour, you can use an alternative serialization library. Odin Serializer is now open source, and I've found it to be very good for handling this sort of thing. It's fast and quite easy to use.