static InputAction GetInputAction(InputActionReference actionReference) { #pragma warning disable IDE0031 // Use null propagation -- Do not use for UnityEngine.Object types return(actionReference != null ? actionReference.action : null); #pragma warning restore IDE0031 }
public static void SetActionCallback(InputActionReference actionReference, Action <InputAction.CallbackContext> callback, bool install) { if (!install && callback == null) { return; } if (actionReference == null) { return; } var action = actionReference.action; if (action == null) { return; } if (install) { action.performed += callback; action.canceled += callback; } else { action.performed -= callback; action.canceled -= callback; } }
void DrawInputProviderButtonInInspector() { var vcamBase = Target; if (!vcamBase.RequiresUserInput() || vcamBase.GetComponent <CinemachineInputProvider>() != null) { return; } EditorGUILayout.Space(); EditorGUILayout.HelpBox( "The InputSystem package is installed, but it is not used to control this vcam.", MessageType.Info); var rect = EditorGUILayout.GetControlRect(true); rect.x += EditorGUIUtility.labelWidth; rect.width -= EditorGUIUtility.labelWidth; if (GUI.Button(rect, s_InputProviderAddLabel)) { if (s_InputActionReference == null) { s_InputActionReference = (InputActionReference)AssetDatabase.LoadAllAssetsAtPath( "Packages/com.unity.inputsystem/InputSystem/Plugins/PlayerInput/DefaultInputActions.inputactions"). FirstOrDefault(x => x.name == "Player/Look"); } var inputProvider = Undo.AddComponent <CinemachineInputProvider>(vcamBase.gameObject); inputProvider.XYAxis = s_InputActionReference; } EditorGUILayout.Space(); }
private string BindToText(InputActionReference actionReference, int index) { return(InputControlPath.ToHumanReadableString( actionReference.action.bindings[index].effectivePath, InputControlPath.HumanReadableStringOptions.OmitDevice )); }
public static InputActionReference UpdateReference(InputActionAsset asset, InputActionReference actionReference) { if (asset == null) { return(null); } var oldAction = actionReference?.action; if (oldAction == null) { return(null); } var oldActionMap = oldAction.actionMap; Debug.Assert(oldActionMap != null, "Not expected to end up with a singleton action here"); var newActionMap = asset.FindActionMap(oldActionMap.name); if (newActionMap == null) { return(null); } var newAction = newActionMap.FindAction(oldAction.name); if (newAction == null) { return(null); } return(InputActionReference.Create(newAction)); }
public void Unbind(InputActionReference inputAction) { if (inputAction) { Unbind(inputAction.action); } }
public void Samples_RebindingUI_UpdatesWhenKeyboardLayoutChanges() { var canvasGO = new GameObject(); canvasGO.AddComponent <Canvas>(); var bindingLabelGO = new GameObject(); bindingLabelGO.transform.parent = canvasGO.transform; var bindingLabel = bindingLabelGO.AddComponent <Text>(); var asset = ScriptableObject.CreateInstance <InputActionAsset>(); var actionMap = asset.AddActionMap("map"); var action = actionMap.AddAction("action", binding: "<Keyboard>/a"); var go = new GameObject(); var rebind = go.AddComponent <RebindActionUI>(); rebind.bindingId = action.bindings[0].id.ToString(); rebind.actionReference = InputActionReference.Create(action); rebind.bindingText = bindingLabel; Assert.That(bindingLabel.text, Is.EqualTo("A")); SetKeyInfo(Key.A, "Q"); Assert.That(bindingLabel.text, Is.EqualTo("Q")); }
/// <summary> /// In a multi-player context, actions are associated with specific players /// This resolves the appropriate action reference for the specified player. /// /// Because the resolution involves a search, we also cache the returned /// action to make future resolutions faster. /// </summary> /// <param name="axis">Which input axis (0, 1, or 2)</param> /// <param name="actionRef">Which action reference to resolve</param> /// <returns>The cached action for the player specified in PlayerIndex</returns> protected InputAction ResolveForPlayer(int axis, InputActionReference actionRef) { if (axis < 0 || axis >= NUM_AXES) { return(null); } if (actionRef == null || actionRef.action == null) { return(null); } if (m_cachedActions == null || m_cachedActions.Length != NUM_AXES) { m_cachedActions = new InputAction[NUM_AXES]; } if (m_cachedActions[axis] != null && actionRef.action.id != m_cachedActions[axis].id) { m_cachedActions[axis] = null; } if (m_cachedActions[axis] == null) { m_cachedActions[axis] = actionRef.action; } // Auto-enable it if disabled if (m_cachedActions[axis] != null && !m_cachedActions[axis].enabled) { m_cachedActions[axis].Enable(); } return(m_cachedActions[axis]); }
/// <summary> /// In a multi-player context, actions are associated with specific players /// This resolves the appropriate action reference for the specified player. /// /// Because the resolution involves a search, we also cache the returned /// action to make future resolutions faster. /// </summary> /// <param name="axis">Which input axis (0, 1, or 2)</param> /// <param name="actionRef">Which action reference to resolve</param> /// <returns>The cached action for the player specified in PlayerIndex</returns> protected InputAction ResolveForPlayer(int axis, InputActionReference actionRef) { if (axis < 0 || axis >= NUM_AXES) { return(null); } if (actionRef == null || actionRef.action == null) { return(null); } if (m_cachedActions == null || m_cachedActions.Length != NUM_AXES) { m_cachedActions = new InputAction[NUM_AXES]; } if (m_cachedActions[axis] != null && actionRef.action.id != m_cachedActions[axis].id) { m_cachedActions[axis] = null; } if (m_cachedActions[axis] == null) { m_cachedActions[axis] = actionRef.action; if (PlayerIndex != -1) { var user = InputUser.all[PlayerIndex]; m_cachedActions[axis] = user.actions.First(x => x.id == actionRef.action.id); } } // Auto-enable it if disabled if (m_cachedActions[axis] != null && !m_cachedActions[axis].enabled) { m_cachedActions[axis].Enable(); } return(m_cachedActions[axis]); }
private bool SetInputEnabled(bool enableInput, InputActionReference input, System.Action <InputAction.CallbackContext> callback) { if (input == null) { return(false); } else { if (callback != null) { input.action.started -= callback; input.action.canceled -= callback; } if (enableInput) { if (callback != null) { input.action.started += callback; input.action.canceled += callback; } input.action.Enable(); } else { input.action.Disable(); } return(true); } }
public void OpenSkillWheel() { Time.timeScale = 0.5f; ((InputSystemUIInputModule)EventSystem.current.currentInputModule).move = InputActionReference.Create(player.input.SkillWheel.Navigate); // this.enabled = true; ToggleSlots(true); StartCoroutine(ReadRightStickInput()); }
private void RebindComplete(InputActionReference inputAction) { keyText.text = BindToText(inputAction); playerInput.SwitchCurrentActionMap("Player"); rebindingOperation.Dispose(); UIText.text = uiText; playerMenuInput = false; compositeIndex = 0; }
public void DisableAction(InputActionReference actionReference) { var action = GetInputAction(actionReference); if (action != null && action.enabled) { action.Disable(); } }
public void PauseGame() { pausemenu.SetActive(true); pc.enabled = false; Time.timeScale = 0f; isPaused = true; EventSystem.current.SetSelectedGameObject(resumebutton); ((InputSystemUIInputModule)EventSystem.current.currentInputModule).move = InputActionReference.Create(input.UI.Navigate); }
private void SwitchActionMapTo(string actionMapName) { playerInput.SwitchCurrentActionMap(actionMapName); InputAction pointAction = inputModule.actionsAsset.FindActionMap(actionMapName).FindAction(ActionPoint); inputModule.point = InputActionReference.Create(pointAction); InputAction clickAction = inputModule.actionsAsset.FindActionMap(actionMapName).FindAction(ActionClick); inputModule.leftClick = InputActionReference.Create(clickAction); }
public BindCode Bind(InputActionReference actionRef, bool deleteOldBindings = true) { if (actionRef == null) { DebugManager.WarningMessage($"There was an issue attempting to bind input, Error Code: {BindCode.INVALID_ACTION.ToString()}"); return(BindCode.INVALID_ACTION); } return(Bind(actionRef.action, deleteOldBindings)); }
// Make a copy public ActionInput(ActionInput selectorInput) { if (selectorInput == null) { return; } SelectorInputName = selectorInput.SelectorInputName; InputActionRef = selectorInput.InputActionRef; Action = selectorInput.Action; }
public PlayerInput2D( InputActionReference moveAction, InputActionReference jumpAction) { MoveAction = moveAction; JumpAction = jumpAction; MoveAction.action.performed += UpdateMoveInputVector; MoveAction.action.canceled += UpdateMoveInputVector; JumpAction.action.performed += cbt => Jumped?.Invoke(); }
// A slower way of opening the skill wheel public void OpenSkillWheel2() { Time.timeScale = 0f; ((InputSystemUIInputModule)EventSystem.current.currentInputModule).move = InputActionReference.Create(player.input.SkillWheel.Navigate); // this.enabled = true; ToggleSlots(true); // Button slotOne = this.GetComponentInChildren<Button>(); // slotOne.Select(); // slotOne.OnSelect(null); slots[0].Select(); slots[0].OnSelect(null); }
public void CloseSkillWheel() { usingFastSkillWheel = false; ((InputSystemUIInputModule)EventSystem.current.currentInputModule).move = InputActionReference.Create(player.input.UI.Navigate); if (OnSkillEquip != null) { OnSkillEquip(); Debug.Log("Equipped Skill"); } // this.enabled = false; ToggleSlots(false); Time.timeScale = 1f; }
/// <summary> /// In a multi-player context, actions are associated with specific players /// This resolves the appropriate action reference for the specified player. /// /// Because the resolution involves a search, we also cache the returned /// action to make future resolutions faster. /// </summary> /// <param name="axis">Which input axis (0, 1, or 2)</param> /// <param name="actionRef">Which action reference to resolve</param> /// <returns>The cached action for the player specified in PlayerIndex</returns> protected InputAction ResolveForPlayer(int axis, InputActionReference actionRef) { if (axis < 0 || axis >= NUM_AXES) { return(null); } if (actionRef == null || actionRef.action == null) { return(null); } if (m_cachedActions == null || m_cachedActions.Length != NUM_AXES) { m_cachedActions = new InputAction[NUM_AXES]; } if (m_cachedActions[axis] != null && actionRef.action.id != m_cachedActions[axis].id) { m_cachedActions[axis] = null; } if (m_cachedActions[axis] == null) { m_cachedActions[axis] = actionRef.action; if (PlayerIndex != -1) { m_cachedActions[axis] = GetFirstMatch(InputUser.all[PlayerIndex], actionRef); } if (AutoEnableInputs && actionRef != null && actionRef.action != null) { actionRef.action.Enable(); } } // Update enabled status if (m_cachedActions[axis] != null && m_cachedActions[axis].enabled != actionRef.action.enabled) { if (actionRef.action.enabled) { m_cachedActions[axis].Enable(); } else { m_cachedActions[axis].Disable(); } } return(m_cachedActions[axis]); // local function to wrap the lambda which otherwise causes a tiny gc InputAction GetFirstMatch(in InputUser user, InputActionReference aRef) => user.actions.First(x => x.id == aRef.action.id); }
private string GetKeyBindingText() { InputActionReference reference = index switch { WeaponClass.AbilityIndex.Abilities1 => inputActionReferences.ability1, WeaponClass.AbilityIndex.Abilities2 => inputActionReferences.ability2, WeaponClass.AbilityIndex.Abilities3 => inputActionReferences.ability3, _ => inputActionReferences.ability1 }; int bindingIndex = reference.action.GetBindingIndexForControl(reference.action.controls[0]); return(reference.action.bindings[bindingIndex].ToDisplayString()); } }
public void Samples_CanCreateRebindingUI() { var canvasGO = new GameObject(); canvasGO.AddComponent <Canvas>(); var actionLabelGO = new GameObject(); actionLabelGO.transform.parent = canvasGO.transform; var actionLabel = actionLabelGO.AddComponent <Text>(); var bindingLabelGO = new GameObject(); bindingLabelGO.transform.parent = canvasGO.transform; var bindingLabel = bindingLabelGO.AddComponent <Text>(); var asset = ScriptableObject.CreateInstance <InputActionAsset>(); var actionMap = asset.AddActionMap("map"); var action = actionMap.AddAction("action", binding: "<Mouse>/leftButton"); var go = new GameObject(); var rebind = go.AddComponent <RebindActionUI>(); rebind.bindingId = action.bindings[0].id.ToString(); rebind.actionReference = InputActionReference.Create(action); rebind.actionLabel = actionLabel; rebind.bindingText = bindingLabel; Assert.That(bindingLabel.text, Is.EqualTo("LMB")); Assert.That(actionLabel.text, Is.EqualTo("action")); // Go through rebind. var keyboard = InputSystem.AddDevice <Keyboard>(); rebind.StartInteractiveRebind(); Assert.That(rebind.ongoingRebind, Is.Not.Null); Assert.That(rebind.ongoingRebind.started, Is.True); Press(keyboard.spaceKey); currentTime += 2; InputSystem.Update(); Assert.That(rebind.ongoingRebind, Is.Null); Assert.That(action.bindings[0].overridePath, Is.EqualTo("<Keyboard>/space")); Assert.That(bindingLabel.text, Is.EqualTo("Space")); }
private static void SwapAction(ref InputActionReference property, InputActionReference newValue, bool actionsHooked, Action <InputAction.CallbackContext> actionCallback) { if (property != null && actionsHooked) { property.action.performed -= actionCallback; property.action.canceled -= actionCallback; } property = newValue; if (newValue != null && actionsHooked) { property.action.performed += actionCallback; property.action.canceled += actionCallback; } }
void DrawInputProviderButtonInInspector() { bool needsButton = false; for (int i = 0; !needsButton && i < targets.Length; ++i) { var vcam = targets[i] as CinemachineVirtualCameraBase; if (vcam.RequiresUserInput() && vcam.GetComponent <AxisState.IInputAxisProvider>() == null) { needsButton = true; } } if (!needsButton) { return; } EditorGUILayout.Space(); EditorGUILayout.HelpBox( "The InputSystem package is installed, but it is not used to control this vcam.", MessageType.Info); var rect = EditorGUILayout.GetControlRect(true); rect.x += EditorGUIUtility.labelWidth; rect.width -= EditorGUIUtility.labelWidth; if (GUI.Button(rect, s_InputProviderAddLabel)) { if (s_InputActionReference == null) { s_InputActionReference = (InputActionReference)AssetDatabase.LoadAllAssetsAtPath( "Packages/com.unity.inputsystem/InputSystem/Plugins/PlayerInput/DefaultInputActions.inputactions"). FirstOrDefault(x => x.name == "Player/Look"); } Undo.SetCurrentGroupName("Add CinemachineInputProvider"); for (int i = 0; i < targets.Length; ++i) { var vcam = targets[i] as CinemachineVirtualCameraBase; if (vcam.GetComponent <AxisState.IInputAxisProvider>() != null) { continue; } var inputProvider = Undo.AddComponent <CinemachineInputProvider>(vcam.gameObject); inputProvider.XYAxis = s_InputActionReference; } } EditorGUILayout.Space(); }
void EnableAction(ref InputActionReference actionReference, ref InputAction action, Action <InputAction.CallbackContext> performedCallback = null) { DisableAction(ref action); if (actionReference != null) { action = actionReference.action; } if (action != null) { if (performedCallback != null) { action.performed += performedCallback; } action.Enable(); } }
private void Start() { playerInput.SwitchCurrentActionMap("Character"); //menuActionMap.Disable(); if (UiMenu != null) { if (UiInstance == null) { UiInstance = Instantiate(UiMenu, UiParentInstance.transform); //need to assign the input system UI input module with the correct action asset - because there is a bug that clears the assignment in inspector after every play. var inputModule = UiInstance.GetComponent <InputSystemUIInputModule>(); inputModule.actionsAsset = playerInput.actions; //need to hook up all the actions because it is not automatically assigned. inputModule.move = InputActionReference.Create(playerInput.actions["Menu/Navigate"]); //playerInput.uiInputModule = inputModule; UiInstance.SetActive(false); } } }
public void SetInputUser(InputUser inputUser, InputDevice controller) { m_CurrentController = (Gamepad)controller; m_Controls = (Controls)inputUser.actions; InputSystemUIInputModule inputModule = transform.parent.parent.GetComponentInChildren <InputSystemUIInputModule>(); m_CharacterControl = GetComponentInChildren <CharacterControl>(); foreach (InputAction action in inputUser.actions) { PlayerInput.ActionEvent newEvent = new PlayerInput.ActionEvent(action); /* ADD one of these to the case to invoke the action * action.performed += newEvent.Invoke; * action.canceled += newEvent.Invoke; * action.started += newEvent.Invoke; */ switch (action.name) { case "Selection": if (inputModule != null) { InputActionReference selectionRef = ScriptableObject.CreateInstance <InputActionReference>(); selectionRef.Set(action); inputModule.move = selectionRef; } break; case "Select": action.performed += newEvent.Invoke; if (inputModule != null) { InputActionReference selectRef = ScriptableObject.CreateInstance <InputActionReference>(); selectRef.Set(action); inputModule.submit = selectRef; } break; case "Start": break; case "Rotate": newEvent.AddListener(Rotate); action.started += newEvent.Invoke; action.performed += newEvent.Invoke; action.canceled += newEvent.Invoke; break; case "Movement": newEvent.AddListener(m_CharacterControl.Movement); action.started += newEvent.Invoke; action.performed += newEvent.Invoke; action.canceled += newEvent.Invoke; break; case "Jump": m_SkipEvents.Add(newEvent); newEvent.AddListener(m_CharacterControl.Jump); action.performed += newEvent.Invoke; break; case "Push": newEvent.AddListener(m_CharacterControl.Push); action.performed += newEvent.Invoke; break; case "Interact": newEvent.AddListener(m_CharacterControl.Interact); action.performed += newEvent.Invoke; break; default: Debug.Log("There is no functionality yet for action: " + action.name + " in action map: " + action.actionMap); break; } } }
public void Samples_RebindingUI_SuppressingEventsDoesNotInterfereWithUIInput() { var keyboard = InputSystem.AddDevice <Keyboard>(); var asset = ScriptableObject.CreateInstance <InputActionAsset>(); var actionMap = asset.AddActionMap("map"); var action = actionMap.AddAction("action", binding: "<Keyboard>/a"); var canvasGO = new GameObject(); canvasGO.SetActive(false); canvasGO.AddComponent <Canvas>(); // Set up UI input module. var eventSystemGO = new GameObject(); eventSystemGO.SetActive(false); var eventSystem = eventSystemGO.AddComponent <TestEventSystem>(); var uiInputModule = eventSystemGO.AddComponent <InputSystemUIInputModule>(); var inputActions = new DefaultInputActions().asset; uiInputModule.actionsAsset = inputActions; uiInputModule.submit = InputActionReference.Create(inputActions["submit"]); var bindingButtonGO = new GameObject(); bindingButtonGO.transform.parent = canvasGO.transform; var bindingButton = bindingButtonGO.AddComponent <Button>(); var bindingLabelGO = new GameObject(); bindingLabelGO.transform.parent = bindingButtonGO.transform; var bindingLabel = bindingLabelGO.AddComponent <Text>(); var rebind = bindingButtonGO.AddComponent <RebindActionUI>(); rebind.bindingId = action.bindings[0].id.ToString(); rebind.actionReference = InputActionReference.Create(action); rebind.bindingText = bindingLabel; bindingButton.onClick.AddListener(rebind.StartInteractiveRebind); canvasGO.SetActive(true); eventSystemGO.SetActive(true); eventSystem.SetSelectedGameObject(bindingButtonGO); eventSystem.InvokeUpdate(); // Initial update switches the input module. Assert.That(rebind.ongoingRebind, Is.Null); Assert.That(bindingLabel.text, Is.EqualTo("A")); // As soon as the submit hits, the rebind starts -- which in turn enables suppression // of events. This means that the enter key release event will not reach the UI. The // UI should be fine with that. PressAndRelease(keyboard.enterKey); eventSystem.InvokeUpdate(); Assert.That(rebind.ongoingRebind, Is.Not.Null); Assert.That(rebind.ongoingRebind.started, Is.True); Assert.That(rebind.ongoingRebind.candidates, Is.Empty); Assert.That(bindingLabel.text, Is.EqualTo("<Waiting...>")); Assert.That(inputActions["submit"].inProgress, Is.False); Press(keyboard.bKey); eventSystem.InvokeUpdate(); Assert.That(rebind.ongoingRebind, Is.Not.Null); Assert.That(rebind.ongoingRebind.started, Is.True); Assert.That(rebind.ongoingRebind.candidates, Is.EquivalentTo(new[] { keyboard.bKey })); Assert.That(bindingLabel.text, Is.EqualTo("<Waiting...>")); Assert.That(inputActions["submit"].inProgress, Is.False); // Expire rebind wait time. currentTime += 1; InputSystem.Update(); Assert.That(rebind.ongoingRebind, Is.Null); Assert.That(bindingLabel.text, Is.EqualTo("B")); Assert.That(inputActions["submit"].inProgress, Is.False); // Start another rebind via "Submit". PressAndRelease(keyboard.enterKey); eventSystem.InvokeUpdate(); Assert.That(rebind.ongoingRebind, Is.Not.Null); Assert.That(rebind.ongoingRebind.started, Is.True); Assert.That(rebind.ongoingRebind.candidates, Is.Empty); Assert.That(bindingLabel.text, Is.EqualTo("<Waiting...>")); }
public InputSystemItem(string actionName, InputActionReference action) { this.actionName = actionName; this.action = action; }