Skip to content

Instantly share code, notes, and snippets.

@WingmanAlex
Last active Mar 19, 2021
Embed
What would you like to do?
Tooltip system for the game Fey Foray
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
[ExecuteInEditMode()]
public class Tooltip : MonoBehaviour
{
public TextMeshProUGUI headerField;
public TextMeshProUGUI contentField;
public LayoutElement layoutElement;
public int characterWrapLimit;
void Update()
{
int headerLength = headerField.text.Length;
int contentLength = contentField.text.Length;
layoutElement.enabled =
(headerLength > characterWrapLimit || contentLength > characterWrapLimit) ? true : false;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToolTipSystem : MonoBehaviour
{
private static ToolTipSystem current;
public Tooltip_01 tooltip;
public void Awake()
{
current = this;
}
public static void Show(string content, string header = "")
{
current.tooltip.SetText(content, header);
current.tooltip.gameObject.SetActive(true);
}
public static void Hide()
{
current.tooltip.gameObject.SetActive(false);
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ToolTipToggle : MonoBehaviour
{
public TextMeshProUGUI onOffText;
public Canvas canvasObject;
private bool canvasEnabled;
private void Start()
{
EnableCanvas();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.T))
{
if (canvasEnabled == true)
{
DisableCanvas();
}
else
{
EnableCanvas();
}
}
}
void DisableCanvas()
{
canvasObject.GetComponent<Canvas>().enabled = false;
onOffText.text = "Off";
onOffText.color = new Color32(224,43, 14, 255);
canvasEnabled = false;
}
private void EnableCanvas()
{
canvasObject.GetComponent<Canvas>().enabled = true;
onOffText.text = "On";
onOffText.color = new Color32(39, 150, 109, 255);
canvasEnabled = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class TooltipTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public string header;
[Multiline()]
public string content;
public void OnPointerEnter(PointerEventData eventData)
{
StartCoroutine(Wait(0.5f));
ToolTipSystem.Show(content, header);
}
public void OnPointerExit(PointerEventData eventData)
{
ToolTipSystem.Hide();
}
public IEnumerator Wait(float delayInSeconds)
{
yield return new WaitForSeconds(delayInSeconds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment