やまめも

【 ゲーム開発・日々のことについて 】

【Unity】enum列挙型に関して

1. "string型(文字列型)"から"enum列挙型"ヘのキャスト

	private List<AchievementContents> LoadAbilityTableFromJSON(string json){
		JsonData jresult = JsonMapper.ToObject (json);
		List<AchievementContents> data = new List<AchievementContents> ();
		
		for(int i=0;i<jresult.Count;i++){
			data.Add(new AchievementContents(){
				_id = (int)jresult[i]["Id"],
				_name = (string)jresult[i]["Name"],
				_type =  (AchievementManager.ACHIEVEMEN_TYPE)Enum.Parse(typeof(AchievementManager.ACHIEVEMEN_TYPE), (string)jresult[i]["Type"]),
				_goal = (string)jresult[i]["Goal"],
				_rewardType = (string)jresult[i]["Reward Type"],
				_reward = (string)jresult[i]["Reward"],
			});

		}
		return data;
	}

数値や文字列を列挙体に変換する: .NET Tips: C#, VB.NET


2. "int型"から"enum列挙型"ヘのキャスト

using UnityEngine;
using System.Collections;

public class AngelManager : MonoBehaviour {


	public GameObject _gVideoAdsNotif;
	public enum ANGEL_BONUS_TYPE
	{
		ANGEL_BONUS_VIDEOADS 				= 0,
		ANGEL_BONUS_COIN				= 1,
		ANGEL_BONUS_SKILL_BAYANGAN			= 2,
	}
	
	void OnEnable()
	{
		EventManager.onCoinBonusClickEvent += HandleonAngelClickEvent;
	}	
	void OnDisable()
	{
		EventManager.onCoinBonusClickEvent -= HandleonAngelClickEvent;
	}

	private void HandleonAngelClickEvent()
	{
		int type = Random.Range(0, System.Enum.GetNames(typeof(ANGEL_BONUS_TYPE)).Length);


		switch((ANGEL_BONUS_TYPE)type){
		case ANGEL_BONUS_TYPE.ANGEL_BONUS_VIDEOADS:
			break;
		case ANGEL_BONUS_TYPE.ANGEL_BONUS_COIN:
			break;
		case ANGEL_BONUS_TYPE.ANGEL_BONUS_SKILL_BAYANGAN:
			break;
		}
	}
}


3. "enum列挙型"の数を数える

var AchievementTypeArray = System.Enum.GetNames(typeof(AchievementManager.ACHIEVEMEN_TYPE)).Length;

My C# script give me an error? - Unity Answers
列挙型と数値or文字列との相互変換や項目数の取得など【C#】 - (:3[kanのメモ帳]
enumの項目数を取得する - Qiita