Get list of Axes?

Is there a way to get the list of axes, or enumerate them somehow, defined in the InputManager? I know I can get them if I know there name. I want to get all the defined ones, to find their names and such.

What would be the point? Axes can't be dynamically added, so just keep a list of it somewhere in your application.

–

4 Answers

4

Yup.

Edit: As pointed out by Bunny in the comments - this will only work in the editor and relies on reflection - it could easily break in future versions of Unity if they randomly decide to rename or restructure their input classes/data. However there is no real alternative with the existing (as of 2017.1) input system.

using UnityEngine;
using System.Collections;

using UnityEditor;

public class ReadInputManager
{
    public static void ReadAxes()
    {
        var inputManager = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0];

        SerializedObject obj = new SerializedObject(inputManager);

        SerializedProperty axisArray = obj.FindProperty("m_Axes");

        if (axisArray.arraySize == 0)
            Debug.Log("No Axes");

        for( int i = 0; i < axisArray.arraySize; ++i )
        {
            var axis = axisArray.GetArrayElementAtIndex(i);

            var name = axis.FindPropertyRelative("m_Name").stringValue;
            var axisVal = axis.FindPropertyRelative("axis").intValue;
            var inputType = (InputType)axis.FindPropertyRelative("type").intValue;

            Debug.Log(name);
            Debug.Log(axisVal);
            Debug.Log(inputType);
        }
    }

    public enum InputType
    {
        KeyOrMouseButton,
        MouseMovement,
        JoystickAxis,
    };

    [MenuItem("Assets/ReadInputManager")]
    public static void DoRead()
    {
        ReadAxes();
    }

}

If you have a look at the Input Manager the rest should be pretty easy to figure out.

Great post! It should be noted that this only works in the editor, not in a build.

–

Thank you so much!! I always wanted to find a way to get a list of them, this is perfect for what I needed it for!

–

Hi, I’m strugling with same issue here. It would be really handy to be able to get that list from a script.

Nope, there’s no way to do it. Unity’s InputManager is extremely limited and doesn’t allow you do to anything at all during runtime except get input.

Update: Yes, it’s possible in the editor as Sarkhan’s post shows, but not in a game build.

I’m not sure exactly how but I think that you need to use GetComponent and for loops :slight_smile:

http://unity3d.com/learn/tutorials/modules/beginner/scripting/loops
http://unity3d.com/learn/tutorials/modules/beginner/scripting/enabling-disabling-components

these two videos might help

Maybe I'm mistaken, but I don't think you really understand what the question is about. The InputManager is not a GameObjects Component. We are talking about the panel we get on the inspector when we select Edit -> Project Settings -> Input. And the question is what's the way to access its properties from a script, if there's one.

–