Advertisement
napland

InputDetector.cs

Oct 18th, 2016
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System;
  5.  
  6. public class InputDetector : MonoBehaviour
  7. {
  8.     public bool logMessages = true;
  9.  
  10.     int maxJoystickNum = 100;
  11.     int maxButtonNum = 100;
  12.     int maxAxis = 100;
  13.     string testJoyButtonLabel = "";
  14.     string testJoyAxisLabel = "";
  15.     string testKeyboardButtonLabel = "";
  16.     //string testMouseInputLabel = "";
  17.     //string testTouchInputLabel = "";
  18.     const float minAxisValue = 0.25f;
  19.  
  20.     public Text uiText;
  21.  
  22.  
  23.     #region set max joystick num, joystick button num, and joystick axis
  24.     void SetMaxJoystickNum()
  25.     {
  26.         if (maxJoystickNum != 100)
  27.             return;
  28.  
  29.         for (int i = 1; i <= maxJoystickNum; i++)
  30.         {
  31.             string joystickButton = "joystick " + i.ToString() + " button 0";
  32.             try
  33.             {
  34.                 Input.GetKeyDown(joystickButton);
  35.             }
  36.             catch (ArgumentException)
  37.             {
  38.                 maxJoystickNum = i - 1;
  39.                 if (maxButtonNum >= 0)
  40.                     Debug.LogFormat("Max joystick number found: {0}", maxJoystickNum);
  41.                 else
  42.                     Debug.LogError("Max joystick number not found.");
  43.  
  44.                 break;
  45.             }
  46.         }
  47.     }
  48.  
  49.     void SetMaxJoystickButtonNum()
  50.     {
  51.         if (maxButtonNum != 100)
  52.             return;
  53.         int joyNum = 1;
  54.  
  55.         for (int i = 0; i <= maxButtonNum; i++)
  56.         {
  57.             string joystickButton = "joystick " + joyNum.ToString() + " button " + i.ToString();
  58.             try
  59.             {
  60.                 Input.GetKeyDown(joystickButton);
  61.             }
  62.             catch (ArgumentException)
  63.             {
  64.                 maxButtonNum = i - 1;
  65.                 if (maxButtonNum >= 0)
  66.                     Debug.LogFormat("Max joystick button number found: {0}.", maxButtonNum);
  67.                 else
  68.                     Debug.LogError("Max joystick button not found.");
  69.                 break;
  70.             }
  71.         }
  72.     }
  73.  
  74.  
  75.     void SetMaxAxisNum()
  76.     {
  77.         if (maxAxis != 100)
  78.             return;
  79.  
  80.         for (int i = 1; i <= maxAxis; i++)
  81.         {
  82.             string axisName = "axis " + i.ToString();
  83.             try
  84.             {
  85.                 Input.GetAxis(axisName);
  86.             }
  87.             catch (ArgumentException)
  88.             {
  89.                 maxAxis = i - 1;
  90.  
  91.                 if (maxAxis >= 1)
  92.                     Debug.LogFormat("max joystick axis found: {0}", maxAxis);
  93.                 else
  94.                     Debug.LogError("Max joystic axis not found. Ensure that InputManager.assat has the correct axis names available.");
  95.                 break;
  96.             }
  97.         }
  98.     }
  99.     #endregion
  100.  
  101.  
  102.     void Awake()
  103.     {
  104.         SetMaxJoystickNum();
  105.         SetMaxJoystickButtonNum();
  106.         SetMaxAxisNum();
  107.     }
  108.  
  109.     void Update()
  110.     {
  111.         TestJoystickButtons();
  112.         TestJoystickAxis();
  113.         TestKeyCodes();
  114.     }
  115.  
  116.  
  117.     private void TestTouch()
  118.     {
  119.  
  120.     }
  121.  
  122.     private void TestMouse()
  123.     {
  124.  
  125.     }
  126.  
  127.     void TestJoystickButtons()
  128.     {
  129.         for (int j = 1; j <= maxJoystickNum; j++)
  130.         {
  131.             for (int i = 0; i <= maxButtonNum; i++)
  132.             {
  133.                 string joystickButton = string.Format("joystick {0} button {1}", j, i);
  134.  
  135.                 try
  136.                 {
  137.                     if (Input.GetKeyDown(joystickButton))
  138.                     {
  139.                         testJoyButtonLabel = joystickButton;
  140.                         Log(testJoyButtonLabel);
  141.                     }
  142.                 }
  143.                 catch (ArgumentException)
  144.                 { }
  145.             }
  146.         }
  147.     }
  148.  
  149.  
  150.     void TestJoystickAxis()
  151.     {
  152.         for (int i = 1; i <= maxAxis; i++)
  153.         {
  154.             string axis = "axis " + i.ToString();
  155.             float value = Input.GetAxis(axis);
  156.             if (Mathf.Abs(value) > minAxisValue)
  157.             {
  158.                 string valueLabel = value.ToString("N2");
  159.  
  160.                 if (value < 0)
  161.                     valueLabel = "<color=red>" + valueLabel + "</color>";
  162.  
  163.                 testJoyAxisLabel = axis + "\t" + valueLabel;
  164.                 return;
  165.             }
  166.         }
  167.     }
  168.  
  169.  
  170.  
  171.     void TestKeyCodes()
  172.     {
  173.         foreach (KeyCode keyCode in Enum.GetValues(typeof(KeyCode)))
  174.         {
  175.             if (keyCode.ToString().Contains("Joystick"))
  176.                 continue;
  177.  
  178.             if (Input.GetKeyDown(keyCode) && keyCode != KeyCode.None)
  179.             {
  180.                 testKeyboardButtonLabel = keyCode.ToString();
  181.                 Log(testKeyboardButtonLabel);
  182.             }
  183.         }
  184.     }
  185.  
  186.  
  187.     private static Rect fullscreenRect = new Rect(0, 0, Screen.width, Screen.height);
  188.     void OnGUI()
  189.     {
  190.         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}",
  191.                 testJoyButtonLabel, testJoyAxisLabel, testKeyboardButtonLabel);
  192.  
  193.         if (uiText == null)
  194.         {
  195.             GUI.Label(fullscreenRect, label);
  196.         }
  197.         else
  198.             uiText.text = label;
  199.     }
  200.  
  201.     void Log(string message)
  202.     {
  203.         if (!logMessages)
  204.             return;
  205.  
  206.         Debug.Log(message);
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement