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

Is there a way to set a custom name to a component in the inspector?

Discussion in 'Scripting' started by pleasurehouse, Apr 29, 2021.

  1. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    I have a class that I use to do different things.
    I would like to know if there is any way to name it and differentiate it at a glance.

    This is my code:

    Code (CSharp):
    1.  
    2. //-------------------------------------------------------
    3. using TransformExtensionMethods;
    4. using UnityEngine;
    5. //-------------------------------------------------------
    6. public class HorizontalDetector : MonoBehaviour
    7. {
    8.     //-------------------------------------------------------
    9.     [SerializeField] float rayCastDistance = 5f;
    10.     [SerializeField] LayerMask layerToDetect;
    11.     [SerializeField] private float offsetX = 0f;
    12.     [SerializeField] private float offsetY = 0f;
    13.     RaycastHandler ray;
    14.     //-------------------------------------------------------
    15.     void Awake() => ray = new RaycastHandler();
    16.     //--------------------------------------------------------
    17.     public bool IsDetected()
    18.     {
    19.         if (ray == null) return false;
    20.         Transform transform1;
    21.         Vector3 direction = (transform1 = transform).GetDirection();
    22.         var position = transform1.position;
    23.         Vector2 origin = new Vector2(position.x + offsetX, position.y + offsetY);
    24.         return ray.IsHitting(origin, direction, rayCastDistance, layerToDetect);
    25.     }
    26.     //-------------------------------------------------
    27.     public Vector3 GetPoint()
    28.     {
    29.         return ray.HitPoint();
    30.     }
    31.     //-------------------------------------------------
    32. }
    33.  
    34.  

    I am looking for something like this:

    Code (CSharp):
    1.  
    2. public class HorizontalDetector : MonoBehaviour
    3. {
    4.     [SetNameFromEditor("  ")]
    5.     //or
    6.     [SetLabel(" ")]
    7.     //or
    8.     [SetCaption(" ")]
    9.    
    10. }
    11.  
    I've thought of just putting a string at the beginning and serializing it. Something like this.

    Code (CSharp):
    1.  
    2. public class HorizontalDetector : MonoBehaviour
    3. {
    4.    [SerializeField] string ComponentName;
    5. }
    6.  
    But that would be a variable that I would not use in the computation and would occupy memory.

    And maybe there is something to do what I'm looking for, I still don't know.

    Is there a way to custom name a component?

    Thank you so much!!
     
    Last edited: Apr 29, 2021
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    Nope. It is not possible and it would make serialization impossible. You can inherit from this base class to more specialized ones and use those in your project. It is a good idea anyway to use this kind of very generic classes as services or bases and use specialized ones to interact with.

    Alternatively you can use the Odin asset (paid), it has some capabilities to put custom labels in your inspector without writing custom inspector code. But research this well, because that solution may not suit your case, they are using attributes for it with some logic capabilities.
     
    pleasurehouse likes this.
  3. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    Okay, thanks for your help!!
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Having recently purchased Odin Inspector, I have been able to make a title that's based on a Scriptable Object's name to make it easier to read in the editor. You can use expressions to call a Property or Method that returns a string. I simply had a Property that returned 'this.name' used by a label attribute.

    The same should be possible in a monobehavior too.
     
    pleasurehouse likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    This ^ ^ ^ and you can even inherit from your base class and provide nothing new at all in the derived class. The "empty offspring" of your base class will still be treated as completely different classes, if you only want the "typing" of it.
     
    pleasurehouse likes this.
  6. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    Thanks for your answers gentlemen