Last active
November 13, 2024 10:53
-
-
Save der-hugo/0915d235f62dbf22e550ff568e09f5ec to your computer and use it in GitHub Desktop.
Inspector dropdown for selecting a tag
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; | |
namespace derHugo | |
{ | |
public class Example : MonoBehaviour | |
{ | |
public Tag Tag; | |
} | |
} |
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; | |
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