G
G
Gwinterion2021-10-04 04:51:08
Unity
Gwinterion, 2021-10-04 04:51:08

Is it possible to assign a composite value to a field of type string in the inspector?

I want to make tooltips that will appear when hovering over an interface element and will contain some information. I use two classes for this:

public class TooltipSystem : MonoBehaviour
{
    private static TooltipSystem current;

    public Tooltip tooltip;

    public void Awake()
    {
        current = this;
    }

    public static void Show(string content, string header = "")
    {
        current.tooltip.SetText(content, header);
        current.tooltip.SetPosition();
        current.tooltip.gameObject.SetActive(true);
    }

    public static void Hide()
    {
        current.tooltip.gameObject.SetActive(false);
    }
}


and

class TooltipTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public string content;
    public string header;
    public void OnPointerEnter(PointerEventData eventData)
    {
        TooltipSystem.Show(content, header);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        TooltipSystem.Hide();
    }
}


The first class is responsible for displaying the tooltip, and the second is a trigger and passes the data that needs to be displayed in the tooltip. I bind the TooltipTrigger script to the interface element for which it is necessary to display a hint, and enter the output data in the inspector window.
615a5dbda5ee8204758633.png
Question: Is it possible to somehow set a composite value to the string field in the inspector?
For example:
string content = $"Heal recover {Player.MaxHealth / 10} HP.}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Boronnikov, 2021-10-04
@red-cat-fat

Yes, you can implement the unit's OnValidate method for this. It will work in the editor when the prefab is changed or loaded.

#if UNITY_EDITOR
    
    private void OnValidate()
    {
      if (content == "")
        content = $"Heal recover {Player.MaxHealth / 10} HP.}";
    }

#endif

Or dynamically substitute.
Type:
content = "Heal recover {0} HP";
...
var result = string.Format(content, 42);

Here, instead of 0, 42 will be transmitted. Just send the data you need there, in this example, Player.MaxHealth / 10.

N
Nekit Medvedev, 2021-10-06
@NIKROTOS

similar to: https://docs.unity3d.com/ScriptReference/TooltipAt...
and other attributes: https://habr.com/ru/post/331042/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question