简体   繁体   中英

Is it possible to show a Partial enum in a custom inspector in Unity?

Is there a way to only have certain enum values available in the inspector? For example I have an enum full of objects, and if I select table I want a second enum with specific object ids to only show table1/table2/table3 instead of all the available objects.

public enum Objects
{
    Chair,
    Table,
    Door
}

public enum ObjectIDs
{
    Chair01, 
    Chair02,
    Table01,
    Table02,
    Table03,
    etc..
}

There is a very robust plugin which makes that kind of things very easy.

https://odininspector.com/attributes/show-if-attribute

you can modify this code to make what you want.

You need to change the EnumOrderDrawer class to make the loops not go for all the enum variables.

For example change the code

public const string TypeOrder = "10,1,5,2";
public enum Type 
    {
        One = 10,
        Two = 1,
        Three = 5,
        Four = 2,
    }

    [EnumOrder(TypeOrder)]
    public Type type3;
.
.
.
.
for (int i=0; i<property.enumNames.Length; i++) 
        {
             items[i] = property.enumNames[indexArray[i]];
        }

to

public const int[] TypeOrder = new int[] { 10, 1, 5, 2 };
public enum Type 
    {
        One = 10,
        Two = 1,
        Three = 5,
        Four = 2,
    }

    [EnumOrder(TypeOrder)]
    public Type type3;
.
.
.
.
for (int i=0; i<TypeOrder.Length; i++) 
        {
             items[i] = property.enumNames[indexArray[i]];
        }
.
.
.
.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM