forked from James9074/Unity-Tooltips
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The first upload to GitHub
- Loading branch information
Showing
12 changed files
with
409 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
using UnityEngine.Events; | ||
using UnityEngine.EventSystems; | ||
using System.Collections; | ||
using System; | ||
|
||
public class Tooltip : MonoBehaviour | ||
{ | ||
public static Tooltip Instance; | ||
|
||
|
||
//Text of the tooltip | ||
Text mText; | ||
|
||
//if the tooltip is inside a UI element | ||
[SerializeField] | ||
bool mInside; | ||
|
||
[SerializeField] | ||
float mWidth; | ||
[SerializeField] | ||
float mHeight; | ||
|
||
float mCanvasWidth; | ||
[SerializeField] | ||
float mCanvasHeight; | ||
|
||
[SerializeField] | ||
float mYShift; | ||
[SerializeField] | ||
float mXShift; | ||
|
||
RectTransform mCornerImage; | ||
RenderMode mGUIMode; | ||
CanvasScaler mScaler; | ||
|
||
void Awake() | ||
{ | ||
Instance = this; | ||
} | ||
|
||
void Start() | ||
{ | ||
mText = GetComponentInChildren<Text>(); | ||
mCornerImage = transform.Find("Corner").GetComponent<RectTransform>(); | ||
mGUIMode = transform.parent.GetComponent<Canvas>().renderMode; | ||
mScaler = transform.parent.GetComponent<CanvasScaler>(); | ||
HideTooltip(); | ||
} | ||
|
||
public void SetTooltip(string aText, int aMaxWidth = 0) | ||
{ | ||
//ScreenSpaceOverlay Tooltip | ||
if (mGUIMode == RenderMode.ScreenSpaceOverlay && aText != "" && aText != null) | ||
{ | ||
mText.GetComponent<RectTransform>().sizeDelta = new Vector2(100, 100); | ||
mText.text = aText; | ||
|
||
if (aMaxWidth != 0) | ||
{ | ||
mText.horizontalOverflow = HorizontalWrapMode.Wrap; | ||
mText.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(aMaxWidth, 100); | ||
this.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(aMaxWidth + 60f, mText.preferredHeight + 20f); | ||
|
||
} | ||
else | ||
{ | ||
mText.horizontalOverflow = HorizontalWrapMode.Overflow; | ||
this.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(mText.preferredWidth + 60f, mText.preferredHeight + 20f); | ||
} | ||
mWidth = this.transform.GetComponent<RectTransform>().sizeDelta[0]; | ||
mHeight = this.transform.GetComponent<RectTransform>().sizeDelta[1]; | ||
|
||
this.gameObject.SetActive(true); | ||
mInside = true; | ||
} | ||
} | ||
|
||
public void HideTooltip() | ||
{ | ||
//ScreenSpaceOverlay Tooltip | ||
if (mGUIMode == RenderMode.ScreenSpaceOverlay) | ||
{ | ||
mText.text = ""; | ||
this.gameObject.SetActive(false); | ||
mInside = false; | ||
} | ||
} | ||
|
||
void FixedUpdate() | ||
{ | ||
mCanvasWidth = transform.parent.GetComponent<RectTransform>().rect.width; | ||
mCanvasHeight = transform.parent.GetComponent<RectTransform>().rect.height; | ||
|
||
mWidth = this.transform.GetComponent<RectTransform>().sizeDelta[0]; | ||
mHeight = this.transform.GetComponent<RectTransform>().sizeDelta[1]; | ||
if (mInside) | ||
{ | ||
//ScreenSpaceOverlay Tooltip | ||
if (mGUIMode == RenderMode.ScreenSpaceOverlay) | ||
{ | ||
|
||
//Figure out which quad we are in | ||
//Determine X Shift | ||
if (Input.mousePosition.x > Screen.width / 2f) | ||
{ | ||
mXShift = mWidth / 2 + 1; | ||
|
||
|
||
//Determine Y Shift | ||
if (Input.mousePosition.y > Screen.height / 2f) | ||
{ | ||
|
||
//mBGTR; | ||
mCornerImage.anchorMin = new Vector2(1,1); | ||
mCornerImage.anchorMax = new Vector2(1,1); | ||
mCornerImage.localRotation = Quaternion.Euler(0, 0, 270); | ||
mCornerImage.anchoredPosition = Vector2.zero; | ||
mYShift = mHeight / 2 + 1; | ||
|
||
} | ||
else | ||
{ | ||
//mBGBR; | ||
mCornerImage.anchorMin = new Vector2(1, 0); | ||
mCornerImage.anchorMax = new Vector2(1, 0); | ||
mCornerImage.localRotation = Quaternion.Euler(0, 0, 180); | ||
mCornerImage.anchoredPosition = Vector2.zero; | ||
mYShift = -mHeight / 2 - 1; | ||
} | ||
|
||
} | ||
else | ||
{ | ||
mXShift = -mWidth / 2 - 1; | ||
//Determine Y Shift | ||
if (Input.mousePosition.y > Screen.height / 2f) | ||
{ | ||
//mBGTL; | ||
mCornerImage.anchorMin = new Vector2(0, 1); | ||
mCornerImage.anchorMax = new Vector2(0, 1); | ||
mCornerImage.localRotation = Quaternion.Euler(0, 0, 0); | ||
mCornerImage.anchoredPosition = Vector2.zero; | ||
mYShift = mHeight / 2 + 1; | ||
|
||
} | ||
else | ||
{ | ||
//mBGBL; | ||
mCornerImage.anchorMin = new Vector2(0, 0); | ||
mCornerImage.anchorMax = new Vector2(0, 0); | ||
mCornerImage.localRotation = Quaternion.Euler(0, 0, 90); | ||
mCornerImage.anchoredPosition = Vector2.zero; | ||
mYShift = -mHeight / 2 - 1; | ||
} | ||
} | ||
|
||
//TODO: So when scaling by a reference resolution, this gets messi. I need to scale with it somehow. | ||
|
||
if (mScaler != null) | ||
{ | ||
Debug.Log(mScaler.uiScaleMode + " " + mScaler.referenceResolution + " " + Screen.width + "," + Screen.height); | ||
//Get the different in our base res and the scaled res | ||
Vector2 screenSizeDifference = new Vector2(mScaler.referenceResolution.x - Screen.width, mScaler.referenceResolution.y - Screen.height); | ||
Debug.Log("Difference: " + screenSizeDifference); | ||
//newPos = new Vector3(newPos.x - screenSizeDifference.x, newPos.y - screenSizeDifference.y, 0); | ||
//Get the ratio? | ||
float ratio = Screen.width / mScaler.referenceResolution.x; | ||
mXShift *= ratio; | ||
mYShift *= ratio; | ||
//newPos = ratio; | ||
Debug.Log("ratio: " + ratio); | ||
} | ||
|
||
Vector3 newPos = Input.mousePosition - new Vector3(mXShift, mYShift, 0f); | ||
Debug.Log(Input.mousePosition + " - " + newPos + " " + mXShift + " "+ mWidth); | ||
|
||
this.transform.position = newPos; | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
using UnityEngine.UI; | ||
using UnityEngine.Events; | ||
using UnityEngine.EventSystems; | ||
|
||
[RequireComponent(typeof(EventTrigger))] | ||
public class TooltipTrigger : MonoBehaviour | ||
{ | ||
[SerializeField] | ||
string mDisplayText; | ||
|
||
// Use this for initialization | ||
void Start() | ||
{ | ||
EventTrigger trig = GetComponent<EventTrigger>(); | ||
AddPointerEnterTrigger(trig, EventTriggerType.PointerEnter); | ||
AddEventTrigger(trig, OnPointerExit, EventTriggerType.PointerExit); | ||
} | ||
|
||
private void AddPointerEnterTrigger(EventTrigger evTrig, EventTriggerType triggerType) | ||
{ | ||
EventTrigger.TriggerEvent trigger = new EventTrigger.TriggerEvent(); | ||
AddEventTrigger(evTrig, d => OnPointerEnter(d, evTrig.gameObject), EventTriggerType.PointerEnter); | ||
EventTrigger.Entry entry = new EventTrigger.Entry() { callback = trigger, eventID = triggerType }; | ||
evTrig.triggers.Add(entry); | ||
} | ||
|
||
private void AddEventTrigger(EventTrigger evTrig, UnityAction action, EventTriggerType triggerType) | ||
{ | ||
EventTrigger.TriggerEvent trigger = new EventTrigger.TriggerEvent(); | ||
trigger.AddListener((eventData) => action()); | ||
EventTrigger.Entry entry = new EventTrigger.Entry() { callback = trigger, eventID = triggerType }; | ||
evTrig.triggers.Add(entry); | ||
} | ||
|
||
|
||
private void AddEventTrigger(EventTrigger evTrig, UnityAction<BaseEventData> action, EventTriggerType triggerType) | ||
{ | ||
EventTrigger.TriggerEvent trigger = new EventTrigger.TriggerEvent(); | ||
trigger.AddListener((eventData) => action(eventData)); | ||
EventTrigger.Entry entry = new EventTrigger.Entry() { callback = trigger, eventID = triggerType }; | ||
evTrig.triggers.Add(entry); | ||
} | ||
|
||
private void OnPointerEnter(BaseEventData dataObject, GameObject hovered) | ||
{ | ||
if (hovered != null) | ||
{ | ||
Tooltip.Instance.SetTooltip(mDisplayText); | ||
} | ||
} | ||
|
||
private void OnPointerExit() | ||
{ | ||
Tooltip.Instance.HideTooltip(); | ||
} | ||
|
||
public void SetTooltip(string aText) | ||
{ | ||
mDisplayText = aText; | ||
} | ||
} |
Oops, something went wrong.