Unity InputManager

24

Comments

  • gribblygribbly Posts: 80Member
    edited January 2013
    Tim I could use a little more hand-holding on this... I've been developing my game using the standard Unity input stuff, and as of tonight got it up and running on the dev kit. I did this by importing OuyaSDK.unitypackage into my existing project.

    Everything went pretty smoothly, except I have no input - I'm not getting any events. Which makes sense as I haven't done anything to set it up!

    What I want to do is get get the Unity controller talking to my existing input code as simply as possible. I'm getting a little confused with the various wrappers/managers, so I'm not sure what is the minimal set of changes I can make to get things working.

    My primary goal is evaluating perf on Ouya, so I'm trying not to get to caught up in re-writing my input stuff right now (I don't mind doing it later if necessary).

    tl;dr: What's the easiest way to simply pipe the Ouya controller though to my existing input code?

    EDIT: I realize that's what you're trying to tell me in your previous post... I'm just not 100% clear on how to apply what you've written there! I'm pretty dumb =]
    Post edited by gribbly on
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited January 2013
    First PM me to get the updates.

    (In an Awake event)

    Initialize the SDK:
    OuyaSDK.initialize(DEVELOPER_ID);
    Register the button listener:
                OuyaSDK.registerInputButtonListener(new OuyaSDK.InputButtonListener()
                {
                    onSuccess = (OuyaSDK.InputButtonEvent inputEvent) =>
                    {
                        OuyaInputManager.HandleButtonEvent(inputEvent);
                    },
    
                    onFailure = (int errorCode, string errorMessage) =>
                    {
                    }
                });
    Register the axis listener:
                OuyaSDK.registerInputAxisListener(new OuyaSDK.InputAxisListener()
                {
                    onSuccess = (OuyaSDK.InputAxisEvent inputEvent) =>
                    {
                        OuyaInputManager.HandleAxisEvent(inputEvent);
                    },
    
                    onFailure = (int errorCode, string errorMessage) =>
                    {
                    }
                });
    And then you can access OuyaInputManager with the same API that you previously used with native Unity input.
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • gribblygribbly Posts: 80Member
    Awesome, thx. Trying this now.
  • gribblygribbly Posts: 80Member
    edited January 2013
    Hmm, I'm getting:
    error CS0426: The nested type `InputButtonListener' does not exist in the type `OuyaSDK'
    error CS1502: The best overloaded method match for `OuyaSDK.registerInputButtonListener(OuyaSDK.InputButtonListener<OuyaSDK.InputButtonEvent>)' has some invalid arguments
    Assets/Global/Script/Vehicle.cs(78,25): error CS1503: Argument `#1' cannot convert `object' expression to type `OuyaSDK.InputButtonListener<OuyaSDK.InputButtonEvent>'

    ...on this line:

    OuyaSDK.registerInputButtonListener(new OuyaSDK.InputButtonListener()
    I get basically the same errors for the AxisListener too.
    Post edited by gribbly on
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited January 2013
    Is that a compile error?
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • gribblygribbly Posts: 80Member
    edited January 2013
    Yes, a compile error.

    Let me double check I'm using the latest package... I'm 99% sure, but my version of OuyaShowController.cs looks like this:

    OuyaSDK.registerInputButtonListener(new OuyaSDK.InputButtonListener<OuyaSDK.InputButtonEvent>()

    ...as opposed to:
    OuyaSDK.registerInputButtonListener(new OuyaSDK.InputButtonListener()

    Post edited by gribbly on
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited January 2013
    Ah yeah I just changed the class name to denote this is a different example of something you can do.

    There's nothing to change if you just use the update as is. I'll add more documentation to explain the new thing.
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • gribblygribbly Posts: 80Member
    Yep, I'm using latest stuff as far as I can tell.

    If I use the:

    new OuyaSDK.InputButtonListener<OuyaSDK.InputButtonEvent>()

    ...version I can get it to compile, but my controls don't work =]
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    Try importing the new package, reload the project, and reopen the scene. Something is messing with your references most likely.
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • gribblygribbly Posts: 80Member
    edited January 2013
    >Ah yeah I just changed the class name

    Not sure I understand... I'm saying that the version of OuyaShowController.cs that works in the same app calls new differently (i.e., it includes a <type> that your example above doesn't).

    Not sure how that relates to the class name?
    Post edited by gribbly on
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited January 2013
    Sorry for the confusion. The OuyaShowController.cs example included in the package is one way to capture input events. There's an alternate way included and not referenced, which is a way that wraps the Unity input. Here is an (OuyaControllerExample2 ) example of passing the input events to a wrapper to access just like the Unity input API.
    using System;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class OuyaControllerExample2 : MonoBehaviour
    {
        /// 
        /// This is your assigned developer id
        /// 
    
        private const string DEVELOPER_ID = "310a8f51-4d6e-4ae5-bda0-b93878e5f5d0";
    
        void Awake()
        {
            try
            {
                OuyaSDK.initialize(DEVELOPER_ID);
    
                OuyaSDK.registerInputButtonListener(new OuyaSDK.InputButtonListener()
                {
                    onSuccess = (OuyaSDK.InputButtonEvent inputEvent) =>
                    {
                        OuyaInputManager.HandleButtonEvent(inputEvent);
                    },
    
                    onFailure = (int errorCode, string errorMessage) =>
                    {
                    }
                });
    
                OuyaSDK.registerInputAxisListener(new OuyaSDK.InputAxisListener()
                {
                    onSuccess = (OuyaSDK.InputAxisEvent inputEvent) =>
                    {
                        OuyaInputManager.HandleAxisEvent(inputEvent);
                    },
    
                    onFailure = (int errorCode, string errorMessage) =>
                    {
                    }
                });
            }
            catch (System.Exception ex)
            {
                Debug.LogError(string.Format("Awake exception={0}", ex));
            }
        }
    }
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • gribblygribbly Posts: 80Member
    OK, thanks for the clarification, that makes sense to me now!

    However, I just literally pasted in your example code and I'm getting the same compile error:

    Assets/Global/Script/Vehicle.cs(80,61): error CS0426: The nested type `InputButtonListener' does not exist in the type `OuyaSDK'

    I've also tried re-importing the package, closing and re-opening my project and scenes, etc.

    I'll keep poking around :-/

  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited January 2013
    Oh sorry. I noticed something about an hour ago, which is this forum software handled my pre tags differently.

    I think the template greater than and less than are being treated like an invalid element and being removed from my example. I'll put into pastebin.

    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • gribblygribbly Posts: 80Member
    edited January 2013
    (Yeah, I have no idea how to control formatting in this forum! The "code tag" button seems to be very flaky)

    So looking in my OuyaSDK.cs, the only declaration of the InputButtonListener type is this:

        public class InputButtonListener<T> : GenericListener<T>
        {
        }

    ...which makes sense of the error I'm seeing. The <T> means its expecting a type, right? So I don't understand how this can work:

                OuyaSDK.registerInputButtonListener(new OuyaSDK.InputButtonListener()
                {
                    onSuccess = (OuyaSDK.InputButtonEvent inputEvent) =>
                    {
                        OuyaInputManager.HandleButtonEvent(inputEvent);
                    },
    
                    onFailure = (int errorCode, string errorMessage) =>
                    {
                    }
                });





    ...unless you have a different version of OuyaSDK.cs to me?
    Post edited by gribbly on
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited January 2013
    I had to paste into pastebin, I couldn't paste into the forums without it getting messed up:

    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • gribblygribbly Posts: 80Member
    edited January 2013
    OK that compiles! Thx =]

    Now, in my Update() I have stuff like:

            if(Input.GetButtonDown("Fire2")) {
                thrusting = true;
            }
            if(Input.GetButtonUp("Fire2")) {
                thrusting = false;
            }

    ...but these events don't trigger. What do I need to change? The "Input" part or the "Fire2" part?

    EDIT: Amusingly, the only thing I can get to trigger my existing code is a double tap on the track pad... no idea why that would be the case. That is sending a "Fire1" message.
    Post edited by gribbly on
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited January 2013
    It's mapped to the following axis strings:
        /// 
        /// Wrap Unity's method
        /// 
        /// 
        /// 
        public static float GetAxis(string inputName)
        {
    #if UNITY_ANDROID
            switch (inputName)
            {
                case "LT":
                    return m_axisLeftTrigger;
                case "RT":
                    return m_axisRightTrigger;
                case "RX":
                    return m_axisRightStickX;
                case "RY":
                    return -m_axisRightStickY;
                case "LX":
                    return m_axisLeftStickX;
                case "LY":
                    return m_axisLeftStickY;
            }
            return 0f;
    #else
            return Input.GetAxis(inputName);
    #endif
        }
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited January 2013
    And the following button strings:
        /// 
        /// Wrap Unity's method
        /// 
        /// 
        /// 
        public static bool GetButton(string inputName)
        {
    #if UNITY_ANDROID
            switch (inputName)
            {
                case "SYS": //arbitrary name and mapping
                    return m_buttonSystem;
                case "DPC": //arbitrary name and mapping
                    return m_buttonDPadCenter;
                case "DPD": //arbitrary name and mapping
                    return m_buttonDPadDown;
                case "DPL": //arbitrary name and mapping
                    return m_buttonDPadLeft;
                case "DPR": //arbitrary name and mapping
                    return m_buttonDPadRight;
                case "DPU": //arbitrary name and mapping
                    return m_buttonDPadUp;
                case "O": //arbitrary name and mapping
                    return m_buttonO;
                case "U": //arbitrary name and mapping
                    return m_buttonU;
                case "Y": //arbitrary name and mapping
                    return m_buttonY;
                case "A": //arbitrary name and mapping
                    return m_buttonA;
                case "LT": //arbitrary name and mapping
                    return m_buttonLT;
                case "RT": //arbitrary name and mapping
                    return m_buttonRT;
                case "LB": //arbitrary name and mapping
                    return m_buttonLB;
                case "RB": //arbitrary name and mapping
                    return m_buttonRB;
            }
            return false;
    #else
            return Input.GetButton(inputName);
    #endif
        }
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited January 2013
    gribbly said:
    OK that compiles! Thx =]

    Now, in my Update() I have stuff like:

            if(Input.GetButtonDown("Fire2")) {
                thrusting = true;
            }
            if(Input.GetButtonUp("Fire2")) {
                thrusting = false;
            }

    ...but these events don't trigger. What do I need to change? The "Input" part or the "Fire2" part?


    So:
            if(Input.GetButtonDown("Fire2")) {

    would change to:

            if(OuyaInputManager.GetButtonDown("LT")) { //left trigger for FIRE



    And:
            if(Input.GetButtonUp("Fire2")) {

    would change to:

            if(OuyaInputManager.GetButtonUp("LT")) { //left trigger for FIRE
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • gribblygribbly Posts: 80Member
    Man, I'm sorry but it still doesn't work :-/

    None of the OuyaInputManager.GetButton* variants are working for me.

    The only thing that fires is:

    Input.GetButtonDown("Fire1")

    ...and that fires on a double-tap of the trackpad. That at least demonstrates that the controller is paired to the console, and *something* is getting through. I've also put Debug.Logs through my code so I know my Update() is getting called, etc. It's just that these input events aren't triggering.

    I've tried "LT", "O", U", and a couple of others... no response.
  • gribblygribbly Posts: 80Member
    edited January 2013
    Here's my actual code, in case it helps:

    void Update () {
            //if(Input.GetButtonDown("Fire2")) {
            if(OuyaInputManager.GetButtonDown("U")) {   
                vehicleCameraScript.SmallThrustShake();
                thrusting = true;
                flightMode = false;
            }
            //if(Input.GetButtonUp("Fire2")) {
            if(OuyaInputManager.GetButtonUp("U")) {
                thrusting = false;
            }
           
            //flight mode thrust button
    #if UNITY_ANDROID
            if(OuyaInputManager.GetButton("O")) {
    #else   
            if(Input.GetButtonDown("Fire1")) {
    #endif
               
                vehicleCameraScript.BigThrustShake();

                //if we're already moving pretty fast, transition to flight mode faster (essentially no altitude gain)
                if(!landed && rigidbody.velocity.magnitude > 100.0f){
                    currentFlightModeTransitionTime = flightModeTransitionTime / 5.0f;
                }

                flightMode = true;
                thrusting = true;
            }
    #if UNITY_ANDROID
            if(OuyaInputManager.GetButtonUp("O")) {
    #else   
            if(Input.GetButtonUp("Fire1")) {
    #endif
                flightMode = false;
                thrusting = false;
               
                currentFlightModeTransitionTime = flightModeTransitionTime;
            }
    }


    ...none of the if() statements ever fire. I'm trying two variants, one wrapped in #if UNITY_ANDROID, one not wrapped (in case that was a problem). Neither work. The only code that works reliably is in another file:

        bool AdvanceInput(){
            if(Input.GetButtonDown("Fire1") || Input.GetButtonDown("Fire2") || OuyaInputManager.GetButtonDown("LT")) {
                Debug.Log("LevelStateMachine - AdvanceInput");
                return true;
            }
            else{
                return false;
            }
        }

    ...here I'm trying a third option (mixing Input.* and OuyaInputManager.* without worrying about platform for now). This if() statement returns true when I double tap the trackpad, and it's the Input.GetButtonDown("Fire1") that's triggering (confirmed by removing others).

    I really appreciate the help, I'm sorry I can't get it to work...
    Post edited by gribbly on
  • gribblygribbly Posts: 80Member
    CORRECTION:

    Input.GetButtonDown("Fire1") is now triggering on a single tap of the trackpad. Not sure if I was wrong before or the behavior changed.
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    Are there any errors in DDMS or logcat?
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • gribblygribbly Posts: 80Member
    Nothing that looks relevant in logcat. I don't know what DDMS is yet =]
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    On windows try running monitor in the android sdk and you can see the java and unity logs.
    C:\Program Files (x86)\Android\android-sdk\tools\monitor.bat
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • gribblygribbly Posts: 80Member
    Woah sweet =]

    OK, I have monitor running and I've switched back to my game after verifying that the "Show Controller" test app works correctly.

    All I get is GC spam, which I assume isn't relevant to this input problem. I don't see any log output when I press buttons.
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    The logging is commented out for performance, but if you turn the logging back on, and use a search filter tag you can see what you are looking for.
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • gribblygribbly Posts: 80Member
    As an experiment I tried adding this to the Show Controller test app:
    //in OuyaShowController.cs
    void Update() {
            if(OuyaInputManager.GetButtonDown("LT")) {
                Debug.Log("OuyaShowController - LT!"); //never fires
            }
    }

    ...and I see the same behavior as in my game (i.e., the if() never returns true).

    I also left Show Controller running for about ten minutes, and it got very laggy. Button highlights took 10-20 seconds to show up. I checked logcat and it was spamming:

    InputDispatcher - Dropped event because it is stale.
    InputDispatcher - Dropped event because it is stale.
    InputDispatcher - Dropped event because it is stale.

    ...not sure what that means, but it wasn't doing it for the first couple of minutes.

  • ShielsShiels Posts: 119Member
    The question is - which is more efficient, and which gives more control? We have had full success with our controller using the default setup, but the controller feels inexact and a little wonky. Oftentimes the character will move on his own without our input! Naturally we also want to minimise processor load...

    Should we move over to your script, tgraupmann?
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    I'd stick with my previous answer. If you want to plan for the future and plan to use profiles, you should expect the input to come from Java. As the elites have said, once you optimize the input events you can run at 60 FPS even with input coming from Java.

    Stick with the controller example that works for now. There's another video tutorial and a cool example coming.
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
Sign In or Register to comment.