Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Why when creating a custom attribute ,using it in mono script the class name is different?

Discussion in 'Scripting' started by Chocolade, Jun 29, 2023.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    The PropertAttributre type script:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class GapRangeAttribute : PropertyAttribute
    5. {
    6.     public float minRange;
    7.     public float maxRange;
    8.     public float sensitivity;
    9.  
    10.     public GapRangeAttribute(float minRange, float maxRange, float sensitivity)
    11.     {
    12.         this.minRange = minRange;
    13.         this.maxRange = maxRange;
    14.         this.sensitivity = sensitivity;
    15.     }
    16. }
    17.  
    The PropertyDrawer type script:

    Code (csharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. [CustomPropertyDrawer(typeof(GapRangeAttribute))]
    6. public class GapRangeDrawer : PropertyDrawer
    7. {
    8.     float minRange;
    9.     float maxRange;
    10.     float sensitivity;
    11.  
    12.     public GapRangeDrawer(float minRange, float maxRange, float sensitivity)
    13.     {
    14.         this.minRange = minRange;
    15.         this.maxRange = maxRange;
    16.         this.sensitivity = sensitivity;
    17.     }
    18.  
    19.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    20.     {
    21.         if (property.propertyType == SerializedPropertyType.Float)
    22.         {
    23.             EditorGUI.BeginProperty(position, label, property);
    24.             EditorGUI.Slider(position, property, minRange, maxRange, label);
    25.             property.floatValue = RoundValue(property.floatValue);
    26.             EditorGUI.EndProperty();
    27.         }
    28.         else
    29.         {
    30.             EditorGUI.LabelField(position, label.text, "Use [GapRangeDrawer] with float values only!");
    31.         }
    32.     }
    33.  
    34.     private float RoundValue(float value)
    35.     {
    36.         return Mathf.Round(value / sensitivity) * sensitivity;
    37.     }
    38. }
    39.  
    Then when using it in a mono script I type : [GapRange but when I click on itwith the mouse right click and go to the definition it's getting to the method GapRangeAttribute in the in the PropertAttribute script.
    if the method name is GapRangeAttribute why in the mono script it's only GapRange?

    and it's working fine , I just wonder why in the mono script it's GapRange and not GapRagneAttribute?

    Code (csharp):
    1.  
    2. public class CubeSpawner : MonoBehaviour
    3. {
    4.     public GameObject cubePrefab;
    5.     public LayerMask terrainLayer;
    6.     [GapRange()]
    7. }
    8.  
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    It's just some syntax sugar of C#.

    For attributes C# doesn't require the 'Attribute' at the end because the 'Attribute' is implied since you're using it as an attribute (inside the square brackets).

    Just makes for cleaner looking code/text.
     
    Bunny83 and Chocolade like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Right, it's not required but strongly recommended to distinguish attributes from other runtime classes. As you said, the fact that you can omit the "Attribute" suffix is just syntactic sugar of the C# language. AFAIK you could use both variants, but of course any sane person would omit the suffix :). Proper IDEs shouldn't have any issues with that.

    Attributes have several simplifications. When the attribute doesn't have any arguments, you can also omit the parentheses. If you want to attach several attributes to the same thing, you can put them in separate square brackets or within the same, separated by a comma.

    Code (CSharp):
    1. [SerializeField()]
    2. [HideInInspector()]
    3. private int test;
    vs

    Code (CSharp):
    1. [SerializeField, HideInInspector]
    2. private int test;
    Attributes are pure compile-time constant metadata. Those "instances" are directly baked into the generated code and are only accessible through the reflection system which deals with all the metadata and type information.