- Home /
Restrict which types of an Enum can be selected in inspector.
If you have an exposed scrip variable in the inspector that has an enum as its type, Unity provides a nice little drop down menu to let you choose from the types contained in the enum.
Is there a way to control which of those are visible/a user can select?
Answer by aka3eka · Sep 28, 2021 at 03:30 PM
This might be old but it still might be helpful for somebody. Use InspectorName attribute with empty string:
enum MyEnum
{
[InspectorName(null)]
HiddenValue,
Value1,
Value2
}
Answer by KazYamof · Nov 26, 2018 at 11:45 AM
Just ressurecting the post in case of someone needs: I've made a slightly different approach.
First, I've created a custom enum with the values I wanted to show.
Then, each value of my custom enum receites the value of the Unity Enum type I want to set. Then, I only need to cast to the Unity Enum Type to set the value to object.
See the example with an Input Field
//That's my custom enum. Only exposes 2 types of numeric values
public enum CustomInputType
{
Integer = InputField.ContentType.IntegerNumber,
Decimal = InputField.ContentType.DecimalNumber
}
//That's the cast I do to set the content type on my InputField
this.inputField.contentType = (InputField.ContentType) this.CustomInputType;
Answer by roojerry · Jun 18, 2013 at 06:55 PM
I would look into creating a custom property drawer that ignores specified values.
Having a bit of trouble figuring out how I'd go about telling it what values of the enum to ignore, how do I access the inside of the enum like that? I've used property drawers before, but I don't have a great grasp of them.
I feel like the answer will have to do with some sort of mask (given the nature of enums), but I'm really at a loss.
Answer by Salim · Aug 16, 2018 at 11:06 PM
Why not create 2 Enum, one public that only have the values you want exposed in the inspector, and another one private that you use in your script. At Start() you can changes the private version based what is in the public instance with IF statements.
Answer by Thibaut_M · Sep 30, 2021 at 12:28 PM
Use the attribute [UnityEngine.HideInInspector] above the entry of your enum you want to hide. ,Use the attribute [UnityEngine.HideInInspector]
above the entry you want to hide.
AFAIK this is only allowed for fields and doesn't help much with enum values.