// This function is called when the object is loaded private void OnEnable() { // Get the scriptable object of the project containing all tags and load them reference = MultiTagsUtility.GetTagsAsset(); // Creates editor for the reference referenceEditor = (TagsEditor)Editor.CreateEditor(reference); }
/// <summary> /// Check if the tag name is valid. /// </summary> private void CheckName() { // If the new tag name entered contains the tag separator, indicate it and refuse to create the tag if (tagName.Contains(MultiTags.TAG_SEPARATOR)) { doesNameContainSeparator = true; doesNameAlreadyExist = false; isNameEmpty = false; SetBigSize(); } else if (doesNameContainSeparator) { doesNameContainSeparator = false; SetSmallSize(); } // If a tag with the same name already exist, indicate it and refuse to create the tag if (MultiTagsUtility.GetUnityTags().Contains(tagName)) { doesNameAlreadyExist = true; isNameEmpty = false; SetBigSize(); } else if (doesNameAlreadyExist) { doesNameAlreadyExist = false; SetSmallSize(); } // If the name entered is empty, indicate it and refuse to create the tag if (string.IsNullOrEmpty(tagName.Trim())) { isNameEmpty = true; SetBigSize(); } else if (isNameEmpty) { isNameEmpty = false; SetSmallSize(); } }
/// <summary> /// Draws this editor window content. /// </summary> private void DrawEditor() { EditorGUILayout.BeginHorizontal(); // Set the name & color of the new tag EditorGUILayout.LabelField(new GUIContent("Name :", "Name of the new tag to create"), GUILayout.Width(50)); EditorGUI.BeginChangeCheck(); tagName = EditorGUILayout.TextField(tagName, GUILayout.Width(100)); if (EditorGUI.EndChangeCheck()) { CheckName(); } EditorGUILayout.LabelField(new GUIContent("Color :", "Color of the new tag to create"), GUILayout.Width(50)); color = EditorGUILayout.ColorField(color, GUILayout.Width(100)); EditorGUILayout.EndHorizontal(); GUILayout.Space(10); EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Create", EditorStyles.miniButton, GUILayout.ExpandWidth(false)) && !doesNameAlreadyExist && !doesNameContainSeparator && !isNameEmpty) { // If everything is okay, create the new tag MultiTagsUtility.CreateTag(new Tag(tagName, color)); Close(); } EditorGUILayout.EndHorizontal(); // If the name contains the tag separator, indicate it if (doesNameContainSeparator) { EditorGUILayout.HelpBox("The name of the tag cannot contains \'" + MultiTags.TAG_SEPARATOR + "\'.", MessageType.Warning); } // If the name is not valid, indicate it else if (doesNameAlreadyExist) { EditorGUILayout.HelpBox("A tag with the same name already exist.", MessageType.Warning); } // If the name is empty, indicate it else if (isNameEmpty) { EditorGUILayout.HelpBox("You must enter a name.", MessageType.Warning); } }
/// <summary> /// Removes a tag from the project. /// </summary> /// <param name="_tag">Tag to remove</param> public void RemoveTag(Tag _tag) { // Display informative dialog about tag suppresion if (!EditorUtility.DisplayDialog("Confirm \"" + _tag.Name + "\" Tag Suppresion", "Are you sure you want to remove the Tag \"" + _tag.Name + "\" from the project ?" + "\n\n" + "This tag will be removed from all Game Objects with this tag in loaded scene(s), but if a Game Object " + "in an unloaded scene has it, it will keep it and tag will be recreated when loading this scene." + "\n\n" + "To be sure the tag will be definitely removed, please check in other scenes and remove it from objects " + "having it first.", "Confirm", "Cancel")) { return; } MultiTagsUtility.DestroyTag(_tag.Name); }
/// <summary> /// Connect this project Unity tags with the tags asset to create missing ones on both of them. /// </summary> private void ConnectProjectTags() { List <string> _projectTags = MultiTagsUtility.GetUnityTags().ToList(); List <string> _projectMultiTags = _projectTags.SelectMany(t => t.Split(MultiTags.TAG_SEPARATOR)).Distinct().ToList(); string[] _referenceTags = tagsSO.CustomTags.TagNames; string[] _referenceUnityTags = tagsSO.UnityBuiltInTags.TagNames; // Adds each tag of this scriptable object to the project in not having them yet foreach (string _tag in _referenceTags) { if (!_projectTags.Contains(_tag)) { MultiTagsUtility.CreateUnityTag(_tag); } else { _projectTags.Remove(_tag); } if (_projectMultiTags.Contains(_tag)) { _projectMultiTags.Remove(_tag); } } // Adds all tags of this project this object doesn't have to it foreach (string _tag in _projectMultiTags) { if (MultiTags.BuiltInTagsNames.Contains(_tag)) { if (!_referenceUnityTags.Contains(_tag)) { tagsSO.UnityBuiltInTags.AddTag(new Tag(_tag)); } } else { tagsSO.CustomTags.AddTag(new Tag(_tag)); } } // Saves the asset EditorUtility.SetDirty(tagsSO); }
/// <summary> /// Draws an editor for the custom tag system for the GameObject class. /// </summary> private void DrawTagSystem() { // If editing game object(s) tag has changed, update its tags if (targetGO.Select(g => g.tag) != lastTags) { GetObjectsTags(); Repaint(); } EditorGUILayout.BeginHorizontal(); // Title of the tag section EditorGUILayout.LabelField(new GUIContent("Tags"), EditorStyles.boldLabel); // Creates a button at the top right of the inspector to open the tags editor window // First, get its rect, then draw it Rect _editButtonRect = GUILayoutUtility.GetRect(new GUIContent("Edit Tags"), EditorStyles.miniButtonRight, GUILayout.Width(100)); if (GUI.Button(_editButtonRect, "Edit Tags", EditorStyles.miniButtonRight)) { TagsEditorWindow.CallWindow(); } EditorGUILayout.EndHorizontal(); // Draws the tag section for editing objects if (isUnfolded) { // Draws a tags field, to edit tag from editing object(s) Action <Tag> _addTagCallback = new Action <Tag>((Tag _tag) => MultiTagsUtility.AddTagToGameObjects(_tag, targetGO)); Action <Tag> _removeTagCallback = new Action <Tag>((Tag _tag) => MultiTagsUtility.RemoveTagFromGameObjects(_tag, targetGO)); MultiTagsUtility.GUILayoutTagsField(editingTags, _addTagCallback, _removeTagCallback, true); if (haveTargetsDifferentTags) { EditorGUILayout.HelpBox("You are editing Game Objects with different tags.\n" + "Only tags in common will be displayed in the inspector.", MessageType.Info); } } // Males a space in the editor to finish GUILayout.Space(7); }
/// <summary> /// Displays the tags of the project. /// </summary> public void DrawTags() { EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginVertical(); GUILayout.Space(10); // Draws a header EditorGUILayout.LabelField("Unity built-in Tags", EditorStyles.boldLabel); EditorGUILayout.EndVertical(); GUILayout.FlexibleSpace(); // Draw a button on top right to connect project tags if (GUILayout.Button(new GUIContent("Connect Project Tags", "Use this if Unity tags or tags on this asset have been created without using this editor ;\nThis will connect this project Unity tags with the tags asset to create missing tags on both of them."), GUILayout.Width(150), GUILayout.Height(25))) { ConnectProjectTags(); } EditorGUILayout.EndHorizontal(); GUILayout.Space(10); // If no tags, draws a information box and return if (tagsSO.UnityBuiltInTags == null || tagsSO.UnityBuiltInTags.ObjectTags.Length == 0) { EditorGUILayout.HelpBox("No built-in tag found on this project", MessageType.Info); } else { // Draw Unity built-in tags MultiTagsUtility.GUILayoutDisplayTags(tagsSO.UnityBuiltInTags.ObjectTags); } GUILayout.Space(10); EditorGUILayout.BeginHorizontal(); // Draws a header with a little plus button next to it, allowing to create new tags GUIContent _customTags = new GUIContent("Custom Tags", "Tags created by users on this project"); EditorGUILayout.LabelField(_customTags, EditorStyles.boldLabel, GUILayout.MaxWidth(EditorStyles.boldLabel.CalcSize(_customTags).x)); GUIStyle _olPlus = MultiTagsUtility.OLPlusStyle; Rect _buttonRect = GUILayoutUtility.GetRect(GUIContent.none, _olPlus); _buttonRect.y += 2; if (GUI.Button(_buttonRect, new GUIContent(string.Empty, "Create a new tag on this project"), _olPlus)) { CreateTagWindow.CallWindow(); } EditorGUILayout.EndHorizontal(); GUILayout.Space(10); // If no tags, draws a information box and return if (tagsSO.CustomTags == null || tagsSO.CustomTags.ObjectTags.Length == 0) { EditorGUILayout.HelpBox("No tag found on this project. How about create a first one ?", MessageType.Info); } else { // Draw all custom tags ; if clicking on a tag left button, remove it from the project and repaint MultiTagsUtility.GUILayoutTagsField(tagsSO.CustomTags.ObjectTags, RemoveTag); } }