Created
October 19, 2016 10:39
-
-
Save ProGM/9cb9ae1f7c8c2a4bd3873e4df14a6687 to your computer and use it in GitHub Desktop.
A PropertyDrawer to show a popup field with a generic list of string for your Unity3d attribute
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
public class MyBehavior : MonoBehaviour { | |
// This will store the string value | |
[StringInList("Cat", "Dog")] public string Animal; | |
// This will store the index of the array value | |
[StringInList("John", "Jack", "Jim")] public int PersonID; | |
// Showing a list of loaded scenes | |
[StringInList(typeof(PropertyDrawersHelper), "AllSceneNames")] public string SceneName; | |
} |
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 UnityEditor; | |
using UnityEngine; | |
public static class PropertyDrawersHelper { | |
#if UNITY_EDITOR | |
public static string[] AllSceneNames() | |
{ | |
var temp = new List<string>(); | |
foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes) | |
{ | |
if (S.enabled) | |
{ | |
string name = S.path.Substring(S.path.LastIndexOf('/')+1); | |
name = name.Substring(0,name.Length-6); | |
temp.Add(name); | |
} | |
} | |
return temp.ToArray(); | |
} | |
#endif | |
} |
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 UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class StringInList : PropertyAttribute { | |
public delegate string[] GetStringList(); | |
public StringInList(params string [] list) { | |
List = list; | |
} | |
public StringInList(Type type, string methodName) { | |
var method = type.GetMethod (methodName); | |
if (method != null) { | |
List = method.Invoke (null, null) as string[]; | |
} else { | |
Debug.LogError ("NO SUCH METHOD " + methodName + " FOR " + type); | |
} | |
} | |
public string[] List { | |
get; | |
private set; | |
} | |
} | |
#if UNITY_EDITOR | |
[CustomPropertyDrawer(typeof(StringInList))] | |
public class StringInListDrawer : PropertyDrawer { | |
// Draw the property inside the given rect | |
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) { | |
var stringInList = attribute as StringInList; | |
var list = stringInList.List; | |
if (property.propertyType == SerializedPropertyType.String) { | |
int index = Mathf.Max (0, Array.IndexOf (list, property.stringValue)); | |
index = EditorGUI.Popup (position, property.displayName, index, list); | |
property.stringValue = list [index]; | |
} else if (property.propertyType == SerializedPropertyType.Integer) { | |
property.intValue = EditorGUI.Popup (position, property.displayName, property.intValue, list); | |
} else { | |
base.OnGUI (position, property, label); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sure! Assume the license is MIT :)