Skip to content

Instantly share code, notes, and snippets.

@XakazukinX
Created May 3, 2020 04:27
Show Gist options
  • Save XakazukinX/0544e19ab5e6c589c41a79b1db75cb77 to your computer and use it in GitHub Desktop.
Save XakazukinX/0544e19ab5e6c589c41a79b1db75cb77 to your computer and use it in GitHub Desktop.
Enumの範囲を指定するやつ
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum, AllowMultiple = true)]
public class EnumRangeAttribute : PropertyAttribute
{
public Type Type { get; private set; }
public int Min { get; private set; }
public int Max { get; private set; }
public EnumRangeAttribute(Type selfType, int min, int max)
{
Type = selfType;
Min = min;
Max = max;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(EnumRangeAttribute))]
public class EnumAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (!(attribute is EnumRangeAttribute attr))
{
return;
}
//指定された範囲内のEnum要素とEnumメンバ
var enumName = new List<string>();
var enumMembers = new List<int>();
for (int i = attr.Min; i < attr.Max; i++)
{
if (!Enum.IsDefined(attr.Type, i)) continue;
enumName.Add(Enum.GetName(attr.Type, i));
enumMembers.Add(i);
}
//描画する
property.intValue = EditorGUI.IntPopup(position, property.displayName, property.intValue,
enumName.ToArray(), enumMembers.ToArray());
}
}
#endif
@XakazukinX
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment