Skip to content

Instantly share code, notes, and snippets.

@uranuno
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uranuno/8be43847015f5e25cf17 to your computer and use it in GitHub Desktop.
Save uranuno/8be43847015f5e25cf17 to your computer and use it in GitHub Desktop.

NameCreator と一緒に使います。

// 自動生成されたクラスを属性で指定することで、
// Inspector上でPopupで値を入力することができる
[Name(typeof(SceneName))] public string sceneName;

ss

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using System;
using System.Linq;
using System.Collections.Generic;
#endif
public class NameAttribute : PropertyAttribute
{
public System.Type type;
public NameAttribute(System.Type type)
{
this.type = type;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(NameAttribute))]
public class NameDrawer : PropertyDrawer
{
const string emptyText = "--EMPTY--";
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
NameAttribute nameAttribute = (NameAttribute)attribute;
string[] emptyValues = {emptyText};
string[] constValues = nameAttribute.type.GetFields()
.Where(f=>f.FieldType==typeof(string[]))
.Select(f=>(string[])f.GetValue(null))
.FirstOrDefault();
string[] values = emptyValues.Concat(constValues).ToArray();
int[] indexes = Enumerable.Range(0,values.Length).ToArray();
string currentValue = property.stringValue;
int selected = Mathf.Max(Array.IndexOf(values, currentValue),0);
selected = EditorGUI.IntPopup(position, label.text, selected, values, indexes);
string selectedValue = (selected == 0) ? "" : values[selected];
property.stringValue = selectedValue;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment