Skip to content

Instantly share code, notes, and snippets.

@der-hugo
Last active November 13, 2024 10:53
Show Gist options
  • Save der-hugo/0915d235f62dbf22e550ff568e09f5ec to your computer and use it in GitHub Desktop.
Save der-hugo/0915d235f62dbf22e550ff568e09f5ec to your computer and use it in GitHub Desktop.
Inspector dropdown for selecting a tag
using UnityEngine;
namespace derHugo
{
public class Example : MonoBehaviour
{
public Tag Tag;
}
}
using System;
using UnityEngine;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace derHugo
{
[Serializable]
public struct Tag
{
[SerializeField] private string m_Name;
public Tag(string name)
{
m_Name = name;
}
public static implicit operator string (Tag tag)
{
return tag.m_Name;
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(Tag))]
public class SingleLayerAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
using (new EditorGUI.PropertyScope(position, label, property))
{
position = EditorGUI.PrefixLabel(position, label);
var nameProperty = property.FindPropertyRelative(nameof(m_Name));
var existingTags = UnityEditorInternal.InternalEditorUtility.tags;
if (!existingTags.Any(o => o == nameProperty.stringValue))
{
nameProperty.stringValue = existingTags[0];
}
nameProperty.stringValue = EditorGUI.TagField(position, nameProperty.stringValue);
}
}
}
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment