Search Unity

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

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

  1. gretty

    gretty

    Joined:
    Jun 20, 2012
    Posts:
    72
    Hello

    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

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    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.