Search Unity

Global Enum for Tags and Layers

Discussion in 'Scripting' started by tribaleur, Sep 21, 2018.

  1. tribaleur

    tribaleur

    Joined:
    Jan 19, 2017
    Posts:
    34
    Hello,

    I'm beginning to use Tags and Layers on my GameObjects and i have to use them by scripting.
    I found this following commands to use them and it works :
    GameObject.FindWithTag("tagName")
    LayerMask.NameToLayer("layername")


    But i don't like to use raw texte in my code. So I made a global namespace were I created Enum and static class to have a list of my tags and Layers.
    Code (CSharp):
    1.  
    2. namespace GeneraleNS {
    3.    
    4.     public static class Tags {
    5.         public const string InGame_EffectAreas    = "InGame_EffectAreas";
    6.         public const string InGame_Projectil    = "InGame_Projectil";
    7.     }
    8.  
    9.     public enum Layers {
    10.         Default = 0,
    11.         TransparentFX = 1,
    12.         IgnoreRaycast = 2,
    13.         Water = 4,
    14.         UI = 5,
    15.         PlayerCharacter = 10,
    16.         NonPlayerCharacter  = 11,
    17.         SpellProjectil = 20
    18.     }  
    19. }
    Than I can call them like this :
    GameObject.FindWithTag(Tags.InGame_EffectAreas)
    (int)Layers.SpellProjectil


    In my opinion it's much easier to maintain and to use. But i'm surprised that it doesn't exists natively in Unity.
    So my question is : am I missing something or is it a "real" good idea to do something like this namespace ?

    Thanks
     
    est-R likes this.
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Your setup is fine and fairly common. There's no real way for Unity to expose those kinds of structures because extending them would be impossible.
     
    wmep1979_unity likes this.
  3. tribaleur

    tribaleur

    Joined:
    Jan 19, 2017
    Posts:
    34
    Hi,

    So i'm not missing something ... it's a good news ;)

    Thanks for your answer.