- Home /
how to get a variable like Layermask but for tags
when using this
public LayerMask[] m_LayerMask;
it shows with a drop down box for the arrey and also for all the layers in the scene but i want this for all the tags i have is there any variable that dose this?
I dont understand why you want a Layer$$anonymous$$ask Array. A Layer$$anonymous$$ask is the equivalent of a single int, decimalised. One int controls all layers.
Answer by haruna9x · Mar 12, 2018 at 03:07 AM
You need to create a custom Attribute, which is quite complex. However, there is a blog here that talks about this issue. You need to copy the two scripts you TagSelectorAttribute.cs and TagSelectorPropertyDrawer.cs. In particular, the TagSelectorPropertyDrawer.cs script must reside in a folder called "Editor". Then use the [TagSelector] attribute on any string variable.
TagSelectorAttribute.cs
using UnityEngine;
public class TagSelectorAttribute : PropertyAttribute
{
public bool UseDefaultTagFieldDrawer = false;
}
TagSelectorPropertyDrawer .cs
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
//Original by DYLAN ENGELMAN http://jupiterlighthousestudio.com/custom-inspectors-unity/
//Altered by Brecht Lecluyse http://www.brechtos.com
[CustomPropertyDrawer(typeof(TagSelectorAttribute))]
public class TagSelectorPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.String)
{
EditorGUI.BeginProperty(position, label, property);
var attrib = this.attribute as TagSelectorAttribute;
if (attrib.UseDefaultTagFieldDrawer)
{
property.stringValue = EditorGUI.TagField(position, label, property.stringValue);
}
else
{
//generate the taglist + custom tags
List<string> tagList = new List<string>();
tagList.Add("<NoTag>");
tagList.AddRange(UnityEditorInternal.InternalEditorUtility.tags);
string propertyString = property.stringValue;
int index = -1;
if(propertyString =="")
{
//The tag is empty
index = 0; //first index is the special <notag> entry
}
else
{
//check if there is an entry that matches the entry and get the index
//we skip index 0 as that is a special custom case
for (int i = 1; i < tagList.Count; i++)
{
if (tagList[i] == propertyString)
{
index = i;
break;
}
}
}
//Draw the popup box with the current selected index
index = EditorGUI.Popup(position, label.text, index, tagList.ToArray());
//Adjust the actual string value of the property based on the selection
if(index==0)
{
property.stringValue = "";
}
else if (index >= 1)
{
property.stringValue = tagList[index];
}
else
{
property.stringValue = "";
}
}
EditorGUI.EndProperty();
}
else
{
EditorGUI.PropertyField(position, property, label);
}
}
}
Thank you this works in a complex way thank you for helping
Answer by HideOnMada · Jun 01, 2020 at 11:07 AM
I'm fairly new to unity and I'm ashamed that I have to ask this but how do you even start using it?
Everyone started from the same place, dont worry!
In a script that you want to input in a variable a tag from the list, after following the steps regarding the script copy/pasting and placing in the 'Editor' folder in your project, you do this:
[TagSelector] public string playerTag;
Then, saving your script and adding it on any object, will show you the dropdown with all the tags you have!
I hope this was clear for you! Best of luck :)