Handheld device friendly script

I have received the warning
"Game scripts or other custom code contains OnMouse_ event handlers. Presence of such handlers might impact performance on handheld devices.

UnityEditor.HostView:OnGUI()".
Is there a way to make the script attached below perform better on handheld devices? I am currently using the OnMouseDown function.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class pressarrow : MonoBehaviour {

    public GameObject box;


    void Start () {
        box.SetActive (false);
    }
   

    void OnMouseDown () {

        box.SetActive (true);


    }
}

This is because Unity event OnMouseDown(and all the OnMouseSomething events) can cause lag if you have a lot of them acting at the same time. You could fix this using something like a Raycast or similar. I hardly ever use this events, so I can’t tell you a lot more about this… but you could search for info at unity’s tutorials:
OnMouseDown:Unity - Scripting API: MonoBehaviour.OnMouseDown()
Raycast:Unity - Scripting API: Physics.Raycast

1 Like

This will work in the same way as OnMouseDown and OnMouseUp.

    public class Example : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
        // This is the OnMouseDown equivalent.
        public void OnPointerDown(PointerEventData eventData)
        {
            Debug.Log("Pointer was just down on this object");
        }

        // This is the OnMouseUp equivalent.
        public void OnPointerUp(PointerEventData eventData)
        {
            Debug.Log("Pointer was just up on this object");
        }
    }
1 Like

So all I had to do was replace OnMouseDown with OnPointerDown.

To be exact, OnMouseDown isn’t too bad, but it’s performance heavy when used on mobile and will come up with a yellow warning in the debug log. Some mobile devices once did not work with OnMouseDown(). I’m not sure whether that’s still the case.

1 Like

But just to be on the safe side I’m guessing it would be better to use the OnPointerDown function.

1 Like

Yes, definitely.

1 Like

Just not worth using the OnMouseDown function as it looks through every script for a box collider. Handheld devices would struggle to cope.