/// <summary><c>SetControls</c> expects <c>actionCollection</c> to be a new instance of your /// InputActionCollection. Ej: var p1Controls = new MyControls(); </summary> public void SetControls <T>(T actionCollection) where T : IInputActionCollection { _controls = actionCollection; var properties = actionCollection.GetType().GetProperty("asset"); var asset = (InputActionAsset)properties.GetValue(actionCollection); if (_playerInput.devices.Count > 0) //based on https://forum.unity.com/threads/playerinput-loses-paired-devices-when-actions-are-changed-at-runtime.771140/ { var devices = _playerInput.devices.ToArray(); _playerInput.actions = asset; foreach (var device in devices) { InputUser.PerformPairingWithDevice(device, _playerInput.user); } } else { _playerInput.actions = asset; } if (_playerInput.uiInputModule != null) { _playerInput.uiInputModule.actionsAsset = _playerInput.actions; _playerInput.uiInputModule.actionsAsset.devices = _playerInput.devices; //This shouldn't be needed, BUT: https://forum.unity.com/threads/input-system-1-2-0-breaks-multiplayer-ui-navigation-weird-fix-inside.1210365/ _playerInput.uiInputModule.enabled = false; _playerInput.uiInputModule.enabled = true; } _playerInput.neverAutoSwitchControlSchemes = _playerInput.playerIndex > 0 || !allowP0AutoSwitch; }
// Just for reference // Based on: https://forum.unity.com/threads/solved-can-the-new-input-system-be-used-without-the-player-input-component.856108/#post-5669128 public void BindGamepad <T>(int playerIndex) where T : IInputActionCollection, new() { _controls?.Disable(); _controls = new T(); _controls.devices = new[] { Gamepad.all[playerIndex] }; _controls.bindingMask = InputBinding.MaskByGroup("Gamepad"); _controls.Enable(); }
T GetWrapper <T>(IInputActionCollection actionCollection) { var properties = actionCollection.GetType().GetProperties(); var wrapperInfo = properties.First(p => p.PropertyType == typeof(T)); var wrapper = (T)wrapperInfo.GetValue(actionCollection); return(wrapper); }
//Pair methods shouldn't be needed. Using the generated class should be enough with methods provided above. public void PairGamepad <T>(int playerIndex) where T : IInputActionCollection, new() { _controls?.Disable(); _controls = new T(); var iUser = InputUser.PerformPairingWithDevice(Gamepad.all[playerIndex]); iUser.AssociateActionsWithUser(_controls); iUser.ActivateControlScheme("Gamepad"); _controls.Enable(); }
public static void SetEnabled(this IInputActionCollection collection, bool enabled) { if (enabled) { collection.Enable(); } else { collection.Disable(); } }
public void PairKeyboardMouse <T>(int playerIndex) where T : IInputActionCollection, new() { _controls?.Disable(); _controls = new T(); var iUser = InputUser.PerformPairingWithDevice(Keyboard.current); InputUser.PerformPairingWithDevice(Mouse.current, user: iUser); iUser.AssociateActionsWithUser(_controls); iUser.ActivateControlScheme("KeyboardMouse"); _controls.Enable(); }
/// <summary> /// Retreives input source, creates one if none exists. Enables the newly created input source. /// </summary> /// <param name="settingsType"></param> /// <returns></returns> private static IInputActionCollection GetControlSettings(Type settingsType) { if (_instance._inputSettings.ContainsKey(settingsType)) { return(_instance._inputSettings[settingsType]); } IInputActionCollection newSettings = Activator.CreateInstance(settingsType) as IInputActionCollection; _instance._inputSettings[settingsType] = newSettings; newSettings.Enable(); return(newSettings); }
public static TMapperType GetActionsMapper <TMapperType>() where TMapperType : IInputActionCollection { IInputActionCollection mapper = null; if (!instance.actionsMapper.TryGetValue(typeof(TMapperType), out mapper)) { mapper = (IInputActionCollection)Activator.CreateInstance(typeof(TMapperType)); instance.actionsMapper.Add(typeof(TMapperType), mapper); } return((TMapperType)mapper); }
/// <summary> /// Return input of target type. /// </summary> /// <typeparam name="InputHandlerType"></typeparam> /// <returns>Input of desired type.</returns> /// Guaranteed to return an input. If the desired input has not been /// instantiated, it will first be instantiated and then returned. public static InputHandlerType GetInput <InputHandlerType>() where InputHandlerType : class, IInputHandler, new() { IInputHandler returnValue; if (!Instance._inputHandlers.TryGetValue(typeof(InputHandlerType), out returnValue)) { returnValue = new InputHandlerType(); Instance._inputHandlers.Add(typeof(InputHandlerType), returnValue); IInputActionCollection source = GetControlSettings(returnValue.InputSourceType); returnValue.InputSource = source; } return(returnValue as InputHandlerType); }
public override void RegisterEvents(IInputActionCollection inputActions) { base.RegisterEvents(inputActions); #region Bind PC input var selectItemActionPC = _inputActionsPC.AddAction(_selectActionNamePC, InputActionType.Button, "<Mouse>/leftButton"); selectItemActionPC.canceled += CallClick; #endregion Bind PC input #region Bind VR input var selectItemActionVR = _inputActionsVR.AddAction(_selectActionNameVR, InputActionType.Button, "<XRInputV1::HTC::HTCViveControllerOpenXR>{RightHand}/triggerpressed"); selectItemActionVR.canceled += CallClick; #endregion Bind VR input }
public override void RegisterEvents(IInputActionCollection inputActions) { base.RegisterEvents(inputActions); #region Bind PC input var selectItemActionPC = _inputActionsPC.AddAction(_selectActionPCName, InputActionType.Button, "<Mouse>/leftButton"); var changeColorActionPC = _inputActionsPC.AddAction(_changeColorActionPCName, InputActionType.Button); changeColorActionPC.AddCompositeBinding("1DAxis").With("Positive", "<Keyboard>/e").With("Negative", "<Keyboard>/q"); selectItemActionPC.canceled += CallSelectItem; changeColorActionPC.started += CallChangeColor; #endregion Bind PC input #region Bind VR input var selectItemActionVR = _inputActionsVR.AddAction(_selectActionVRName, InputActionType.Button, "<XRInputV1::HTC::HTCViveControllerOpenXR>{RightHand}/triggerpressed"); var changeColorActionVR = _inputActionsVR.AddAction(_changeColorActionVRName, InputActionType.Value, "<ViveController>{RightHand}/trackpad/x"); selectItemActionVR.canceled += CallSelectItem; changeColorActionVR.started += CallChangeColor; #endregion Bind VR input }
/// <summary> /// Associate an .inputactions asset with the given user. /// </summary> /// <param name="user"></param> /// <param name="actions"></param> /// <typeparam name="TUser"></typeparam> /// <exception cref="ArgumentNullException"></exception> public static void AssignInputActions <TUser>(this TUser user, IInputActionCollection actions) where TUser : class, IInputUser { if (user == null) { throw new ArgumentNullException("user"); } if (actions == null) { throw new ArgumentNullException("actions"); } var index = FindUserIndex(user); if (index == -1) { throw new InvalidOperationException(string.Format("User '{0}' has not been added to the system", user)); } s_AllUserData[index].actions = actions; }
public virtual void Apply(IInputActionCollection actions) { //set overrideProcessors and redirectPaths on respective bindings }
public InputState(IInputActionCollection actions, InputControlScheme controlScheme) { User = InputUser.CreateUserWithoutPairedDevices(); User.AssociateActionsWithUser(actions); SwitchControlScheme(controlScheme); }