How to call function by releasing mouse over UI element.

I’m making this point and click type thing, and I want to:

  1. Click the screen.
  2. have a menu appear.
    3)drag the mouse over the button I want to press.
  3. release the mouse to execute a function.

So having the menu appear is not a problem, but I can’t figure out how to call a function by releasing the drag when the mouse is initially pressed in world space. The OnEndDrag() function and OnMouseUp() function seems to only work when the mouse button is initially pressed already inside of the element.

Any help is greatly appreciated!

Thanks.

I guess I could, I would only need to detect that I entered the gui, which should be no problem. I’m sorry if my question is too newbish, I’m a little new to this scripting business.

This is what I made, maybe that was also what you were thinking:

using UnityEngine.EventSystems;

public class buttonScript : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {

public bool _inside;

void Start () {
	_inside = false;
}

void Update () {
	if(Input.GetMouseButtonUp(0)){
		if(_inside == true){
			Debug.Log("Clicked");
			_inside = false;
		}
	}
}

public void OnPointerEnter(PointerEventData eventData){
	Debug.Log("Entered");
	_inside = true;
}
public void OnPointerExit(PointerEventData eventData){
	Debug.Log("Exited");
	_inside = false; 
}

}

It seems to work when I have it do stuff where it now debugs “clicked”. For some reason I don’t understand it only debugs “Entered” and “Exited” once, no matter how many times I enter and exit the gui. But I guess it doesn’t matter, since the jist of it works. It keeps switching on and off the bool _inside and such.

Thanks for the help!

Hello,
Woaw, your problem is really fun, I tried it and I just can’t make it work with a beautilful and clean method.

Actually, OnMouse functions work only in the world, they don’t work with UI elements, that have their own methods. And even with these Handler methods, I couldn’t make the Up work the way you want.

The approach I used to make it work is to give the selected button to its parent, and its parent has the controller that checks the mouse state with an Update function.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CustomButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
	public void OnPointerEnter (PointerEventData eventData) 
	{
		Debug.Log ("cursor entered");
		transform.parent.GetComponent<CustomPanel> ().selectedButton = this;
	}

	public void OnPointerExit (PointerEventData eventData) 
	{
		Debug.Log ("cursor exited");
            transform.parent.GetComponent<CustomPanel> ().selectedButton = null;
	}
}

And the parent :

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CustomPanel : MonoBehaviour
{
	public CustomButton selectedButton;

	void Update()
	{
		if (Input.GetMouseButtonUp (0) && selectedButton != null) 
		{
			Debug.Log ("click on button " + selectedButton.name);
		}
	}
}

But it’s not really great to create an Update function for something that is supposed to work with events… =/

I’ll update this answer if I find another trick.