int GetGamepadId(VirtualAxisData data) { if (data.type == 0) { if (data.positiveButton.StartsWith("joystick") && char.IsDigit(data.positiveButton[9])) { string[] parts = data.positiveButton.Split(new char[] { ' ' }); if (parts.Length > 3) { return(int.Parse(parts[1])); } } if (data.negativeButton.StartsWith("joystick") && char.IsDigit(data.negativeButton[9])) { string[] parts = data.negativeButton.Split(new char[' ']); if (parts.Length > 3) { return(int.Parse(parts[1])); } } if (data.altPositiveButton.StartsWith("joystick") && char.IsDigit(data.altPositiveButton[9])) { string[] parts = data.altPositiveButton.Split(new char[' ']); if (parts.Length > 3) { return(int.Parse(parts[1])); } } if (data.altNegativeButton.StartsWith("joystick") && char.IsDigit(data.altNegativeButton[9])) { string[] parts = data.altNegativeButton.Split(new char[' ']); if (parts.Length > 3) { return(int.Parse(parts[1])); } } } if (data.type == 2) { return(data.joyNum); } return(0); }
bool IsGamepad(VirtualAxisData data) { if (data.type == 1) { return(false); } if (data.type == 0) { return(data.positiveButton.StartsWith("joystick") || data.negativeButton.StartsWith("joystick") || data.altPositiveButton.StartsWith("joystick") || data.altNegativeButton.StartsWith("joystick")); } return(true); }
VirtualAxisData ParseAxisData(SerializedProperty data) { VirtualAxisData parsedAxisData = new VirtualAxisData(); data.Next(true); do { switch (data.name) { case "m_Name": parsedAxisData.name = data.stringValue; break; case "negativeButton": parsedAxisData.negativeButton = data.stringValue; break; case "positiveButton": parsedAxisData.positiveButton = data.stringValue; break; case "altNegativeButton": parsedAxisData.altNegativeButton = data.stringValue; break; case "altPositiveButton": parsedAxisData.altPositiveButton = data.stringValue; break; case "type": parsedAxisData.type = data.intValue; break; case "axis": parsedAxisData.axis = data.intValue; break; case "joyNum": parsedAxisData.joyNum = data.intValue; break; } }while (data.Next(false) && data.propertyType != SerializedPropertyType.Generic); return(parsedAxisData); }
void GetVirtualAxisNames() { m_VirtualAxisNames.ClearArray(); m_VirtualAxisNames.InsertArrayElementAtIndex(0); m_VirtualAxisNames.GetArrayElementAtIndex(0).stringValue = "None"; Object[] assets = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset"); if (assets.Length != 0) { SerializedObject inputManager = new SerializedObject(assets[0]); SerializedProperty axes = inputManager.FindProperty("m_Axes"); for (int i = 0; i < axes.arraySize; i++) { SerializedProperty axisData = axes.GetArrayElementAtIndex(i); VirtualAxisData parsedAxisData = ParseAxisData(axisData); m_VirtualAxisNames.InsertArrayElementAtIndex(m_VirtualAxisNames.arraySize); m_VirtualAxisNames.GetArrayElementAtIndex(m_VirtualAxisNames.arraySize - 1).stringValue = parsedAxisData.name; } } }
int GetGamepadButton(VirtualAxisData data) { if (data.positiveButton.StartsWith("joystick")) { return(int.Parse(data.positiveButton.Substring(data.positiveButton.LastIndexOf(" ") + 1))); } if (data.negativeButton.StartsWith("joystick")) { return(int.Parse(data.negativeButton.Substring(data.negativeButton.LastIndexOf(" ") + 1))); } if (data.altPositiveButton.StartsWith("joystick")) { return(int.Parse(data.altPositiveButton.Substring(data.altPositiveButton.LastIndexOf(" ") + 1))); } if (data.altNegativeButton.StartsWith("joystick")) { return(int.Parse(data.altNegativeButton.Substring(data.altNegativeButton.LastIndexOf(" ") + 1))); } return(-1); }
void MapFromInputManager() { GetVirtualAxisNames(); Object[] assets = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset"); if (assets.Length != 0) { SerializedObject inputManager = new SerializedObject(assets[0]); SerializedProperty axes = inputManager.FindProperty("m_Axes"); m_GamepadMappings.ClearArray(); for (int i = 0; i < axes.arraySize; i++) { SerializedProperty axisData = axes.GetArrayElementAtIndex(i); VirtualAxisData parsedAxisData = ParseAxisData(axisData); if (!IsGamepad(parsedAxisData)) { continue; } int gamepadID = GetGamepadId(parsedAxisData); int mapIndex = GetGamepadMapIndexById(gamepadID); if (mapIndex == -1) { mapIndex = m_GamepadMappings.arraySize; m_GamepadMappings.InsertArrayElementAtIndex(m_GamepadMappings.arraySize); SerializedProperty prop = m_GamepadMappings.GetArrayElementAtIndex(mapIndex); prop.Next(true); do { if (prop.name == "Id") { prop.intValue = gamepadID; } if (prop.isArray) { prop.ClearArray(); } }while (prop.Next(false) && prop.propertyPath.Contains(m_GamepadMappings.propertyPath)); } if (parsedAxisData.type == 0) { int buttonIndex = GetGamepadButton(parsedAxisData); if (buttonIndex == -1) { Debug.LogError("Failed to map axis " + parsedAxisData.name + "!" + "Couldn't get joystick button id."); continue; } SerializedProperty prop = m_GamepadMappings.GetArrayElementAtIndex(mapIndex); while (prop.name != "Buttons") { prop.Next(true); } while (buttonIndex >= prop.arraySize) { prop.InsertArrayElementAtIndex(prop.arraySize); prop.GetArrayElementAtIndex(prop.arraySize - 1).intValue = 0; } prop.GetArrayElementAtIndex(buttonIndex).intValue = i + 1; } if (parsedAxisData.type == 2) { SerializedProperty prop = m_GamepadMappings.GetArrayElementAtIndex(mapIndex); while (prop.name != "Axes") { prop.Next(true); } while (parsedAxisData.axis >= prop.arraySize) { prop.InsertArrayElementAtIndex(prop.arraySize); prop.GetArrayElementAtIndex(prop.arraySize - 1).intValue = 0; } prop.GetArrayElementAtIndex(parsedAxisData.axis).intValue = i + 1; } } } }