Right now I’m exposing a string to write the tags.
I guess the better question is: How can I get the enum representing the tags of my game in a script, so I can then expose it in the inspector?
Right now I’m exposing a string to write the tags.
I guess the better question is: How can I get the enum representing the tags of my game in a script, so I can then expose it in the inspector?
They are strings, afaik…Not sure how to produce that list in the inspector (beyond the one that’s there by default).
I did a quick google search and it may be possible to access it otherwise, but depending really on what you want…? There may be easier ways.
There has to be some way, what I want is just to expose it in the inspector like an enum would be or like a LayerMask object would be.
First google result for “unity get all tags”
You could use an enum and an array of strings.
string[ ] stringtags = new string[ ]{“player”, “enemy”};
public enum tagNumbers{player,enemy};
public tagNumbers tagNum;
Great that takes care of finding them!
Now I still have to expose it in the inspector. I’ll try figuring that out and I’ll post the answer when I find it.
Like…literally linked from the page I posted: https://docs.unity3d.com/ScriptReference/EditorGUI.TagField.html
This works ok but you have to remember to keep them in sync.
That’s not for the inspector, right? Only for editor windows.
I saw this in that thread: http://answers.unity3d.com/questions/854000/creating-enum-using-a-string-array.html
[CustomEditor(typeof(PointerColliderAction))]
public class PointerColliderEditor : Editor
{
string[] tagStr = UnityEditorInternal.InternalEditorUtility.tags;
int tagIndex = 0;
public override void OnInspectorGUI ()
{
// Draw the default inspector
DrawDefaultInspector();
tagIndex = EditorGUILayout.Popup(tagIndex, tagStr);
EditorUtility.SetDirty(target);
}
}
But apparently you can only input 1 tag at a time, I want the possibility for several. And currently anyway it doesn’t work in my editor but that might be because my dll is not one of Unity’s official.
So wait - you’re trying to apply multiple tags to the same GameObject?
I’m trying to define what tags a hitbox can hit, so it could be that the hitbox can hit a various amount of tags.
This might help with enum-based behavior: Custom inspector multi-select enum dropdown? - Questions & Answers - Unity Discussions
Otherwise it’ll be tough. Layermasks are bitmasks internally which wouldn’t work for tags because you can have as many of them as you want. It would be easier to have an array of strings and draw a custom inspector that gives you a dropdown of available tags instead of making you type them in manually.
I see, I’ll take a look into that, thanks, it might be better to stick to my strings in the end!
Of course if you go the dropdown method then you’ll have to figure out what happens when you remove a tag…
It might be better to tackle it from a component perspective. Make scripts that implement a common interface and check for that interface on the thing you’re interacting with.
I will provide a few different answers, and you can pick what is relevant. I think the main question evolved into making a tags string array into a multiple select drop down inspector GUI. This is addressed by the first answer.
Create multiple select drop-down from string array (Editor script): Unity Mask Field
Programmatically read and change tags (Editor script): Forum link
Multiple Tags for a gameObect (I authored one of them): Search asset store
Regards,
Kevin
To be clear - this is a multi-select from an enum, not a multi-select from a string array.
Try the link again, my apologies I copied the wrong link. It worked well in my MultipleTags product as shown below. The tagFlagMask is an integer with bits set (One-hot) for each index in the string array that is selected.
int tagFlagMask;
string[] displayTags;
tagFlagMask = EditorGUILayout.MaskField("MyLabel", tagFlagMask, displayTags);
I was implementing this myself when I found that it was already implemented in the Cinemachine package. To use it:
using Cinemachine;
public class SomeClass: Monobehaviour
{
[TagField]
[SerializeField] private string yourTag;
}
This will show a dropdown in the inspector allowing you to pick a tag as if it was an enum
Whoa, didn’t know this, but it really works, Thanks for this mate!
For people who don’t need a multi-tag selector, only single tag selector, note that you can use the built-in methods directly: either EditorGUI.TagField or EditorGUILayout.TagField depending on where you are.
A. If you’re writing an attribute + drawer, as in Using tags as a dropdown property in Unity's inspector using PropertyDrawers - Brecht Lecluyse then just use EditorGUI.TagField as you already have Rect position from context (note that the drawer in the link above has two branches, in the first one they use TagField indeed, while in the second one they use UnityEditorInternal.InternalEditorUtility.tags + EditorGUI.Popup, similarly to the code snippet with PointerColliderEditor in this thread, except it’s not EditorGUILayout.Popup).
B. If you’re writing a custom editor (like PointerColliderEditor above), then you can use EditorGUILayout.TagField directly.
I discovered something important by reading Unity - Scripting API: EditorGUI.BeginProperty
To support multi-select, you must make sure to surround the property control call with a Change Check (either EditorGUI.BeginChangeCheck() / if (EditorGUI.EndChangeCheck()) or using (var check = new EditorGUI.ChangeCheckScope())
+ if (check.changed)
inside), then only set the property if a change occurred.
Otherwise, when selecting multiple object and multi-select inspector is supported, the property would be set to the last selected object (in Hierarchy order, apparently) property value immediately, as soon as the inspector is shown, causing all selected objects’ components to receive the same value. I verified that adding the change check fixes this indeed.
At least this affects custom PropertyDrawer, and I suspect the same would happen with a custom Editor with override OnInspectorGUI.
Probably, the TagSelectorPropertyDrawer defined in the blog page linked in my previous post would benefit from this fix too.