Game scripts or other custom code contains OnMouse_ event handlers. Presence of such handlers might impact performance on handheld devices
Hello, I wrote this custom script in order to enable my mobile application to use touch controls on a sprite and navigate to a next scene or page. The code I wrote below is uses a isTouched function to call the LoadScene method inside so that when I load my application, I can tap on the screen and it will load the next scene.
public class Next_lvl : MonoBehaviour {
public static Collider2D cd;
void Update () {
if (isTouched ()) {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
public bool isTouched(){
bool result = false;
if (Input.touchCount == 1) {
if (Input.touches [0].phase == TouchPhase.Ended) {
Vector3 wp = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);
Vector2 touchPos = new Vector2 (wp.x, wp.y);
if (cd == Physics2D.OverlapPoint (touchPos)) {
result = true;
}
}
}
return result;
}
}
However, this causes my application to have bugs in it, crashes and does allow me to navigate to any other pages when I test this on the phone. I keep getting this error that game scripts or other custom code contains OnMouse_ event handlers. The presence of such handlers might impact performance on handheld devices. I'm not sure why my code generates this error but is there anyway I can write a touch method that would allow me to tap on a sprite on my mobile device the go to another scene or call a function without causing errors on my phone.
Thank you.
Answer by Lysander · Jan 05, 2018 at 11:13 PM
Looks fine, and none of this shows any OnMouse_ events (such as OnMouseEnter, OnMouseExit, etc...), so it's probably referring to a different script. I would suggest a couple of things though- use a button instead of a Collider2D, as that's what they're made for. If you need non-rectangular buttons, just set this setting higher than 0 on the Image component the button is attached to.
Also, the use of scenes for something like "pages" may be inefficient (if that's like a "turning pages in a book" kind of situation)- possibly far more inefficient than any specific input methods on mobile. I would recommend looking into statemachines in a single scene, and enable and disable GameObjects as the state changes, if you have the memory for it.
Would you say creating a Save State and a Load State are the best ways of saving and loading the scenes in my application.
Answer by Kheremos · Feb 22 at 04:50 PM
Great answer above, but link has changed: https://docs.unity3d.com/ScriptReference/UI.Image-alphaHitTestMinimumThreshold.html One reference for the above broken link is: https://docs.unity3d.com/2018.1/Documentation/ScriptReference/UI.Image-alphaHitTestMinimumThreshold.html
Your answer
Follow this Question
Related Questions
How to get the touch controls to work smooth and properly in FPS android gamemobile Game? 1 Answer
Two touches not working as expected 0 Answers
TouchPhase.Began hold problem 0 Answers
Disable Input.getTouch in full screen 0 Answers
Touch Joystick not working properly with multiple touches 0 Answers