Created
May 3, 2020 04:27
-
-
Save XakazukinX/0544e19ab5e6c589c41a79b1db75cb77 to your computer and use it in GitHub Desktop.
Enumの範囲を指定するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こんな感じ↓の列挙型に対して
enum Hoge
{
FugaStart=0,
FugaValue
FugaValueValue,
FugaEnd,
}
[EnumRange(typeof(Hoge), (int) Hoge.FugaStart + 1, (int) Hoge.FugaEnd)]
public Hoge hoge;
みたいに指定してあげると便利、かも