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")); }
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 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 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 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); }
// 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; }
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 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 IEnumerator MouseActions_CanDriveUI() { // Create devices. var mouse = InputSystem.AddDevice <Mouse>(); var objects = CreateScene(); var uiModule = objects.uiModule; var eventSystem = objects.eventSystem; var leftChildGameObject = objects.leftGameObject; var leftChildReceiver = leftChildGameObject != null?leftChildGameObject.GetComponent <UICallbackReceiver>() : null; var rightChildGameObject = objects.rightGameObject; var rightChildReceiver = rightChildGameObject != null?rightChildGameObject.GetComponent <UICallbackReceiver>() : null; // Create asset var asset = ScriptableObject.CreateInstance <InputActionAsset>(); // Create actions. var map = new InputActionMap("map"); asset.AddActionMap(map); var pointAction = map.AddAction("point"); var leftClickAction = map.AddAction("leftClick"); var rightClickAction = map.AddAction("rightClick"); var middleClickAction = map.AddAction("middleClick"); var scrollAction = map.AddAction("scroll"); // Create bindings. pointAction.AddBinding(mouse.position); leftClickAction.AddBinding(mouse.leftButton); rightClickAction.AddBinding(mouse.rightButton); middleClickAction.AddBinding(mouse.middleButton); scrollAction.AddBinding(mouse.scroll); // Wire up actions. // NOTE: In a normal usage scenario, the user would wire these up in the inspector. uiModule.point = InputActionReference.Create(pointAction); uiModule.leftClick = InputActionReference.Create(leftClickAction); uiModule.middleClick = InputActionReference.Create(middleClickAction); uiModule.rightClick = InputActionReference.Create(rightClickAction); uiModule.scrollWheel = InputActionReference.Create(scrollAction); // Enable the whole thing. map.Enable(); // We need to wait a frame to let the underlying canvas update and properly order the graphics images for raycasting. yield return(null); // Reset initial selection leftChildReceiver.Reset(); // Move mouse over left child. InputSystem.QueueStateEvent(mouse, new MouseState { position = new Vector2(100, 100) }); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Enter)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); // Check basic down/up InputSystem.QueueStateEvent(mouse, new MouseState { position = new Vector2(100, 100), buttons = 1 << (int)MouseButton.Left }); InputSystem.QueueStateEvent(mouse, new MouseState { position = new Vector2(100, 100), buttons = 0 }); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(4)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Down)); Assert.That(leftChildReceiver.events[0].data, Is.TypeOf <PointerEventData>()); Assert.That((leftChildReceiver.events[0].data as PointerEventData).button, Is.EqualTo(PointerEventData.InputButton.Left)); Assert.That(leftChildReceiver.events[1].type, Is.EqualTo(EventType.PotentialDrag)); Assert.That(leftChildReceiver.events[2].type, Is.EqualTo(EventType.Up)); Assert.That(leftChildReceiver.events[3].type, Is.EqualTo(EventType.Click)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); // Check down and drag InputSystem.QueueStateEvent(mouse, new MouseState { position = new Vector2(100, 100), buttons = 1 << (int)MouseButton.Right }); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(2)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Down)); Assert.That(leftChildReceiver.events[0].data, Is.TypeOf <PointerEventData>()); Assert.That((leftChildReceiver.events[0].data as PointerEventData).button, Is.EqualTo(PointerEventData.InputButton.Right)); Assert.That(leftChildReceiver.events[1].type, Is.EqualTo(EventType.PotentialDrag)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); // Move to new location on left child InputSystem.QueueDeltaStateEvent(mouse.position, new Vector2(100, 200)); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(2)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.BeginDrag)); Assert.That(leftChildReceiver.events[1].type, Is.EqualTo(EventType.Dragging)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); // Move children InputSystem.QueueDeltaStateEvent(mouse.position, new Vector2(350, 200)); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(2)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Exit)); Assert.That(leftChildReceiver.events[1].type, Is.EqualTo(EventType.Dragging)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(rightChildReceiver.events[0].type, Is.EqualTo(EventType.Enter)); rightChildReceiver.Reset(); // Release button InputSystem.QueueStateEvent(mouse, new MouseState { position = new Vector2(350, 200), buttons = 0 }); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(2)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Up)); Assert.That(leftChildReceiver.events[1].type, Is.EqualTo(EventType.EndDrag)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(rightChildReceiver.events[0].type, Is.EqualTo(EventType.Drop)); rightChildReceiver.Reset(); // Check Scroll InputSystem.QueueDeltaStateEvent(mouse.scroll, Vector2.one); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(0)); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(rightChildReceiver.events[0].type, Is.EqualTo(EventType.Scroll)); rightChildReceiver.Reset(); }
// Check that two players can have separate UI and control it using separate gamepads, using MultiplayerEventSystem. public IEnumerator JoystickActions_MultiplayerEventSystemKeepsPerPlayerSelection() { // Create devices. var gamepads = new[] { InputSystem.AddDevice <Gamepad>(), InputSystem.AddDevice <Gamepad>() }; var players = new[] { CreateScene(0, 240), CreateScene(240, 480) }; for (var i = 0; i < 2; i++) { // Create asset var asset = ScriptableObject.CreateInstance <InputActionAsset>(); // Create actions. var map = new InputActionMap("map"); asset.AddActionMap(map); var moveAction = map.AddAction("move"); var submitAction = map.AddAction("submit"); var cancelAction = map.AddAction("cancel"); // Create bindings. moveAction.AddBinding(gamepads[i].leftStick); submitAction.AddBinding(gamepads[i].buttonSouth); cancelAction.AddBinding(gamepads[i].buttonEast); // Wire up actions. players[i].uiModule.move = InputActionReference.Create(moveAction); players[i].uiModule.submit = InputActionReference.Create(submitAction); players[i].uiModule.cancel = InputActionReference.Create(cancelAction); players[i].leftChildReceiver.moveTo = players[i].rightGameObject; players[i].rightChildReceiver.moveTo = players[i].leftGameObject; // Enable the whole thing. map.Enable(); } // We need to wait a frame to let the underlying canvas update and properly order the graphics images for raycasting. yield return(null); Assert.That(players[0].eventSystem.currentSelectedGameObject, Is.SameAs(players[0].leftGameObject)); Assert.That(players[1].eventSystem.currentSelectedGameObject, Is.SameAs(players[1].leftGameObject)); // Reset initial selection players[0].leftChildReceiver.Reset(); players[1].leftChildReceiver.Reset(); // Check Player 0 Move Axes InputSystem.QueueDeltaStateEvent(gamepads[0].leftStick, new Vector2(1.0f, 0.0f)); InputSystem.Update(); foreach (var player in players) { Assert.That(player.leftChildReceiver.events, Has.Count.EqualTo(0)); Assert.That(player.rightChildReceiver.events, Has.Count.EqualTo(0)); player.eventSystem.InvokeUpdate(); } Assert.That(players[0].eventSystem.currentSelectedGameObject, Is.SameAs(players[0].rightGameObject)); Assert.That(players[1].eventSystem.currentSelectedGameObject, Is.SameAs(players[1].leftGameObject)); Assert.That(players[0].leftChildReceiver.events, Has.Count.EqualTo(2)); Assert.That(players[0].leftChildReceiver.events[0].type, Is.EqualTo(EventType.Move)); Assert.That(players[0].leftChildReceiver.events[1].type, Is.EqualTo(EventType.Deselect)); players[0].leftChildReceiver.Reset(); Assert.That(players[0].rightChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(players[0].rightChildReceiver.events[0].type, Is.EqualTo(EventType.Select)); players[0].rightChildReceiver.Reset(); foreach (var player in players) { Assert.That(player.leftChildReceiver.events, Has.Count.EqualTo(0)); Assert.That(player.rightChildReceiver.events, Has.Count.EqualTo(0)); player.eventSystem.InvokeUpdate(); } // Check Player 0 Submit InputSystem.QueueStateEvent(gamepads[0], new GamepadState { buttons = 1 << (int)GamepadButton.South }); InputSystem.Update(); foreach (var player in players) { Assert.That(player.leftChildReceiver.events, Has.Count.EqualTo(0)); Assert.That(player.rightChildReceiver.events, Has.Count.EqualTo(0)); player.eventSystem.InvokeUpdate(); } Assert.That(players[0].rightChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(players[0].rightChildReceiver.events[0].type, Is.EqualTo(EventType.Submit)); players[0].rightChildReceiver.Reset(); // Check Player 1 Submit InputSystem.QueueStateEvent(gamepads[1], new GamepadState { buttons = 1 << (int)GamepadButton.South }); InputSystem.Update(); foreach (var player in players) { Assert.That(player.leftChildReceiver.events, Has.Count.EqualTo(0)); Assert.That(player.rightChildReceiver.events, Has.Count.EqualTo(0)); player.eventSystem.InvokeUpdate(); } Assert.That(players[1].leftChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(players[1].leftChildReceiver.events[0].type, Is.EqualTo(EventType.Submit)); players[1].leftChildReceiver.Reset(); foreach (var player in players) { Assert.That(player.leftChildReceiver.events, Has.Count.EqualTo(0)); Assert.That(player.rightChildReceiver.events, Has.Count.EqualTo(0)); } }
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 IEnumerator JoystickActions_CanDriveUI() { // Create devices. var gamepad = InputSystem.AddDevice <Gamepad>(); var objects = CreateScene(); var uiModule = objects.uiModule; var eventSystem = objects.eventSystem; var leftChildGameObject = objects.leftGameObject; var leftChildReceiver = leftChildGameObject != null?leftChildGameObject.GetComponent <UICallbackReceiver>() : null; var rightChildGameObject = objects.rightGameObject; var rightChildReceiver = rightChildGameObject != null?rightChildGameObject.GetComponent <UICallbackReceiver>() : null; // Create asset var asset = ScriptableObject.CreateInstance <InputActionAsset>(); // Create actions. var map = new InputActionMap("map"); asset.AddActionMap(map); var moveAction = map.AddAction("move"); var submitAction = map.AddAction("submit"); var cancelAction = map.AddAction("cancel"); // Create bindings. moveAction.AddBinding(gamepad.leftStick); submitAction.AddBinding(gamepad.buttonSouth); cancelAction.AddBinding(gamepad.buttonEast); // Wire up actions. // NOTE: In a normal usage scenario, the user would wire these up in the inspector. uiModule.move = InputActionReference.Create(moveAction); uiModule.submit = InputActionReference.Create(submitAction); uiModule.cancel = InputActionReference.Create(cancelAction); // Enable the whole thing. map.Enable(); // We need to wait a frame to let the underlying canvas update and properly order the graphics images for raycasting. yield return(null); // Test Selection eventSystem.SetSelectedGameObject(leftChildGameObject); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Select)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); // Check Move Axes // Fixme: replacing this with Set(gamepads[0].leftStick, new Vector2(1, 0)); throws a NRE. InputSystem.QueueDeltaStateEvent(gamepad.leftStick, new Vector2(1.0f, 0.0f)); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Move)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); // Check Submit InputSystem.QueueStateEvent(gamepad, new GamepadState { buttons = 1 << (int)GamepadButton.South }); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Submit)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); // Check Cancel InputSystem.QueueStateEvent(gamepad, new GamepadState { buttons = 1 << (int)GamepadButton.East }); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Cancel)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); // Check Selection Swap eventSystem.SetSelectedGameObject(rightChildGameObject); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Deselect)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(rightChildReceiver.events[0].type, Is.EqualTo(EventType.Select)); rightChildReceiver.Reset(); // Check Deselect eventSystem.SetSelectedGameObject(null); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(0)); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(rightChildReceiver.events[0].type, Is.EqualTo(EventType.Deselect)); rightChildReceiver.Reset(); }
// Check that two players can have separate UI, and that both selections will stay active when clicking on UI with the mouse, // using MultiPlayerEventSystem.playerRoot to match UI to the players. public IEnumerator MouseActions_MultiplayerEventSystemKeepsPerPlayerSelection() { // Create devices. var mouse = InputSystem.AddDevice <Mouse>(); var players = new[] { CreateScene(0, 240), CreateScene(240, 480) }; // Create asset var asset = ScriptableObject.CreateInstance <InputActionAsset>(); // Create actions. var map = new InputActionMap("map"); asset.AddActionMap(map); var pointAction = map.AddAction("point"); var leftClickAction = map.AddAction("leftClick"); var rightClickAction = map.AddAction("rightClick"); var middleClickAction = map.AddAction("middleClick"); var scrollAction = map.AddAction("scroll"); // Create bindings. pointAction.AddBinding(mouse.position); leftClickAction.AddBinding(mouse.leftButton); rightClickAction.AddBinding(mouse.rightButton); middleClickAction.AddBinding(mouse.middleButton); scrollAction.AddBinding(mouse.scroll); // Wire up actions. // NOTE: In a normal usage scenario, the user would wire these up in the inspector. foreach (var player in players) { player.uiModule.point = InputActionReference.Create(pointAction); player.uiModule.leftClick = InputActionReference.Create(leftClickAction); player.uiModule.middleClick = InputActionReference.Create(middleClickAction); player.uiModule.rightClick = InputActionReference.Create(rightClickAction); player.uiModule.scrollWheel = InputActionReference.Create(scrollAction); player.eventSystem.SetSelectedGameObject(null); } // Enable the whole thing. map.Enable(); // We need to wait a frame to let the underlying canvas update and properly order the graphics images for raycasting. yield return(null); // Click left gameObject of player 0 InputSystem.QueueStateEvent(mouse, new MouseState { position = new Vector2(100, 100), buttons = 1 << (int)MouseButton.Left }); InputSystem.QueueStateEvent(mouse, new MouseState { position = new Vector2(100, 100), buttons = 0 }); InputSystem.Update(); foreach (var player in players) { player.eventSystem.InvokeUpdate(); } Assert.That(players[0].eventSystem.currentSelectedGameObject, Is.SameAs(players[0].leftGameObject)); Assert.That(players[1].eventSystem.currentSelectedGameObject, Is.Null); // Click right gameObject of player 1 InputSystem.QueueStateEvent(mouse, new MouseState { position = new Vector2(400, 300), buttons = 1 << (int)MouseButton.Left }); InputSystem.QueueStateEvent(mouse, new MouseState { position = new Vector2(400, 300), buttons = 0 }); InputSystem.Update(); foreach (var player in players) { player.eventSystem.InvokeUpdate(); } Assert.That(players[0].eventSystem.currentSelectedGameObject, Is.SameAs(players[0].leftGameObject)); Assert.That(players[1].eventSystem.currentSelectedGameObject, Is.SameAs(players[1].rightGameObject)); // Click right gameObject of player 0 InputSystem.QueueStateEvent(mouse, new MouseState { position = new Vector2(400, 100), buttons = 1 << (int)MouseButton.Left }); InputSystem.QueueStateEvent(mouse, new MouseState { position = new Vector2(400, 100), buttons = 0 }); InputSystem.Update(); foreach (var player in players) { player.eventSystem.InvokeUpdate(); } Assert.That(players[0].eventSystem.currentSelectedGameObject, Is.SameAs(players[0].rightGameObject)); Assert.That(players[1].eventSystem.currentSelectedGameObject, Is.SameAs(players[1].rightGameObject)); }
public static void BindUIActions(this InputSystemUIInputModule uiInputModule, DemoControls.MenuActions menuActions) { uiInputModule.move = InputActionReference.Create(menuActions.navigate); uiInputModule.leftClick = InputActionReference.Create(menuActions.click); }
public IEnumerator Devices_CanHaveOnScreenJoystickControls() { foreach (var c in Camera.allCameras) { Object.Destroy(c.gameObject); } yield return(null); InputSystem.AddDevice <Touchscreen>(); // Set up a full UI scene with an on-screen stick and button. var eventSystemGO = new GameObject("EventSystem"); var eventSystem = eventSystemGO.AddComponent <TestEventSystem>(); var uiModule = eventSystemGO.AddComponent <InputSystemUIInputModule>(); eventSystem.OnApplicationFocus(true); var uiActions = new DefaultInputActions(); uiModule.actionsAsset = uiActions.asset; uiModule.leftClick = InputActionReference.Create(uiActions.UI.Click); uiModule.point = InputActionReference.Create(uiActions.UI.Point); var canvasGO = new GameObject("Canvas"); var canvasTransform = canvasGO.AddComponent <RectTransform>(); var canvas = canvasGO.AddComponent <Canvas>(); canvasGO.AddComponent <GraphicRaycaster>(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; var stickGO = new GameObject("Stick"); stickGO.SetActive(false); var stickTransform = stickGO.AddComponent <RectTransform>(); var stick = stickGO.AddComponent <OnScreenStick>(); stickGO.AddComponent <Image>(); stickTransform.SetParent(canvasTransform); stickTransform.anchorMin = new Vector2(0, 0); stickTransform.anchorMax = new Vector2(0, 0); stickTransform.anchoredPosition = new Vector2(100, 100); stickTransform.sizeDelta = new Vector2(100, 100); stick.controlPath = "<Gamepad>/leftStick"; stickGO.SetActive(true); var buttonGO = new GameObject("Button"); buttonGO.SetActive(false); var buttonTransform = buttonGO.AddComponent <RectTransform>(); var button = buttonGO.AddComponent <OnScreenButton>(); buttonGO.AddComponent <Button>(); buttonGO.AddComponent <Image>(); // Give it a Graphic so the raycaster sees it. buttonTransform.SetParent(canvasTransform); buttonTransform.anchorMin = new Vector2(0, 0); buttonTransform.anchorMax = new Vector2(0, 0); buttonTransform.anchoredPosition = new Vector2(300, 100); buttonTransform.sizeDelta = new Vector2(100, 100); button.controlPath = "<Gamepad>/buttonSouth"; buttonGO.SetActive(true); // Add player and hook it up to the gamepad. var playerActions = new DefaultInputActions(); var playerGO = new GameObject("Player"); playerGO.SetActive(false); var player = playerGO.AddComponent <PlayerInput>(); player.actions = playerActions.asset; player.defaultControlScheme = "Gamepad"; player.neverAutoSwitchControlSchemes = true; playerGO.SetActive(true); yield return(null); eventSystem.Update(); Assert.That(player.devices, Is.EquivalentTo(new[] { Gamepad.all[0] })); // Touch the stick and drag it upwards. BeginTouch(1, new Vector2(150, 150)); yield return(null); eventSystem.Update(); Assert.That(eventSystem.IsPointerOverGameObject(), Is.True); MoveTouch(1, new Vector2(150, 200)); yield return(null); eventSystem.Update(); InputSystem.Update(); // Stick is feeding events when responding to UI events. Assert.That(Gamepad.all[0].leftStick.ReadValue(), Is.EqualTo(new Vector2(0, 1)).Using(Vector2EqualityComparer.Instance)); // Press the button. BeginTouch(2, new Vector2(350, 150)); yield return(null); eventSystem.Update(); InputSystem.Update(); // Button is feeding events when responding to UI events. Assert.That(Gamepad.all[0].buttonSouth.isPressed, Is.True); // Release the button. EndTouch(2, new Vector2(351, 151)); yield return(null); eventSystem.Update(); InputSystem.Update(); // Button is feeding events when responding to UI events. Assert.That(Gamepad.all[0].buttonSouth.isPressed, Is.False); }
// Check that we can track multiple tracked devices separately, with different pointer ids. public IEnumerator TrackedDeviceActions_CanDriveUIMultipleTrackers() { // Create device. InputSystem.RegisterLayout <TestTrackedDevice>(); var trackedDevice = InputSystem.AddDevice <TestTrackedDevice>(); var trackedDevice2 = InputSystem.AddDevice <TestTrackedDevice>(); var objects = CreateScene(); var uiModule = objects.uiModule; var eventSystem = objects.eventSystem; var leftChildGameObject = objects.leftGameObject; var leftChildReceiver = leftChildGameObject != null?leftChildGameObject.GetComponent <UICallbackReceiver>() : null; var rightChildGameObject = objects.rightGameObject; var rightChildReceiver = rightChildGameObject != null?rightChildGameObject.GetComponent <UICallbackReceiver>() : null; // Create actions. var asset = ScriptableObject.CreateInstance <InputActionAsset>(); // Create actions. var map = new InputActionMap("map"); asset.AddActionMap(map); var trackedPositionAction = map.AddAction("position", type: InputActionType.PassThrough); var trackedOrientationAction = map.AddAction("orientation", type: InputActionType.PassThrough); var trackedSelectAction = map.AddAction("selection", type: InputActionType.PassThrough); trackedPositionAction.AddBinding("*/position"); trackedOrientationAction.AddBinding("*/orientation"); trackedSelectAction.AddBinding("*/select"); // Wire up actions. // NOTE: In a normal usage scenario, the user would wire these up in the inspector. uiModule.trackedDevicePosition = InputActionReference.Create(trackedPositionAction); uiModule.trackedDeviceOrientation = InputActionReference.Create(trackedOrientationAction); uiModule.trackedDeviceSelect = InputActionReference.Create(trackedSelectAction); // Enable the whole thing. map.Enable(); // We need to wait a frame to let the underlying canvas update and properly order the graphics images for raycasting. yield return(null); using (StateEvent.From(trackedDevice, out var stateEvent)) using (StateEvent.From(trackedDevice2, out var stateEvent2)) { // Reset to Defaults trackedDevice.position.WriteValueIntoEvent(Vector3.zero, stateEvent); trackedDevice.orientation.WriteValueIntoEvent(Quaternion.Euler(0.0f, -90.0f, 0.0f), stateEvent); trackedDevice.select.WriteValueIntoEvent(0f, stateEvent); InputSystem.QueueEvent(stateEvent); trackedDevice2.position.WriteValueIntoEvent(Vector3.zero, stateEvent2); trackedDevice2.orientation.WriteValueIntoEvent(Quaternion.Euler(0.0f, -90.0f, 0.0f), stateEvent2); trackedDevice2.select.WriteValueIntoEvent(0f, stateEvent2); InputSystem.QueueEvent(stateEvent2); InputSystem.Update(); leftChildReceiver.Reset(); rightChildReceiver.Reset(); // Move over left child. trackedDevice.orientation.WriteValueIntoEvent(Quaternion.Euler(0.0f, -30.0f, 0.0f), stateEvent); InputSystem.QueueEvent(stateEvent); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Enter)); Assert.That((leftChildReceiver.events[0].data as PointerEventData).pointerId, Is.EqualTo(1)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); trackedDevice2.orientation.WriteValueIntoEvent(Quaternion.Euler(0.0f, -30.0f, 0.0f), stateEvent2); InputSystem.QueueEvent(stateEvent2); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Enter)); Assert.That((leftChildReceiver.events[0].data as PointerEventData).pointerId, Is.EqualTo(2)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); // Check basic down/up trackedDevice.select.WriteValueIntoEvent(1f, stateEvent); InputSystem.QueueEvent(stateEvent); trackedDevice.select.WriteValueIntoEvent(0f, stateEvent); InputSystem.QueueEvent(stateEvent); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(4)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Down)); Assert.That((leftChildReceiver.events[0].data as PointerEventData).pointerId, Is.EqualTo(1)); Assert.That(leftChildReceiver.events[1].type, Is.EqualTo(EventType.PotentialDrag)); Assert.That((leftChildReceiver.events[1].data as PointerEventData).pointerId, Is.EqualTo(1)); Assert.That(leftChildReceiver.events[2].type, Is.EqualTo(EventType.Up)); Assert.That((leftChildReceiver.events[2].data as PointerEventData).pointerId, Is.EqualTo(1)); Assert.That(leftChildReceiver.events[3].type, Is.EqualTo(EventType.Click)); Assert.That((leftChildReceiver.events[3].data as PointerEventData).pointerId, Is.EqualTo(1)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); trackedDevice2.select.WriteValueIntoEvent(1f, stateEvent2); InputSystem.QueueEvent(stateEvent2); trackedDevice2.select.WriteValueIntoEvent(0f, stateEvent2); InputSystem.QueueEvent(stateEvent2); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(4)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Down)); Assert.That((leftChildReceiver.events[0].data as PointerEventData).pointerId, Is.EqualTo(2)); Assert.That(leftChildReceiver.events[1].type, Is.EqualTo(EventType.PotentialDrag)); Assert.That((leftChildReceiver.events[0].data as PointerEventData).pointerId, Is.EqualTo(2)); Assert.That(leftChildReceiver.events[2].type, Is.EqualTo(EventType.Up)); Assert.That((leftChildReceiver.events[0].data as PointerEventData).pointerId, Is.EqualTo(2)); Assert.That(leftChildReceiver.events[3].type, Is.EqualTo(EventType.Click)); Assert.That((leftChildReceiver.events[0].data as PointerEventData).pointerId, Is.EqualTo(2)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(0)); // Move to new location on right child trackedDevice.orientation.WriteValueIntoEvent(Quaternion.Euler(0.0f, 30.0f, 0.0f), stateEvent); InputSystem.QueueEvent(stateEvent); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Exit)); Assert.That((leftChildReceiver.events[0].data as PointerEventData).pointerId, Is.EqualTo(1)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(rightChildReceiver.events[0].type, Is.EqualTo(EventType.Enter)); Assert.That((rightChildReceiver.events[0].data as PointerEventData).pointerId, Is.EqualTo(1)); rightChildReceiver.Reset(); trackedDevice2.orientation.WriteValueIntoEvent(Quaternion.Euler(0.0f, 30.0f, 0.0f), stateEvent2); InputSystem.QueueEvent(stateEvent2); InputSystem.Update(); eventSystem.InvokeUpdate(); Assert.That(leftChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(leftChildReceiver.events[0].type, Is.EqualTo(EventType.Exit)); Assert.That((leftChildReceiver.events[0].data as PointerEventData).pointerId, Is.EqualTo(2)); leftChildReceiver.Reset(); Assert.That(rightChildReceiver.events, Has.Count.EqualTo(1)); Assert.That(rightChildReceiver.events[0].type, Is.EqualTo(EventType.Enter)); Assert.That((rightChildReceiver.events[0].data as PointerEventData).pointerId, Is.EqualTo(2)); rightChildReceiver.Reset(); } }