OneShotOneKill/Assets/Script/My/MyEnumToInt.cs

52 lines
1.3 KiB
C#
Raw Normal View History

2026-01-07 21:27:42 +00:00
using System;
using System.Collections.Generic;
using UnityEngine;
public class MyEnumToInt : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod]
static void OnRuntimeMethodLoad()
{
new GameObject("MyEnumToInt").AddComponent<MyEnumToInt>();
}
public static MyEnumToInt Ins;
private Dictionary<Type, Dictionary<Enum, int>> enumToIntMapping = new Dictionary<Type, Dictionary<Enum, int>>();
void Awake()
{
Ins = this;
DontDestroyOnLoad(gameObject);
// 열거형 초기화
InitializeEnumMapping<eGameMode>();
InitializeEnumMapping<eDifficult>();
InitializeEnumMapping<eAttackType>();
InitializeEnumMapping<eStat>();
InitializeEnumMapping<ePurpose>();
}
private void InitializeEnumMapping<T>() where T : Enum
{
var mapping = new Dictionary<Enum, int>();
int index = 0;
foreach (T value in Enum.GetValues(typeof(T)))
{
mapping.Add(value, index++);
}
enumToIntMapping[typeof(T)] = mapping;
}
public int Get_Int<T>(T enumValue) where T : Enum
{
if (enumToIntMapping.TryGetValue(typeof(T), out var mapping))
{
return mapping[enumValue];
}
throw new Exception($"Enum type {typeof(T).Name} is not initialized.");
}
}