- Home /
Question by
MSachs · Sep 06, 2018 at 09:22 AM ·
scripting problemtextclickmouseover
Make text appear and stay when clicking.
Hi,
I have a script which makes a custom text appear when hovering over an object using OnMouseOver and it disappears when I move the mouse away. Which is what I want.
But I also would like the text to stay visible if I click on the object. How would I do that?
This is my code for now to just show the text while the mouse is over the object..
{
public string myString;
public Text myText;
public float fadeTime;
public bool displayInfo;
bool mouseOver;
Color grey = new Color (0.65f, 0.65f, 0.65f, 1.0f);
void Start ()
{
myText.color = Color.clear;
}
void OnMouseOver()
{
mouseOver = true;
}
void OnMouseExit()
{
mouseOver = false;
}
public void Update()
{
FadeText ();
if (mouseOver) {
displayInfo = true;
}
else {
displayInfo = false;
}
}
void FadeText ()
{
if(displayInfo)
{
myText.text = myString;
myText.color = Color.Lerp (myText.color, grey, fadeTime * Time.deltaTime);
}
else
{
myText.color = Color.Lerp (myText.color, Color.clear, fadeTime * Time.deltaTime);
}
}
}
Thanks in advance :)
Comment
Best Answer
Answer by Hellium · Sep 06, 2018 at 10:00 AM
private bool selected;
void OnMouseDown()
{
selected = !selected;
}
public void Update()
{
displayInfo = mouseOver || selected;
FadeText ();
}