Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

tvOS Input Code?

Discussion in 'Scripting' started by ExbowFTW, Jan 13, 2016.

  1. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    I just updated to 5.3.1f1 Personal, and there is a tvOS Build option. However, I cannot find a Unity TVOS Documentation online on how to access the tvOS Remote's touchpad and take it as an input. Like, for example,:
    Code (csharp):
    1.  
    2. if(input.GetTouchscreen.Rightside) { //if the right side of the touchscreen of the tvos remote is touched     BallTurnsRight();
    3. }
    4.  
    How do I do this?
     
  2. Mikea15

    Mikea15

    Joined:
    Sep 20, 2012
    Posts:
    93
    I'd also like more support on this, as in, Documentation please! :)
     
  3. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    I know right!
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,083
    I'd recommend a few things:

    Use the 'InControl' plugin - it has configurations for the apple tv remote

    You can query the raw position of the touchscreen using this (without the plugin):

    Code (CSharp):
    1.  
    2. UnityEngine.Apple.TV.Remote.reportAbsoluteDpadValues = true;
    3.  
    4. float m_PadLR = Input.GetAxisRaw("Horizontal");
    5. float m_PadUD = Input.GetAxisRaw("Vertical");
    This gives you values from -1 to +1 which maps across the touchscreen.

    InControl lets you query button presses too, for example:

    Code (CSharp):
    1. InputDevice CurDevice = InputManager.ActiveDevice;
    2. float PlayPauseButton = CurDevice.GetControl( InControl.InputControlType.Action3 ).Value
    3. float TouchScreenButton = CurDevice.GetControl( InControl.InputControlType.Action1 ).Value
    4. float MenuBackButton = CurDevice.GetControl( InControl.InputControlType.Pause ).Value
    You can do it without the plugin too, Action3 is Unity Button 15, Action 1 is Unity Button 14 and Pause is Unity Button 0

    If you want to you can also use Input.touches like you would on iOS but it works differently. When you put your finger down that point becomes 0,0 and everything is relative to it which makes life more difficult. It also returns values in screen resolution rather than a normalised viewport.
     
  5. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Wow thanks! So could I do:
    Code (csharp):
    1.  
    2. If(Input.GetAxisRaw("Horizontal")) > 0) {
    3. //Go to the right
    4. }
    5. If(Input.GetAxisRaw("Horizontal")) < 0) {
    6. //Go to the left
    7. }
    8.  
    Also, is the InControl Plugin on the Unity Asset Store?
     
  6. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    Rewired also supports the Apple TV Siri remote. It allows you to get the touchpad as a joystick and also allows you to map tilt and rotation to Actions. It also supports all MFI game controllers on tvOS and makes handling multiple players easy. You can try a free demo of Rewired and see if it works for you.
     
    Last edited: Jan 25, 2016
  7. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,083
    @ExbowFTW - your code looks fine to me, that should work ok. InControl is on the asset store as far as I remember, there's also an older GitHub version but I don't think it has the up to date profiles for all controllers (and apple tv)
     
  8. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    O
    Ok thanks.
     
  9. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Thanks. Once the trial runs out, does it no longer work?
     
  10. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    The trial never runs out. The only limitation is a 2-minute time limit per-play session. Once you press stop in the editor and Play again, you have 2 more minutes.

    Note: The download link to the trial is currently down because of unexpected server modifications by my webhost that I cannot fix. I've contacted support to get this resolved.
     
  11. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Okay, thanks for your help!
     
  12. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    Sorry for being late to this thread. Rewired and Incontrol are both great options and if you are already using one I'm sure you are happy.

    Since you originally asked about the "sides" of the controller @tonemcbride responses for the native Unity way of doing things without a plugin is also correct. You could set any threshold you wanted for "left/right/up/down side" like less than -.75 or greater than .75 if you only wanted it to be the edges.

    There is one additional asset in my signature below called Easy Input Helper that might also be able to help. It's specific to the Apple TV not general purpose like Rewired and Incontrol so if you are cross platform this might be a hindrance. However, if you are making a Apple TV specific title it will help with some of the common things with no coding required. Things like if you wanted a touch dpad or joystick, wanted a marble type game with the accelerometer, wanted first person controls with the siri remote with the touchpad and gyro, Unity GUI menu swipe navigation, etc. It essentially if you are targeting Apple TV only has coded most of the common things for you already so all you have to do is change a few sliders in the inspector for dead zones, swipe length, etc. and it will work. Naturally, if you want to do something custom you can use our API to extend functionality with coding, but many tasks are already done for you.
     
  13. Gord10

    Gord10

    Joined:
    Mar 27, 2013
    Posts:
    142
    I couldn't get a value different than 0 in
    Code (csharp):
    1.  
    2. print(Input.GetAxisRaw("Vertical").ToString());
    3.  
    What am I doing wrong? I am sure touch is read, I press alt button when I move my mouse in simulator and touchCount is 1.

    Also,

    Code (csharp):
    1.  
    2.   if(Input.GetKeyDown(KeyCode.JoystickButton14))
    3. {
    4. SceneManager.LoadScene("Menu");
    5. }
    6.  
    doesn't work, neither.
     
    Last edited: Feb 5, 2016
  14. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    When you aren't using any plugins and are just using regular Unity Input you need to make sure to have the following line appear before your call to getaxis or getaxisraw if you want it to behave like a joystick and not be swipe based (default is swipe based so just touching won't change from 0 only dragging will).

    Code (CSharp):
    1. UnityEngine.Apple.TV.Remote.reportAbsoluteDpadValues = true;
    As far as the button press goes that code should work. Where are you executing this, on a physical device? If by any chance you are using the simulator unity doesn't support the Siri Remote in editor or the simulator.
     
  15. Gord10

    Gord10

    Joined:
    Mar 27, 2013
    Posts:
    142
    Ah, I see. Yes, I was working on simulator. Thank you for the answer.
     
  16. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
  17. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
  18. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Oh dang that was fast ;)

    Is there a Apple tvOS - Unity Documentation where you get this information?
    Also, how would I code it? Just:
    Code (csharp):
    1.  
    2. if(getbuttondown(Button15)) {
    3. //PAUSE THE GAME
    4. }
    5.  
    ?

    THANKS
     
  19. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Also, without using any special unity plugins, does Input.Acceleration work on the tvOS Remote (like, is there a gyrometer on it) ?
     
  20. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    Some of it comes from the beta group. I don't see where they have published any docs on AppleTV. Here are some docs on input in iOS.

    No. This follows all the standard rules for using UnityEngine.Input. Documentation.

    Assuming the Siri Remote is assigned to be Joystick 1 by Unity:

    Code (csharp):
    1. if(Input.GetButtonDown(KeyCode.Joystick1Button15)) {
    2.     // Pause the game
    3. }
     
  21. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    Accelerometer, yes. Gyroscope, no. You can get acceleration, but orientation is only a rough estimate based on the gravity vector. It is not accurate enough to be used as a Wii-like pointing device for example.
     
  22. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    This is mostly correct but not entirely. There actually is both a gyroscope and an accelerometer in the siri remote but no magnometer. Also, on the native side the Apple API doesn't actually expose either raw value it tries to fuse the two and exposes them as userAcceleration and Gravity.

    Unity still supports both APIs so Input.Acceleration and Input.Gyro.UserAcceleration and Input.Gyro.Gravity both work but its important to note these are really returning the same data and neither is really the accelerometer or the gyro but rather a fusion of both.

    Therefore:
    Input.Acceleration = Input.Gyro.UserAcceleration + Input.Gyro.Gravity on the Apple TV and aren't unique.

    It's important to note because there is no magnometer that Input.Gyro.Attitude and Input.Gyro.RotationRate like your used to on mobile do not return valid information on the axis parallel with gravity. This is because a phone also fuses this data with magnometer but there is none present on the siri remote to do that with. So that means this data is unreliable (usually the y axis parallel with gravity unless you hold the remote at a different angle).

    The other two axis you can get the orientation fine based on the gravity vector and Unity does try to return this info in the API just know to not count on the data as being 100% accurate only trust the info that is for axes not parallel with gravity.

    Not that I know of for TVOS and even some of the other platforms. If you ever encounter lack of documentation just download this free utility from the assetstore and just press buttons on device and it will let you know for sure what they are. Some of the other platforms the documentation is out of date and wrong as well so never hurts to double check.

    https://www.assetstore.unity3d.com/en/#!/content/43621
     
    Last edited: Feb 26, 2016
  23. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    It was my impression that the gyroscope was not used in the calculation of the gravity vector or the user acceleration exposed by Apple's API.

    See this message:

    https://forums.developer.apple.com/message/58363#58748
     
    Last edited: Feb 26, 2016
  24. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    That is not correct (people were speculating at the time). Here is a thread with Apple staff in it.

    https://forums.developer.apple.com/message/70704#70704

    Also if you look at the telemetry closely you can verify the fusion. Swing the remote left/right a couple of times then stop suddenly and hold it still (easiest if you stop where the gravity vectors should be 0 and 1). You will notice that at first the gravity vector is off by about 20 degrees from all the sudden motion and then about 2-3 seconds later while still holding the remote still it will in one frame do a full correction and then gravity vector is correct again.

    You would only see this behavior in a fusion environment an accelerometer doesn't drift, but is noisy. A gyro does drift but wouldn't correct itself if it didn't get data from the accelerometer.
     
  25. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    Thanks for that link. That does clear things up. Sadly, the rotation rate is not accessible still.
     
  26. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    In the Unity API for the other two axes or do you mean in the native Apple API?

    If you mean the native Apple API I don't think we'll ever see it this generation because of the decision to not put a magnometer in the remote. They don't seem to want to expose not totally reliable data even though 2 out of the 3 axis you can get from gravity. One can hope that in the next version of the Apple TV they will add the sensor, these things are dirt cheap no reason not to.

    If you mean the Unity API I must admit I haven't checked in awhile since writing Easy Input Helper after calculating it myself with some smoothing. It's fairly straightforward just (thisFrameOrientation - lastFrameOrientation) * Time.deltatime .
     
  27. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    Yes, I was referring to Apple's API.
     
  28. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    Yeah, I hope that the 5th generation Apple TV siri remote isn't as short sided and includes a magnometer.

    BTW, Congrats on getting featured in the current sale. It's always a nice boost wehn that happens. Only a little over a month until the valve VR controllers come out and we get to have more controller fun! I didn't get selected for a dev kit this time around so unfortunately I won't be able to launch at the same time as platform release this time :(.
     
    Last edited: Feb 26, 2016
  29. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    Thanks! All the new gear coming out will be exciting.
     
  30. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Okay, thanks. I put that code in my game... will test to see if it works soon - XCode has an Apple TV Simulator, right, where I can test to make sure all the controls are working smoothly and correctly?
     
  31. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    Xcode does, but Unity does not currently support the remote on the Mac. You will have to deploy to the device currently if you are using straight Unity without Asset Store plugins. Unity did say it's on their roadmap but don't know when they are targeting.

    Easy Input Helper does provide keyboard/mouse emulation of the siri remote in editor. @guavaman Rewired might support it on Mac. Augie, have you tried the siri remote in editor or the tvos simulator on a Mac in Rewired? I've lost track at which platforms you are native on and which you are just falling back to Unity under the covers.

    In either case I believe you said you weren't looking to buy anything and definitely in default Unity you have to currently use the physical Apple TV.
     
  32. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    OSX is native, but the Siri Remote is not supported. I was unable to pair it with the Mac the last time I tested. I may revisit this at some point.
     
  33. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    So, you mean the game would show up and stuff, but i wouldn't actually be able to control it?
     
  34. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    That is correct. On the Mac you can pair the Siri Remote but Unity does not support it currently on the Mac. So you will in effect not be able to use the Siri Remote to control your game when in editor and also in the TVOS simulator. You can currently only use the Siri Remote on the physical Apple TV when using Unity.

    That being said if you setup your Input Manager correctly you can control it some basic ways with mouse/keyboard in editor (not the simulator) but to actually use the Siri Remote will require the Apple TV.
     
  35. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Hmm I guess. Thanks!!!
     
  36. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    For some reason,
    Code (csharp):
    1.  
    2. if(Input.GetButtonDown(KeyCode.Joystick1Button15)) {
    3.  Pause ();
    4.  }
    5.  
    isn't working. It gives me this error:
    "Assets/Scripts/PauseButton.cs(42,26): error CS1503: Argument `#1' cannot convert `UnityEngine.KeyCode' expression to type `string'"
     
  37. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    If you want to use the keycode use GetKeyDown not GetButtonDown. If you want to use GetButtonDown like above create an entry under ProjectSettings -> Input and then pass the name you added there as the string in the above function call.

    Either way will work but you are confusing and mixing the two ways of doing it.
     
  38. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    It was a typo, sorry. It should have been GetKeyDown. Please read Unity's input documentation. It has all the information you need to know how to use UnityEngine.Input. I was never intending for you to copy and paste the untested code.

    Manual: Input
    Scripting Reference: UnityEngine.Input
     
  39. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Ohh okay, thanks guys! That makes sense xD
     
  40. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Also, I tried running this code on my computer on the Unity Editor. Once I ran the code, it automatically sent the message TVClicked...
    Code (csharp):
    1.  
    2. void Start () {
    3.  UnityEngine.Apple.TV.Remote.reportAbsoluteDpadValues = true;
    4.  GameObject.Find ("LoadingText").SetActive (false);
    5.  }
    6.  
    7.  //Updateiscalledonceper frame
    8.  void Update () {
    9.  if (Input.GetAxisRaw ("Horizontal") > 0) {
    10.  GameObject.Find ("PlayButton").SendMessage ("TVClicked");
    11.  }
    12.  if (Input.GetAxisRaw ("Horizontal") < 0) {
    13.  GameObject.Find ("PlayButton").SendMessage ("TVClicked");
    14.  }
    15.  if (Input.GetAxisRaw ("Horizontal") == 0) {
    16.  GameObject.Find ("PlayButton").SendMessage ("TVClicked");
    17.  }
    18.  }
    19.  
    This code is supposed to wait for the user to click anywhere on the TVOS Remote, and then send TVClicked.
    I searched up about Input.GetAxisRaw, but it was all very confusing. Is it not working properly because I'm using a computer mouse, or am I doing something wrong in my code?

    Thanks!!!
     
  41. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    There are several errors in the above code and not all with just the Apple TV aspects, some with general programming aspects. Just glancing at this quickly the following seem wrong to me.

    General:
    1. The Above code should always send TVClicked because it will always be one of those 3. There is no other way it could possibly be. If you are not touching it will be 0, If you are touching it will be some decimal between 0 and 1.
    2. Generally you shouldn't use == with floats (although on 0 it's probably fine). Floats are approximations and if you get in the habit of using == bad things will happen
    3. GetAxisRaw is a float and you are comparing to an int. 0 is an int and 0f is the float equivalent. In general you should line up the datatypes

    AppleTV specific:
    4. The click is not the same as just touching. The code above is for just touching. If you want the click check button 14.
    5. The siri remote pad for touching (not clicking) in the code above will be 0 most of the time even when not touching. In general if you wanted to check for not touching I would use the touch Input API unity uses and once you determine you are touching then check the value of the axis.
     
  42. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Ohhhhhh! If you look up a picture of "Apple TV Remote" there are only 5 buttons. Which button does "Button 14" apply to?

    Thanks!
     
  43. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    The click of the touchpad is button 14. In Unity there on the siri remote there are actually only 3 available buttons.

    Touchpad click - button 14
    play/pause - button 15
    menu - button 0

    The rest are reserved by apple, like the volume buttons, siri button, etc.
     
  44. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Oh okay, thanks. This is a really good reference for me to use ;)
     
  45. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Is this error anything to be concerned about? (I'm still running this on Unity Editor on Computer):
    MissingMethodException: Cannot find the requested method.
    SideToSideControls.Start () (at Assets/Scripts/SideToSideControls.cs:15)

    It is coming from this line of code:
    UnityEngine.Apple.TV.Remote.reportAbsoluteDpadValues = true;
     
  46. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    That call is TVOS specific so in editor (not TVOS platform) it doesn't have an idea what that call is. I would think Unity should probably be able to recognize this themselves and ignore it, but if your getting an error I guess they didn't do this.

    Simply add the following around your the above line and the error should go away.
    Code (CSharp):
    1. #if !UNITY_EDITOR && UNITY_TVOS
    2.             UnityEngine.Apple.TV.Remote.reportAbsoluteDpadValues = true;
    3. #endif
     
  47. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Ah okay, thanks.
     
  48. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
  49. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    First off, iAD is being discontinued.

    http://www.immortal.org/23024/apple-iad-app-network-closing/

    Secondly, assuming you are talking about the Apple TV and not iphone/ipad, the Apple TV doesn't have a browser so there isn't a way to use the competitors like admob, unity ads, etc. at the moment. Perhaps they'll add one eventually, but not currently. If you want to launch for free and get revenue as you go, you'll have to use in app purchases on TVOS at the moment, or just charge for your game.
     
  50. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Oh. But will Apple iAds work for iOS Apple Store apps?