fungus

Open full view…

Change sprite on hover and revert back on exit?

Pillar Angel
Wed, 24 Feb 2016 23:09:28 GMT

Like hover cursor, is there a way to change a sprite, specifically clickable sprites, to change the sprite when the mouse hovers over it and reverts back when the mouse stops hovering over it? If this is not possible through fungus or code, then I can go by with changing the color tint of the sprite instead on mouse hover. Been trying to figure out how that would be written, but I'm kinda stumped as usual when it comes to code haha. --- using UnityEngine; using System.Collections; public class HoverColor : MonoBehaviour { public virtual void OnMouseEnter() { SpriteRenderer renderer = gameObject.GetComponent<SpriteRenderer> (); renderer.color = new Color(240f, 234f, 30f, 255f); } public virtual void OnMouseExit() { SpriteRenderer renderer = gameObject.GetComponent<SpriteRenderer> (); renderer.color = new Color(255f, 255f, 255f, 255f); } } ---

Pillar Angel
Thu, 25 Feb 2016 00:13:38 GMT

Okay back to the changing sprite thing, I managed to make it work with this. --- using UnityEngine; using System.Collections; public class HoverTextureChange : MonoBehaviour { public Sprite originalTexture; public Sprite hoverTexture; public virtual void OnMouseOver() { transform.GetComponent<SpriteRenderer> ().sprite = hoverTexture; } public virtual void OnMouseExit() { transform.GetComponent<SpriteRenderer> ().sprite = originalTexture; } } --- Now my question is, is there a way to set it so the original texture the sprite renderer was originally set with shows up on mouse exit automatically (as in, not having to set the texture twice)? This isn't a huge gripe, but just wondering to save some time.

Pillar Angel
Thu, 25 Feb 2016 05:17:43 GMT

Managed to make the sprite thing work by setting originalTexture using the Start() method. Now I just need an answer about the color cause I'm still not quite sure how to control the color tint of the sprite.

chrisgregan
Thu, 25 Feb 2016 22:16:17 GMT

That looks like the correct way to change the color of a sprite renderer. Does it have no effect when you try it? One other suggestion would be to have a child sprite with a glow and then switch that on/off depending on the hover state. You can use the order in layer property to make the glow sprite appear in front of the clickable sprite.

Pillar Angel
Thu, 25 Feb 2016 22:36:34 GMT

Yeah for some reason nothing happens. / I even tried correcting the OnMouseOver() method since originally I had it as Enter instead of Over. The glow idea is a really good idea though! I can increase the scale to make it look like the an item is highlighted when the player mouses over it. Thanks! :D

Pillar Angel
Thu, 03 Mar 2016 03:54:16 GMT

For anyone wondering, I did some more searching and this seems to work in regards to changing the color of the SpriteRenderer. --- GetComponent<SpriteRenderer>().color = Color.yellow; --- Guess the previous method is obsolete. :/

alanmattano
Sat, 18 May 2019 14:13:34 GMT

I think is a Unity bug. SOLUTION: OnPointerExit or OnMouseExit, try to disable and enable the Button component script.