Skip to content

Instantly share code, notes, and snippets.

@hiyorin hiyorin/Example.cs
Last active Oct 29, 2015

Embed
What would you like to do?
特定の種類(拡張子)のファイルだけを設定したい
using UnityEngine;
public class Example : MonoBehaviour
{
[SerializeField, FilterFileExtension ("unity")]
private Object[] sceneFileList = new Object[] {};
}
using UnityEngine;
public class FilterFileExtensionAttribute : PropertyAttribute
{
public string fileExtension = string.Empty;
public FilterFileExtensionAttribute (string fileExtension)
{
this.fileExtension = fileExtension;
}
}
using UnityEditor;
using UnityEngine;
using System.IO;
[CustomPropertyDrawer (typeof (FilterFileExtensionAttribute))]
public class FilterFileExtensionDrawer : PropertyDrawer
{
public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true);
}
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
FilterFileExtensionAttribute attr = attribute as FilterFileExtensionAttribute;
if (property.objectReferenceValue != null)
{
string path = AssetDatabase.GetAssetPath (property.objectReferenceValue);
if (Path.GetExtension (path).Equals ("." + attr.fileExtension) == false)
{
Debug.LogErrorFormat ("{0} is not {1}", property.objectReferenceValue.name, attr.fileExtension);
property.objectReferenceValue = null;
}
}
EditorGUI.PropertyField (position, property, label);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.