冷奴の開発メモ

UnityとかC#とかプログラミングのことをメモしていく場所

IntをEnumとして表示する

ソース

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Diagnostics;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Hiyayakko
{
	[AttributeUsage(AttributeTargets.Field), Conditional("UNITY_EDITOR")]
	public sealed class PopupEnumAttribute : PropertyAttribute
	{
		internal Type type;

		public PopupEnumAttribute(Type type)
		{
			this.type = type;
		}
	}

	#if UNITY_EDITOR
	[CustomPropertyDrawer(typeof(PopupEnumAttribute))]
	public class PopupEnumAttributeDrawer : PropertyDrawer
	{
		public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
		{
			var popup = (PopupEnumAttribute)attribute;
			if (property.propertyType == SerializedPropertyType.Integer)
			{
				property.intValue = Convert.ToInt32(EditorGUI.EnumPopup(position, label, (Enum)Enum.ToObject(popup.type, property.intValue)));
			}
			else
			{
				EditorGUI.LabelField(position, label.text, "Use PopupEnum with int.");
			}
		}
	}
	#endif
}

使用例

private enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

[PopupEnum(typeof(Days))]
public int day = 0;

f:id:hiyayakkoapp:20161122003953p:plain