Skip to content

Instantly share code, notes, and snippets.

@openroomxyz
Created April 21, 2020 12:05
Show Gist options
  • Select an option

  • Save openroomxyz/ecc5d94ab2958b20cadb13ce4767c957 to your computer and use it in GitHub Desktop.

Select an option

Save openroomxyz/ecc5d94ab2958b20cadb13ce4767c957 to your computer and use it in GitHub Desktop.
Unity : How to in code create / delete / check for existence of tags and layers ?
using UnityEditor;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Cosmos
{
public static class TagsAndLayers
{
private static int maxTags = 10000;
private static int maxLayers = 31;
//Return true, if tag was added, false otherwise.
public static bool AddNewTag(string tagName)
{
// Open tag manager
SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
// Tags Property
SerializedProperty tagsProp = tagManager.FindProperty("tags");
if (tagsProp.arraySize >= maxTags)
{
Debug.Log("No more tags can be added to the Tags property. You have " + tagsProp.arraySize + " tags");
return false;
}
// if not found, add it
if (!PropertyExists(tagsProp, 0, tagsProp.arraySize, tagName))
{
int index = tagsProp.arraySize;
// Insert new array element
tagsProp.InsertArrayElementAtIndex(index);
SerializedProperty sp = tagsProp.GetArrayElementAtIndex(index);
// Set array element to tagName
sp.stringValue = tagName;
Debug.Log("Tag: " + tagName + " has been added");
// Save settings
tagManager.ApplyModifiedProperties();
return true;
}
else
{
//Debug.Log ("Tag: " + tagName + " already exists");
}
return false;
}
private static bool PropertyExists(SerializedProperty property, int start, int end, string value)
{
for (int i = start; i < end; i++)
{
SerializedProperty t = property.GetArrayElementAtIndex(i);
if (t.stringValue.Equals(value))
{
return true;
}
}
return false;
}
//returns true, if tag was removed, false otherwise.
public static bool RemoveTag(string tagName)
{
// Open tag manager
SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
// Tags Property
SerializedProperty tagsProp = tagManager.FindProperty("tags");
if (PropertyExists(tagsProp, 0, tagsProp.arraySize, tagName))
{
SerializedProperty sp;
for (int i = 0, j = tagsProp.arraySize; i < j; i++)
{
sp = tagsProp.GetArrayElementAtIndex(i);
if (sp.stringValue == tagName)
{
tagsProp.DeleteArrayElementAtIndex(i);
Debug.Log("Tag: " + tagName + " has been removed");
// Save settings
tagManager.ApplyModifiedProperties();
return true;
}
}
}
return false;
}
/// Checks to see if tag exists.
/// <returns><c>true</c>, if tag exists, <c>false</c> otherwise.</returns>
public static bool TagExists(string tagName)
{
// Open tag manager
SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
// Layers Property
SerializedProperty tagsProp = tagManager.FindProperty("tags");
return PropertyExists(tagsProp, 0, maxTags, tagName);
}
/// Adds the layer.
/// <returns><c>true</c>, if layer was added, <c>false</c> otherwise.</returns>
public static bool AddNewLayer(string layerName)
{
// Open tag manager
SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
// Layers Property
SerializedProperty layersProp = tagManager.FindProperty("layers");
if (!PropertyExists(layersProp, 0, maxLayers, layerName))
{
SerializedProperty sp;
// Start at layer 9th index -> 8 (zero based) => first 8 reserved for unity / greyed out
for (int i = 8, j = maxLayers; i < j; i++)
{
sp = layersProp.GetArrayElementAtIndex(i);
if (sp.stringValue == "")
{
// Assign string value to layer
sp.stringValue = layerName;
Debug.Log("Layer: " + layerName + " has been added");
// Save settings
tagManager.ApplyModifiedProperties();
return true;
}
if (i == j)
Debug.Log("All allowed layers have been filled");
}
}
else
{
//Debug.Log ("Layer: " + layerName + " already exists");
}
return false;
}
public static string NewLayer(string name)
{
if (name != null || name != "")
{
AddNewLayer(name);
}
return name;
}
/// Removes the layer.
/// <returns><c>true</c>, if layer was removed, <c>false</c> otherwise.</returns>
public static bool RemoveLayer(string layerName)
{
// Open tag manager
SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
// Tags Property
SerializedProperty layersProp = tagManager.FindProperty("layers");
if (PropertyExists(layersProp, 0, layersProp.arraySize, layerName))
{
SerializedProperty sp;
for (int i = 0, j = layersProp.arraySize; i < j; i++)
{
sp = layersProp.GetArrayElementAtIndex(i);
if (sp.stringValue == layerName)
{
sp.stringValue = "";
Debug.Log("Layer: " + layerName + " has been removed");
// Save settings
tagManager.ApplyModifiedProperties();
return true;
}
}
}
return false;
}
/// Checks to see if layer exists.
/// <returns><c>true</c>, if layer exists, <c>false</c> otherwise.</returns>
public static bool LayerExists(string layerName)
{
// Open tag manager
SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
// Layers Property
SerializedProperty layersProp = tagManager.FindProperty("layers");
return PropertyExists(layersProp, 0, maxLayers, layerName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment