Created
December 4, 2015 15:29
-
-
Save Simie/ca742233957179cbb56f to your computer and use it in GitHub Desktop.
Editor extension providing a dropdown picker for choosing resources. (Requires TypeSafe, https://www.stompyrobot.uk/tools/typesafe)
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 UnityEngine; | |
public class ResourceDropdownTest : MonoBehaviour | |
{ | |
[ResourcePicker(typeof(Texture2D))] | |
public string ResourceTest; | |
} |
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 System.Reflection; | |
using TypeSafe; | |
using UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
using Object = UnityEngine.Object; | |
public class ResourcePickerAttribute : PropertyAttribute | |
{ | |
/// <summary> | |
/// The type of the Resource that can be picked for this field | |
/// </summary> | |
public Type ResourceType { get; private set; } | |
/// <summary> | |
/// An optional filter, used by string.StartsWith applied to path. | |
/// </summary> | |
public string Filter { get; private set; } | |
public ResourcePickerAttribute(Type resourceType, string filter = null) | |
{ | |
ResourceType = resourceType; | |
Filter = filter; | |
} | |
} | |
#if UNITY_EDITOR | |
[CustomPropertyDrawer(typeof(ResourcePickerAttribute))] | |
public class ResourcePickerDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
var resourceAttribute = attribute as ResourcePickerAttribute; | |
if (!typeof (UnityEngine.Object).IsAssignableFrom(resourceAttribute.ResourceType)) | |
{ | |
Debug.LogError("[ResourceDropdownDrawer] ResourceType must inherit from UnityEngine.Object"); | |
return; | |
} | |
var availableTypes = GetContentsRecursive(resourceAttribute.ResourceType, resourceAttribute.Filter); | |
var nameList = new GUIContent[availableTypes.Count]; | |
var selectedIndex = -1; | |
for (var i = 0; i < availableTypes.Count; i++) | |
{ | |
if (property.stringValue == availableTypes[i].Path) | |
{ | |
selectedIndex = i; | |
} | |
nameList[i] = new GUIContent(availableTypes[i].Path); | |
} | |
var newIndex = EditorGUI.Popup(position, label, selectedIndex, nameList); | |
if (newIndex != selectedIndex) | |
{ | |
property.stringValue = newIndex >= 0 ? availableTypes[newIndex].Path : null; | |
} | |
} | |
private static List<IResource> GetContentsRecursive(Type type, string filter) | |
{ | |
return | |
(List<IResource>) | |
typeof (ResourcePickerDrawer).GetMethod("GetContentsRecursiveInternal", | |
BindingFlags.Static | BindingFlags.NonPublic) | |
.MakeGenericMethod(type) | |
.Invoke(null, new object[] {filter}); | |
} | |
private static List<IResource> GetContentsRecursiveInternal<T>(string filter) where T : Object | |
{ | |
var returnList = new List<IResource>(); | |
var contents = SRResources.GetContentsRecursive<T>(); | |
foreach (var resource in contents) | |
{ | |
if(filter == null || resource.Path.StartsWith(filter)) | |
returnList.Add(resource); | |
} | |
return returnList; | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment