Custom enumeration class accessible through inspector?
Hi! I am working on my custom enumeration class, I think it looks beautiful and most of all in my opinion it's a good OOP solution. Any ideas how could I make its values (sword, exampleValue, troll) public CustomEnumeration dropDownList;
accessible through inspector?
using System.Collections.Generic;
using System.Collections.ObjectModel;
//Example
public class CustomEnumeration
{
//Attribute
public readonly Vector3 Position;
public readonly string Word;
//Easy foreach imitation
protected static readonly List<CustomEnumeration> _enumeration = new List<CustomEnumeration>();
public static ReadOnlyCollection<CustomEnumeration> Enumeration
{
get
{
return _enumeration.AsReadOnly();
}
}
//"Enums"
public static readonly CustomEnumeration sword = new CustomEnumeration(new Vector3(0,0,0), "Sw0rd");
public static readonly CustomEnumeration exampleValue = new CustomEnumeration(new Vector3(1, 2, 3), "example");
public static readonly CustomEnumeration troll = new CustomEnumeration(new Vector3(3, 2, 1), "troll");
//PROTECTED constructor - only here (in this script) we can create enum values
protected CustomEnumeration(Vector3 position, string word)
{
this.Position = position;
this.Word = word;
_enumeration.Add(this);
}
public void DoSth()
{
//Instead of switch statements we can expand our enumeration class by creating methods
//So OOP
//Wow
//Such CleanCode
//Doge is dead meme like Harambe
}
}
I'd like you to be more specific in your question, but I'll try to help you anyway.
I'm not sure what you mean by "accessible" since the values you are talking about are static and read-only, but you could write a PropertyDrawer for your class: https://docs.unity3d.com/$$anonymous$$anual/editor-PropertyDrawers.html
With a PropertyDrawer, you could show the values of the CustomEnumerator when it is shown in the inspector of another script.
Answer by LucianoMacaDonati · Nov 13, 2016 at 07:17 PM
Hello ShivekGG,
I believe you want to use something like SerializeField.
Example:
public enum MyWeapons
{
Knives, Rope, Dagger, Chains, Rocks, Laser_beams, Acid, Body_bag
}
[SerializeField]
public MyWeapons weaponEnum;
If you're making your own custom classes you might want to take a look at this too.
I took a second look at your code and you will want to add [System.Serializable] at the top of your class. Look at the last link I showed you.
Your answer
![](https://koobas.hobune.stream/wayback/20220612100604im_/https://answers.unity.com/themes/thub/images/avi.jpg)