Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System;
- public class InputDetector : MonoBehaviour
- {
- public bool logMessages = true;
- int maxJoystickNum = 100;
- int maxButtonNum = 100;
- int maxAxis = 100;
- string testJoyButtonLabel = "";
- string testJoyAxisLabel = "";
- string testKeyboardButtonLabel = "";
- //string testMouseInputLabel = "";
- //string testTouchInputLabel = "";
- const float minAxisValue = 0.25f;
- public Text uiText;
- #region set max joystick num, joystick button num, and joystick axis
- void SetMaxJoystickNum()
- {
- if (maxJoystickNum != 100)
- return;
- for (int i = 1; i <= maxJoystickNum; i++)
- {
- string joystickButton = "joystick " + i.ToString() + " button 0";
- try
- {
- Input.GetKeyDown(joystickButton);
- }
- catch (ArgumentException)
- {
- maxJoystickNum = i - 1;
- if (maxButtonNum >= 0)
- Debug.LogFormat("Max joystick number found: {0}", maxJoystickNum);
- else
- Debug.LogError("Max joystick number not found.");
- break;
- }
- }
- }
- void SetMaxJoystickButtonNum()
- {
- if (maxButtonNum != 100)
- return;
- int joyNum = 1;
- for (int i = 0; i <= maxButtonNum; i++)
- {
- string joystickButton = "joystick " + joyNum.ToString() + " button " + i.ToString();
- try
- {
- Input.GetKeyDown(joystickButton);
- }
- catch (ArgumentException)
- {
- maxButtonNum = i - 1;
- if (maxButtonNum >= 0)
- Debug.LogFormat("Max joystick button number found: {0}.", maxButtonNum);
- else
- Debug.LogError("Max joystick button not found.");
- break;
- }
- }
- }
- void SetMaxAxisNum()
- {
- if (maxAxis != 100)
- return;
- for (int i = 1; i <= maxAxis; i++)
- {
- string axisName = "axis " + i.ToString();
- try
- {
- Input.GetAxis(axisName);
- }
- catch (ArgumentException)
- {
- maxAxis = i - 1;
- if (maxAxis >= 1)
- Debug.LogFormat("max joystick axis found: {0}", maxAxis);
- else
- Debug.LogError("Max joystic axis not found. Ensure that InputManager.assat has the correct axis names available.");
- break;
- }
- }
- }
- #endregion
- void Awake()
- {
- SetMaxJoystickNum();
- SetMaxJoystickButtonNum();
- SetMaxAxisNum();
- }
- void Update()
- {
- TestJoystickButtons();
- TestJoystickAxis();
- TestKeyCodes();
- }
- private void TestTouch()
- {
- }
- private void TestMouse()
- {
- }
- void TestJoystickButtons()
- {
- for (int j = 1; j <= maxJoystickNum; j++)
- {
- for (int i = 0; i <= maxButtonNum; i++)
- {
- string joystickButton = string.Format("joystick {0} button {1}", j, i);
- try
- {
- if (Input.GetKeyDown(joystickButton))
- {
- testJoyButtonLabel = joystickButton;
- Log(testJoyButtonLabel);
- }
- }
- catch (ArgumentException)
- { }
- }
- }
- }
- void TestJoystickAxis()
- {
- for (int i = 1; i <= maxAxis; i++)
- {
- string axis = "axis " + i.ToString();
- float value = Input.GetAxis(axis);
- if (Mathf.Abs(value) > minAxisValue)
- {
- string valueLabel = value.ToString("N2");
- if (value < 0)
- valueLabel = "<color=red>" + valueLabel + "</color>";
- testJoyAxisLabel = axis + "\t" + valueLabel;
- return;
- }
- }
- }
- void TestKeyCodes()
- {
- foreach (KeyCode keyCode in Enum.GetValues(typeof(KeyCode)))
- {
- if (keyCode.ToString().Contains("Joystick"))
- continue;
- if (Input.GetKeyDown(keyCode) && keyCode != KeyCode.None)
- {
- testKeyboardButtonLabel = keyCode.ToString();
- Log(testKeyboardButtonLabel);
- }
- }
- }
- private static Rect fullscreenRect = new Rect(0, 0, Screen.width, Screen.height);
- void OnGUI()
- {
- string label = string.Format("<b>Joystick Button:</b>\n\t{0}\n\n<b>Joystick Axis:</b>\n\t{1}\n\n<b>Keyboard Button:</b>\n\t{2}",
- testJoyButtonLabel, testJoyAxisLabel, testKeyboardButtonLabel);
- if (uiText == null)
- {
- GUI.Label(fullscreenRect, label);
- }
- else
- uiText.text = label;
- }
- void Log(string message)
- {
- if (!logMessages)
- return;
- Debug.Log(message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement