Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Have a Cross-Platform App: 1 or More Input Handling Scripts?

Discussion in 'Scripting' started by gretty, Jun 26, 2014.

  1. gretty


    Jun 20, 2012

    I have a question for people who develop games that run on both Tablets (or Smart Phones) and Desktop.

    Do you write 2 different input handling scripts? Ie, do you write 1 to handle Mouse and Keyboard input (for desktop) and 1 for Touch Input (Tablets and Smart Phones)? Do you go even further? Like do you write 1 script for each operating system (to handle input differences between OS's).

    Or do you just write 1 input handling script that uses TouchScript or some other library? Can TouchScript detect normal desktop input though?

    I am looking for a way to write 1 Input Handling script that is platform independant. Any advice about techniques and libraries? Is it possible? How do you do it?
  2. TonyLi


    Apr 10, 2012
    Take a look at the scripts in Unity's free Sample Assets. They use different input-handling scripts for different platforms.

    Many developers also add a virtual controller, which is a layer of abstraction on top of the platform-dependent input script. Your higher-level code can read input from the virtual controller without worrying about the underlying input device.

    You can implement a virtual controller as a C# interface, but if you instead make it a separate script layer you can swap out the underlying input script on the fly -- for example to receive input from AI control, and then switch to player control -- and the higher-level code will work seamlessly with it. Brett Laming has written a lot about how Rockstar implements virtual controllers. See page 18 of this presentation for example.