Search Unity

Question can I make an array of classes and have it list like an int array?

Discussion in 'Scripting' started by Not_Sure, Jan 11, 2024.

  1. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    Silly question, but when you make an int array you get a neat little tab in the inspector that allows you to add int's and edit them.

    Is that possible to do with a whole class?

    Like if I wanted to make an RPG and make stats/skills into classes and have each class cover the name, the level, the experience in that stat, etc...
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,861
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    Not out of the box, no, but you could make a "ClassReference" class, and a custom editor for those. However, you would have to decide how to display them, how to search for them, and how to ensure you aren't just referring to any old random class but only ones that comply (say, by interface or base class). They'd likely be serialized by their name, and what happens if you rename a class after your ClassReference contains an older name? Lots of things to consider there.

    Edit: It looks like spiney199 is talking about serializing whole instances of a class, while I am talking about whole classes. It's always important to distinguish between the two.
     
    Bunny83, Ryiah and Not_Sure like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Ryiah and Not_Sure like this.
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,861
    I mean you're kind of describing
    [SerializeReference]
    : https://docs.unity3d.com/ScriptReference/SerializeReference.html

    As in if they wanted a base class with derived types, then SerializeReference is required to handle the polymorphic serialisation.

    But they would need a custom inspector as Unity doesn't have built in support for this (as far as selecting sub-types, but it can render fields that already have an reference).
     
  6. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    Sorry, instances.
     
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    473
  8. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    No, I meant a Unity wrapper for System.Type along the lines of this:

    Code (CSharp):
    1. [Serializable] public class ClassReference
    2. {
    3.     public string fullyQualifiedTypeName;
    4.     private System.Type type;
    5.     private System.Type mustInherit;
    6.     //...
    7. }
    with all the custom drawer code that would let you browse. Actually making instances of the type would be left as an exercise for the reader. Then you can have a list of "skill classes" with no instances, and when you earn the skill you instantiate state for it in the appropriate manner.
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,861
    That's definitely an idea. Though,
    [SerializeReference]
    can serialize from a
    System.Object
    type field, so I think that would be a substantially less flaky approach.

    2023 and beyond also allows SerializeReference on generic types. So this would make for something that would actually workout of the box with no editor work required:
    Code (CSharp):
    1. [System.Serializable]
    2. public class ClassReference<T> where T : class, new()
    3. {
    4.     [SerializeReference]
    5.     private T _reference = new T();
    6.  
    7.     public T Reference => _reference;
    8. }
     
    CodeRonnie likes this.
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,861
    In hind sight the above code is superfluous unless you had a specific reason to have a generalised wrapper object.

    Otherwise you can just serialise T directly.