Search code examples
c#unity-game-engineenumsunity-editor

Updating an Enum for a ScriptableObject based on another enum selection?


Basically, I am creating a ScriptableObject Skill and I want to restrict it to only certain character types. However, I can't quite figure out how to go about doing it. I want to avoid having a single enum for all the classes unless it is unavoidable. I will try and show you what I mean below.

public class Skills : ScriptableObject {

public string Description;
public Sprite Thumbnail;
public int LevelNeeded;
public int XPNeeded;

ClassRestrictions classRestrictions;

}

public enum ClassRestrictions
{ None, Warrior, Rogue, Mage };

If in the editor I select Warrior I would want another enum to appear and ask about if I want to restrict it to a certain subtype. Something like this:

public enum WarriorSubClassRestrictions
{None, Barbarian, Knight, Paladin};

However, if I selected the Rogue type I wouldn't want the Warrior Enum to appear. Is there a way to do this in the Editor inside of my ScriptableObject class?


Solution

  • Hmm. The only way I can think of doing this is something along these lines:

    public interface IClassRestriction<T> {
        public T subclass { get; set; }
    }
    
    public class ClassRestrictionWarrior : IClassRestriction<WarriorSubClassRestrictions>{
        public enum WarriorSubClassRestrictions {
            None, Barbarian, Knight, Paladin
        }
    }
    

    By declaring the subclass property as a generic and defining that generic by the implementation, that should work.