https://github.com/solutena/toenumattribute
ToEnumAttribute
https://github.com/solutena/toenumattribute
attribute enum inspector string unity unitypackage
Last synced: 2 months ago
JSON representation
ToEnumAttribute
- Host: GitHub
- URL: https://github.com/solutena/toenumattribute
- Owner: solutena
- Created: 2024-01-08T13:48:25.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-04T18:14:56.000Z (over 1 year ago)
- Last Synced: 2025-06-02T17:38:23.613Z (10 months ago)
- Topics: attribute, enum, inspector, string, unity, unitypackage
- Language: C#
- Homepage:
- Size: 45.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ToEnumAttribute
인스펙터에서 string 타입을 지정한 enum 형식으로 보여주는 어트리뷰트입니다.
# 설치

1. URL 복사

2. 패키지 매니저에서 Add Package from Git URL 선택

3. 복사한 URL로 설치
## 예제
```csharp
public enum ItemType
{
Weapon,
Shield,
Armor
}
```
```csharp
public class Item : MonoBehaviour
{
[ToEnum(typeof(ItemType))] public string itemType;
}
```

enum을 string형식으로 선언 한 후 [ToEnum(typeof(`Enum`))] 을 추가
meta 파일에는 string으로 저장되기 때문에
enum의 중간에 값을 추가해도 값이 변하지 않습니다.
```csharp
public enum ItemType
{
None, //추가
Weapon,
Shield,
Armor
}
```
```csharp
public class Item : MonoBehaviour
{
[ToEnum(typeof(ItemType))] public string stringType;
public ItemType enumType;
}
```
### None타입 추가 전

### None타입 추가 후

## 활용
```csharp
public class Item : MonoBehaviour
{
[ToEnum(typeof(ItemType))] public string itemType;
public ItemType ItemType => Enum.TryParse(itemType, out ItemType result) ? result : ItemType.None;
}
```
코드에서 Enum으로 사용 할 땐 속성을 추가합니다.
```C#
public class Item : MonoBehaviour
{
[ToEnum(typeof(ItemType))] public List list;
[ToEnum(typeof(ItemType))] public string[] array;
}
```
리스트와 배열을 지원합니다.