Touch and MouseClick - One Game for Mobile and Desktop

Hi,

How can I get mouse clicks and touch position in a function when I am building for both Mobile and Desktop from the same build. From what I understand, mouse functions do work with mobile but it is better to use Touch.

So is there a way for me to make sure Touch and MouseClick both get’s used without having to code it twice platform dependent? I wouldn’t be requiring multi touch as far as I am concerned for now (if I do require then I will code once for mobile and once for desktop (use keyboard input on desktop)).

Hello,
you can use Unity Plataform Dependent Complitation, and mark whick lines of codes will run on each plataform (Mobile, PC).

A good alternative solution would be to implement your own wrapper to handle the input. This way, you wouldn’t have to worry about error-prone code in your main scripts and it would be easier to debug.

You can have a setup method inside the wrapper that handles which function to call when, say, InputWrapper.Interaction(x,y) is called and either the screen touch or the mouse click has to be invoked. You could use Delegates and Events to decide what kind of Input to use, or a simple combination of if-else (or switches), but the basic idea is that it’s this wrapper’s job to decide which input method to use.

This way, you can also implement your own variants of different inputs (gamepads, joysticks, voice recognition…), the possibilities are endless.

you can use interfaces and implement those methods they work in all platforms ive tried it
https://docs.unity3d.com/ScriptReference/EventSystems.IBeginDragHandler.html