Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tags and Layers Enums Builder for Unity3D
//This class is auto-generated, do not modify (use Tools/TagsLayersEnumBuilder)
public abstract class Layers {
public const string Default = "Default";
public const string TransparentFX = "TransparentFX";
public const string IgnoreRaycast = "Ignore Raycast";
public const string Water = "Water";
public const string UI = "UI";
public const string Camera = "Camera";
public const string Characters = "Characters";
public const string Floor = "Floor";
public const string Sprites = "Sprites";
public const string Terrain = "Terrain";
public const string Squads = "Squads";
public const string Selectable = "Selectable";
public const string Obstacles = "Obstacles";
public const int DefaultMask = 1;
public const int TransparentFXMask = 1 << 1;
public const int IgnoreRaycastMask = 1 << 2;
public const int WaterMask = 1 << 4;
public const int UIMask = 1 << 5;
public const int CameraMask = 1 << 8;
public const int CharactersMask = 1 << 9;
public const int FloorMask = 1 << 10;
public const int SpritesMask = 1 << 11;
public const int TerrainMask = 1 << 12;
public const int SquadsMask = 1 << 13;
public const int SelectableMask = 1 << 14;
public const int ObstaclesMask = 1 << 15;
public const int DefaultNumber = 0;
public const int TransparentFXNumber = 1;
public const int IgnoreRaycastNumber = 2;
public const int WaterNumber = 4;
public const int UINumber = 5;
public const int CameraNumber = 8;
public const int CharactersNumber = 9;
public const int FloorNumber = 10;
public const int SpritesNumber = 11;
public const int TerrainNumber = 12;
public const int SquadsNumber = 13;
public const int SelectableNumber = 14;
public const int ObstaclesNumber = 15;
}
//This class is auto-generated, do not modify (TagsLayersEnumBuilder.cs)
public abstract class Tags {
public const string AIOrder = "AIOrder";
public const string Camera2D = "Camera2D";
public const string CameraMan = "CameraMan";
public const string Dangerous = "Dangerous";
public const string Debug = "Debug";
public const string EditorOnly = "EditorOnly";
public const string EnemyStatsBar = "EnemyStatsBar";
public const string Finish = "Finish";
public const string Fire = "Fire";
public const string Formation = "Formation";
public const string GameController = "GameController";
public const string Ground = "Ground";
public const string HUDPanel = "HUDPanel";
public const string Item = "Item";
public const string MainCamera = "MainCamera";
public const string MatchTimerLabel = "MatchTimerLabel";
public const string Obstacles = "Obstacles";
public const string Player = "Player";
public const string PlayerSkillPointsBar = "PlayerSkillPointsBar";
public const string PlayerSpawningPoint = "PlayerSpawningPoint";
public const string PlayerStatsBar = "PlayerStatsBar";
public const string RangedAttack = "RangedAttack";
public const string Respawn = "Respawn";
public const string SelectedSquad = "SelectedSquad";
public const string Skill = "Skill";
public const string SkillButton = "SkillButton";
public const string SkillLabel = "SkillLabel";
public const string Smelly = "Smelly";
public const string SquadPosition = "SquadPosition";
public const string SquadSelector = "SquadSelector";
public const string SquadSpawningPoint = "SquadSpawningPoint";
public const string Unit = "Unit";
public const string Untagged = "Untagged";
}
#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
using System.IO;
using System.Text;
public class TagsLayersEnumBuilder : EditorWindow {
[MenuItem("Edit/Rebuild Tags And Layers Enums")]
static void RebuildTagsAndLayersEnums() {
var enumsPath = Application.dataPath + "/Code/Enums/";
rebuildTagsFile(enumsPath + "Tags.cs");
rebuildLayersFile(enumsPath + "Layers.cs");
AssetDatabase.ImportAsset(enumsPath + "Tags.cs", ImportAssetOptions.ForceUpdate);
AssetDatabase.ImportAsset(enumsPath + "Layers.cs", ImportAssetOptions.ForceUpdate);
}
static void rebuildTagsFile(string filePath) {
StringBuilder sb = new StringBuilder();
sb.Append("//This class is auto-generated, do not modify (TagsLayersEnumBuilder.cs)\n");
sb.Append("public abstract class Tags {\n");
var srcArr = UnityEditorInternal.InternalEditorUtility.tags;
var tags = new String[srcArr.Length];
Array.Copy(srcArr, tags, tags.Length);
Array.Sort(tags, StringComparer.InvariantCultureIgnoreCase);
for (int i = 0, n = tags.Length; i < n; ++i) {
string tagName = tags[i];
sb.Append("\tpublic const string " + tagName + " = \"" + tagName + "\";\n");
}
sb.Append("}\n");
#if !UNITY_WEBPLAYER
File.WriteAllText(filePath, sb.ToString());
#endif
}
static void rebuildLayersFile(string filePath) {
StringBuilder sb = new StringBuilder();
sb.Append("//This class is auto-generated, do not modify (use Tools/TagsLayersEnumBuilder)\n");
sb.Append("public abstract class Layers {\n");
var layers = UnityEditorInternal.InternalEditorUtility.layers;
for (int i = 0, n = layers.Length; i < n; ++i) {
string layerName = layers[i];
sb.Append("\tpublic const string " + GetVariableName(layerName) + " = \"" + layerName + "\";\n");
}
sb.Append("\n");
for (int i = 0, n = layers.Length; i < n; ++i) {
string layerName = layers[i];
int layerNumber = LayerMask.NameToLayer(layerName);
string layerMask = layerNumber == 0 ? "1" : ("1 << " + layerNumber);
sb.Append("\tpublic const int " + GetVariableName(layerName) + "Mask" + " = " + layerMask + ";\n");
}
sb.Append("\n");
for (int i = 0, n = layers.Length; i < n; ++i) {
string layerName = layers[i];
int layerNumber = LayerMask.NameToLayer(layerName);
sb.Append("\tpublic const int " + GetVariableName(layerName) + "Number" + " = " + layerNumber + ";\n");
}
sb.Append("}\n");
#if !UNITY_WEBPLAYER
File.WriteAllText(filePath, sb.ToString());
#endif
}
private static string GetVariableName(string str) {
return str.Replace(" ", "");
}
}
#endif
@Minamiyama

This comment has been minimized.

Copy link

commented Oct 18, 2014

Fantastic work!!! Thx, It's what I need right now.

When I use it, some problem occured when some layer names started whith numeric character like "2DLayer". And I do some fixes for it.
Here it is:

  1. Add a static method

    private static string CheckStartWithNumberic(string str)
    {
        if (str[0] >= '0' && str[0] <= '9')
        {
            return string.Format("_{0}", str);
        }
        else return str;
    }
    
  2. Use the method above after get field name:
    here:

    private static string GetVariableName(string str)
    {
        return CheckStartWithNumberic(str.Replace(" ", ""));
    }
    

    and here (in method of rebuildTagsFile):
    sb.Append("\tpublic const string " + CheckStartWithNumberic(tagName + " = "" + tagName + "";\n"));

  3. Done

Another problem: the path of "enumsPath" isn't exist
add a check and create it

    var enumsPath = Application.dataPath + "/Code/Enums/";

    if (!Directory.Exists(enumsPath))
    {
        Directory.CreateDirectory(enumsPath);
    }

All above.

Thank you for all your working~~
Best wishes
yours

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.