[Solved] When mouse on button image show up ?

Updated on October 24, 2016 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
9 on October 23, 2016

my canvas have two buttons and images. What i need is make the mouse showing an image when it enter the button without click on it.

//
public GameObject button1;
public GameObject button2;

public Image image1; // as wallpaper or background
public Image image2;

void Update() {

}

public void OnPointerEnter (PointerEventData eventData)
{
Debug.Log (“The cursor entered the selectable UI element.”);
}
//

  • Liked by
Reply
3 on October 23, 2016

So I’m pretty sure you can do all this in the editor which I think would be easier since you already have your buttons on the canvas.
 
Down where it says OnClick() and then you decide what you want to do, you can click on the plus sign (+) to add a new event. If I remember correctly, there should be an option like on pointer enter, and then you can change your button sprite from the series of selections. You’ll probably want to do the same for OnPointerExit to change it back too. But this way, you don’t have to create a script and for the buttons to do this. 
I wish I could show you via pictures, but my Unity is updating currently. Maybe if you need clarification I could show you more of what I mean in a bit…

That being said, if you really want to handle it with scripts, you’re well on your way. I’d just add something like this:

public void OnPointerEnter (PointerEventData eventData) 
{
button2.GetComponent<Button>().image= image2; // or at least something similar to this...
}

on October 23, 2016

Thank you for your reply.
Unfortunately. I can’t see this option ! also this code doesn’t work.

Devoted
on October 23, 2016

Here’s a step by step picture guide I made for changing the settings in the editor for you (and anyone else interested). The nice thing about this is that it doesn’t clutter up your game with excess scripts, and you can just copy and paste components/buttons to create future buttons/effects afterwards, but it’s personal preference on the method to choose.

on October 24, 2016

Thank you BinaryBeal. This is new to me. I was thinking on script but this good idea.

Show more replies
  • Liked by
Reply
Cancel