4.6 New UI: MouseOver event/function needed

I’m quite new, and fear I’m a bit lost.

I need an OnMouseOver function/event in order to determine when a script/function can be run on a certain UI element such as a panel. I’ve tried PointerEnter/Exit events, and while they do work to a certain extent, I really need something that runs every frame In order to detect if certain conditions are met.

I’ve attempted to use a script with an OnMouseOver function attached to a UI panel, but it doesn’t seem to work. The same script works fine with a basic cube object. I placed a 2D box collider on the panel, resized it to fit, but the script still did not work.

I could probably manage with an PointerOver event, but I can’t seem to find one.

Thank you in advance!

Ps. I’m attempting to get multiple UI “windows” with the same or similar scripts to work well with each other. They all check where the cursor currently is, and act upon its positioning. The problem comes when their positions overlap. Again, the PointerEnter/Exit events allow me to control which object/script runs to an extent, but without the events running until conditions change, they don’t allow me full control.

If you have the PointerEnter and the PointerExit then just use an update or a coroutine:

bool isInside = false;

void OnPointerEnter(ped)
{
   isInside = true;
}
void OnPointerExit(ped)
{
   isInside = false;
}

void Udpate(){
  if(isInside){}
}

with a coroutine:

bool isInside = false;

void OnPointerEnter(ped)
{
   if(isInside == false)
   {
       StartCoroutine("UpdateCoroutine");
   }
}
void OnPointerExit(ped)
{
   StopCoroutine("UpdateCoroutine");
   isInside = false;
}

IEnumerator UpdateCoroutine(){
  isInside = true;
  // do your stuff
  yield return null;
}

Note that there might be an event from UnityEngine.EventSystem that handles this but I can recall…neither can I recall the exact type of the paramaters…PointerDataEvent I think it is

  1. Add a collision rect to your UI element
  2. Create a component
  3. In the component’s Update function, do a Physics2D raycast and detect whether you get a collision with your UI element (possibly by checking against its name).

For anyone still interested, I actually ended up creating my own “Mouse Over” style function that works for UI objects using the default canvas settings. The following is a streamlined version.

First, to detect when the cursor is over the scripted UI object…

	void Update ()
	{
		RectTransform objectRectTransform = gameObject.GetComponent<RectTransform> ();				// This section gets the RectTransform information from this object. Height and width are stored in variables. The borders of the object are also defined
		float width = objectRectTransform.rect.width;
		float height = objectRectTransform.rect.height;
		float rightOuterBorder = (width * .5f);
		float leftOuterBorder = (width * -.5f);
		float topOuterBorder = (height * .5f);
		float bottomOuterBorder = (height * -.5f);

        // The following line determines if the cursor is on the object
		if(Input.mousePosition.x <= (transform.position.x + rightOuterBorder) && Input.mousePosition.x >= (transform.position.x + leftOuterBorder) && Input.mousePosition.y <= (transform.position.y + topOuterBorder) && Input.mousePosition.y >= (transform.position.y + bottomOuterBorder))
		{
			PerformRaycast ();								// Calls the function to perform a raycast
		}
	}

Then, use a graphic raycast to determine if the scripted object is actually the fore-most UI object where the cursor is positioned…

	void PerformRaycast ()				// This function performs a raycast to detect if this object is in front of other objects if any overlap at the cursor's position when the mouse button is initially pressed
	{
		PointerEventData cursor = new PointerEventData(EventSystem.current);							// This section prepares a list for all objects hit with the raycast
		cursor.position = Input.mousePosition;
		List<RaycastResult> objectsHit = new List<RaycastResult> ();
		EventSystem.current.RaycastAll(cursor, objectsHit);
		int count = objectsHit.Count;
		int x = 0;


		if(objectsHit[x].gameObject == this.gameObject) 										
		{	
			// This section runs only if this object is the front object where the cursor is
		}
	}

Using more comprehensive versions of the above code, I’ve created an entire package of scripts that is available on the asset store. All scripts are fully commented, and there are many example scenes included.

Move & Resize UI on the Asset Store


Version 3.0 Webplayer Demo and Full Readme

Move/Resize Webplayer Demo

Scale/Anchor Webplayer Demo


I do hope this helps! Good luck!

Another way to accomplish this is to use Animation Events. Set as Transition for the button an animation. Then in the “Highlighted Trigger” animation, add an animation event which calls your desired method. No programming required.