GetLabels
Obtains all labels of a given asset.
Read time 2 minutesLast updated 4 days ago
Definition
- Type: Method
- Namespace: UnityEditor
- Assembly: UnityEditor
GetLabels(GUID)
Obtains all labels of a given asset.
public static string[] GetLabels(GUID guid)
Parameters
Returns
Type | Description |
|---|---|
| string[] | Returns all labels attached to a given asset. |
Remarks
The following code checks that every texture in the folder has a label, and logs a warning for any asset without the label.
RoadRoadRoadExamples
using System.Linq;using UnityEditor;using UnityEngine;static class TextureLabelAudit{ [MenuItem("Tools/Audit Texture Labels")] static void AuditTextureLabels() { string[] searchFolders = new string[] { "Assets/Textures/Road" }; GUID[] guids = AssetDatabase.FindAssetGUIDs("t:Texture2D", searchFolders); foreach (GUID guid in guids) { // GetLabels reads the label list from the asset database, so // the texture is never loaded into memory. string[] labels = AssetDatabase.GetLabels(guid); if (!labels.Contains("Road")) { string path = AssetDatabase.GUIDToAssetPath(guid); Debug.LogWarning($"{path} has no \"Road\" label."); } } }}
GetLabels(Object)
Obtains all labels of a given asset.
public static string[] GetLabels(Object obj)
Parameters
The asset object to check.
Returns
Type | Description |
|---|---|
| string[] | Returns all labels attached to a given asset. |
Remarks
The following code checks that every texture in the folder has a label, and logs a warning for any asset without the label:
RoadRoadRoadExamples
using System.Linq;using UnityEditor;using UnityEngine;static class RoadTextureLabelAudit{ [MenuItem("Tools/Audit Road Texture Labels")] static void AuditRoadTextureLabels() { // This example assumes the project contains ten road textures // named Road0 to Road9. for (int i = 0; i < 10; i++) { string texturePath = $"Assets/Textures/Road/Road{i}.png"; Object asset = AssetDatabase.LoadMainAssetAtPath(texturePath); if (asset == null) { Debug.LogWarning($"No asset found at \"{texturePath}\"."); continue; } string[] labels = AssetDatabase.GetLabels(asset); if (!labels.Contains("Road")) { Debug.LogWarning( $"\"{texturePath}\" has no \"Road\" label."); } } }}