How can I make an enum appear in a slot in the inspector?

Hello.
How can I make an enum appear in a slot in the inspector?

This is probably the same question as below.

However, I am using CocosCreator 3.8.2 and could not use the above code because of the different version.
var EnGameState;" cannot be declared because it causes a “parsing error” error, so the subsequent “cc.Class” cannot be used either.

The script file I created is below.

import { _decorator, Component, Node, Enum } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('EnumTest')
export class EnumTest extends Component {
  Pet = Enum({
    non: 1,
    dog: 2,
    cat: 3,
  });

  start() {}

  update(deltaTime: number) {}
}

I want to attach this class to a node and change the variables declared for Pet from the inspector.

Hi, that is an old version of the enum implementation.

Use this for CC 3.x :

import { Enum } from 'cc';

export enum EnButtonColor{
  Blue,
  Orange,
  Red,
  Green,
  Pink,
  Purple
}

@property({type:Enum(EnButtonColor)})
ButtonColor : EnButtonColor = EnButtonColor.Blue;

2 Likes

I know three styles.

Style 1

import { _decorator, Component, Enum, Node } from 'cc';
const { ccclass, property } = _decorator;

enum Pet {
    non = 1,
    dog = 2,
    cat = 3,
}

Enum(Pet);

@ccclass('EnumTest1')
export class EnumTest1 extends Component {
    @property({ type: Pet })
    public pet: Pet = Pet.non;
}

Style 2

import { _decorator, Component, Enum, Node } from 'cc';
const { ccclass, property } = _decorator;

const Pet = Enum({
    non: 1,
    dog: 2,
    cat: 3,
});

@ccclass('EnumTest2')
export class EnumTest2 extends Component {
    @property({ type: Pet })
    public = Pet.non;
}

Style3

import { _decorator, Component, Enum, Node } from 'cc';
const { ccclass, property, type} = _decorator;

const Pet = Enum({
    non: 1,
    dog: 2,
    cat: 3,
});

@ccclass('EnumTest2')
export class EnumTest2 extends Component {
    @type(Pet)
    public = Pet.non;
}

I prefer style 1 personally.

2 Likes

Thank you for your response.
I did not expect to receive an answer from the same person whose post I quoted.

1 Like

Thank you for your response.
It is very helpful for a beginner like me to be able to present the entire file.
Therefore, I have decided to accept this as the solution.