Files
SimUL/Assets/Scripts/ToolTip.cs
T
Vladimir Koshevarov 73f06d8754 added tooltips
2022-11-29 19:09:00 +02:00

57 lines
1.6 KiB
C#

using TMPro;
using UnityEngine;
using UnityEngine.UI;
[ExecuteInEditMode()]
public class ToolTip : MonoBehaviour
{
public TextMeshProUGUI headerField;
public TextMeshProUGUI contentField;
public LayoutElement layoutElement;
public int characterWrapLimit;
public RectTransform rectTransform;
private void Awake()
{
rectTransform = GetComponent<RectTransform>();
}
// Start is called before the first frame update
public void SetText(string content, string header = "")
{
if (string.IsNullOrEmpty(header))
{
headerField.gameObject.SetActive(false);
}
else
{
headerField.gameObject.SetActive(true);
headerField.text = header;
}
contentField.text = content;
int headerLength = headerField.text.Length;
int contentLength = contentField.text.Length;
layoutElement.enabled = (headerLength > characterWrapLimit || contentLength > characterWrapLimit);
}
// Update is called once per frame
void Update()
{
if (Application.isEditor)
{
int headerLength = headerField.text.Length;
int contentLength = contentField.text.Length;
layoutElement.enabled = (headerLength > characterWrapLimit || contentLength > characterWrapLimit);
}
Vector2 position = Input.mousePosition;
float pivotX = position.x / Screen.width;
float pivotY = position.y / Screen.height;
rectTransform.pivot = new Vector2(pivotX, pivotY);
transform.position = position;
}
}