1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unityメモ:UGUIのドロップダウンメニューに簡単にEnum型を適用する

Last updated at Posted at 2019-07-28

下記のプログラムでエディタ上から任意のプルダウンメニューに任意の型のEnum型でドロップダウンメニューを組めます。Enum型の中身が5個くらいまでなら手動で入れても良いですけど50個以上とかになると面倒ですよね。

enumNameにはタイプのフルネームを指定する必要があります。
インナーEnum型の場合は「"クラス名+Enum型名"」となるはずですのでご注意ください。

DropDownEnumSetter.cs
////////////////////////////////////////////////////////////////////////////////
//
// Light Wings GameProject
//
// (C) 2009 Light Wings GameProject.
//
// https://twitter.com/LightWings_gp
// https://twitter.com/sy_game_
//
////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DropDownEnumSetter : MonoBehaviour
{
	[SerializeField]
	Dropdown dropDown;

	[SerializeField]
	string enumName;

	void Awake()
	{
		var type = Type.GetType(enumName);
		if (type == null && !type.IsEnum) {
			Debug.LogError("指定のEnum型は存在しません");
			return;
		}

		//EnumをStringの配列に変換
		string[] enumNames = System.Enum.GetNames(type);

		//Stringの配列をリストに変換
		var names = new List<string>(enumNames);

		if(dropDown == null) {
			Debug.LogError("ドロップダウンメニューが存在しません");
			return;
		}

		// 一旦クリア
		dropDown.ClearOptions();

		//DropDownの要素にリストを追加
		dropDown.AddOptions(names);
	}
}

MIT License

Copyright (c) 2009 Light Wings GameProject.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?