- Home /
Retrieve Variable's [Tooltip()] Property From a Custom Inspector (C#)
Hello,
I have been searching for a solution to this question for quite a while, unfortunately, to no avail:
How does one retrieve a script's [[Tooltip()] property from within a Custom Inspector?
I wish to retrieve the value of a tooltip defined within a script using Unity 4.6+'s [Tooltip()] attribute. In its place, I am writing what you see below, which seems redundant:
The script (C#):
using UnityEngine;
public class Example : MonoBehaviour
{
[Tooltip("Here is a tooltip")]
public float testFloat;
}
The inspector (C#):
using UnityEditor;
[CustomEditor(typeof(Example))]
public class ExampleEditor : Editor
{
private void OnEnable()
{
Target = (Example) target;
}
public override void OnInspectorGUI()
{
Target.testFloat = EditorGUILayout.FloatField(new GUIContent("Test Float", "Here is a tooltip"), Target.testFloat);
}
}
Replacing "Here is a tooltip" in the custom inspector with a call to the field's [Tooltip()] property will not only save time and code, but will make the script far more maintainable.
Thank you greatly in advance!
Answer by Hodgson_SDAS · Jan 07, 2016 at 01:11 PM
After more research the OP actually got it answered in the Unity Forums: http://forum.unity3d.com/threads/retrieve-variables-tooltip-property-from-a-custom-inspector-c.312107/
Answer by Dave-Carlile · Jan 05, 2016 at 09:05 PM
You should be able to use the Attribute.GetCustomAttribute method as discussed in Microsoft's Retrieving Information Stored in Attributes article.
One of the examples shows how to use the class type to get a list of members and then retrieve attributes for those members. You should be able to grab just the single member you want and then get the specific attribute you want. I'm not somewhere where I can dig into it more deeply than that right now, but hopefully that's enough to point you in the right direction.