Hey @Funderful,
A simple solution would be this, using OnMouseEnter and Exit like you suggested:
Simple Package:
https://www.dropbox.com/s/jptfcmrky3wwjpb/InspectableSimple.unitypackage?dl=0
Visually:
https://gyazo.com/9bd1503a77ace25a7d04cfe500e990c6
Code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class InspectableSimple : MonoBehaviour {
public RectTransform m_tooltipCanvas;
public Text m_tooltipText;
private Transform m_transform;
private CharacterController m_player;
void Start()
{
m_player = FindObjectOfType(); // Note that this object is not the player.
m_transform = transform;
m_tooltipText.text = string.Format( "Hi, my name is {0}", this.name );
}
void Update()
{
// Since a canvas' forward is turned away from the camera by default, we'll make the canvas look in the opposite direction.
Vector3 inverseDirection = m_tooltipCanvas.position - m_player.transform.position;
m_tooltipCanvas.rotation = Quaternion.LookRotation( inverseDirection );
}
void OnMouseEnter()
{
_setTooltip( true );
}
void OnMouseExit()
{
_setTooltip( false );
}
private void _setTooltip(bool visible)
{
m_tooltipCanvas.gameObject.SetActive( visible );
}
}
Note however, that this can cause some problems when the inspectable object doesn't have a "Uniform" (1,1,1) scale, because the TooltipCanvas will deform.
Instead you can try the following (Has 2 classes, Inspectable and ToolTipHandler )
Package:
https://www.dropbox.com/s/ckjzmf7l3t51jmr/InspectableScaleProof.unitypackage?dl=0
Notice that the tooltip is not deformed for either of the 3 cubes you can look at.
(Preview image below)
![Preview of the scaleproof setup][1]
The code for the scaleproof variant:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Inspectable : MonoBehaviour
{
public Transform m_tooltTipShowTransform;
private Transform m_transform;
private CharacterController m_player;
private Transform m_currentTooltip;
void Start()
{
m_player = FindObjectOfType(); // Note that this object is not the player.
m_transform = transform;
}
void Update()
{
if ( m_currentTooltip != null )
{
// Since a canvas' forward is turned away from the camera by default, we'll make the canvas look in the opposite direction.
Vector3 inverseDirection = m_currentTooltip.position - m_player.transform.position;
m_currentTooltip.rotation = Quaternion.LookRotation( inverseDirection );
}
}
void OnMouseEnter()
{
_setTooltip( true );
}
void OnMouseExit()
{
_setTooltip( false );
}
private void _setTooltip( bool visible )
{
if( visible )
{
m_currentTooltip = ToolTipHandler.Instance.GetTooltip( m_tooltTipShowTransform.position, _getTextToDisplay() ).transform;
}
else if (m_currentTooltip != null)
{
Destroy( m_currentTooltip.gameObject );
}
}
private string _getTextToDisplay()
{
return string.Format( "Hi, my name is {0}", this.name );
}
}
Tooltip Pool-like class:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ToolTipHandler : MonoBehaviour {
public static ToolTipHandler Instance;
void Awake() { Instance = this; }
public GameObject m_tooltipPrefab;
public GameObject GetTooltip( Vector3 position, string displayText )
{
GameObject newTooltip = (GameObject) Instantiate( m_tooltipPrefab, position, Quaternion.identity );
newTooltip.GetComponentInChildren().text = displayText;
return newTooltip;
}
}
To improve this setup:
1. Instead of using OnMouseEnter and OnMouseExit (if those don't work out for you), use **Physics.Raycast**
2. Instead of creating an destroying the tooltip everytime, you could apply the **Instance Pool Pattern**, to improve performance on larger scales.
I hope this answer helps you out! Best of luck in school and with that deadline, you can do it :)
If you need anything else, let me know.
Also, if this helped, please accept this answer, it'd be much appreciated!
Cheers,
ThePersister
[1]: /storage/temp/82344-inspectablescaleproofscreenshot.png
↧