Skip to content

Instantly share code, notes, and snippets.

@hiyorin
Last active October 29, 2015 18:15
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 hiyorin/f797d024d03747424d30 to your computer and use it in GitHub Desktop.
Save hiyorin/f797d024d03747424d30 to your computer and use it in GitHub Desktop.
特定の種類(拡張子)のファイルだけを設定したい
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