Our game works great with touch or mouse, but the Surface pen doesn't seem to register as a pointing device at all. How can we support the pen in Unity in a native build?

This question on the Unity Answers site suggests we could build it as a UWP app, but that means restricting ourselves to a very limited subset of the .NET framework (specifically, we lose a lot of the Serialization libraries).

  • \$\begingroup\$ Can you just treat it as touch input? \$\endgroup\$ – Evorlor May 22 '17 at 12:56
  • \$\begingroup\$ @Everlor, it doesn't register to Unity as touch input. If you know how to make it do so, that would be awesome. \$\endgroup\$ – Gregory Avery-Weir May 22 '17 at 20:03
  • \$\begingroup\$ @ArcaneEngineer I'm not sure; at the moment (1) is looking very tricky and I'm not sure how to do (2) at all. Supporting one of an OS's built-in input systems doesn't seem like it should be particularly difficult, but I'm unable to find resources on it at all. \$\endgroup\$ – Gregory Avery-Weir May 25 '17 at 18:18
  • 2
    \$\begingroup\$ I've found no answers elsewhere, like you. For (2), you'd need a DLL called by your Unity app, or a Windows service (or Win application if a service won't do the job) running alongside your app. You'd need to grab the inputs in that service/app, from the appropriate Microsoft API (Pen, I think), and route that data to your Unity application via IPC. DLL hooking into Unity direct looks easier. But could it get access to the API in question? - I'm not sure. \$\endgroup\$ – Engineer May 30 '17 at 10:08
  • 1

On my SP3 the pen works with https://docs.unity3d.com/ScriptReference/Input-mousePosition.html and returns a Vector3 with x, y and 0z co-ordinates. I'm using Unity 5.6 on Linux but if the pen doesn't show up as touch input in Windows I would assume it has the same behavior.

| improve this answer | |
  • 3
    \$\begingroup\$ This is a comment, not an answer to the question which is, "How can we support the pen in Unity in a native build?". \$\endgroup\$ – Engineer Jun 3 '17 at 9:11
  • 1
    \$\begingroup\$ I don't have enough rep to comment on the question and this does work. I tested this again with a SP3 with Windows 10 and Unity 5.2 and it was correctly reporting the location of the surface pen as if it was a mouse. \$\endgroup\$ – user3362964 Jun 3 '17 at 18:57
  • \$\begingroup\$ Awesome! Pen position does indeed get reported, but I didn't realize it because pen taps don't register as mouse clicks. Do you have any insight as to how to recognize pen taps as mouse clicks (or any sort of input)? \$\endgroup\$ – Gregory Avery-Weir Jun 3 '17 at 19:54
  • 1
    \$\begingroup\$ You could maybe do something hacky with detecting when the pen stops moving but I haven't found anything in the Unity input api that properly detects the taps. I think you would have to use one of @Arcane Engineer's suggestions and use something external that passes the data into your game. \$\endgroup\$ – user3362964 Jun 4 '17 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.