|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using NUnit.Framework; |
| 4 | +using UnityEngine; |
| 5 | +using UnityEngine.InputSystem; |
| 6 | +using UnityEngine.InputSystem.LowLevel; |
| 7 | +using UnityEngine.TestTools; |
| 8 | +using Is = UnityEngine.TestTools.Constraints.Is; |
| 9 | +using UnityEngineInternal.Input; |
| 10 | +using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch; |
| 11 | + |
| 12 | + |
| 13 | +partial class CoreTests |
| 14 | +{ |
| 15 | + [UnityTest] |
| 16 | + [Category("MouseEvents")] |
| 17 | + public IEnumerator MouseEvents_CanReceiveOnMouseDown() |
| 18 | + { |
| 19 | + var mouse = InputSystem.AddDevice<Mouse>(); |
| 20 | + |
| 21 | + var gameObject = GameObject.CreatePrimitive(PrimitiveType.Cube); |
| 22 | + gameObject.AddComponent<OnMouseEventsTest>(); |
| 23 | + gameObject.transform.position = Vector3.zero; |
| 24 | + var camera = new GameObject("MainCamera").AddComponent<Camera>(); |
| 25 | + camera.transform.position = new Vector3(0, 0, -2f); |
| 26 | + camera.tag = "MainCamera"; |
| 27 | + var vec = Camera.main.WorldToScreenPoint(gameObject.transform.position); |
| 28 | + SetMouse(mouse, new Vector2(vec.x, vec.y)); |
| 29 | + |
| 30 | + yield return null; |
| 31 | + |
| 32 | + Assert.That(gameObject.transform.position, Is.EqualTo(new Vector3(0, 0, 1)), "No MouseDown event received."); |
| 33 | + } |
| 34 | + |
| 35 | + unsafe void SetMouse(Mouse mouse, Vector2 pos, float pressed = 1f) |
| 36 | + { |
| 37 | + using (StateEvent.From(mouse, out var eventPtr)) |
| 38 | + { |
| 39 | + eventPtr.time = InputState.currentTime; |
| 40 | + mouse.position.WriteValueIntoEvent(pos, eventPtr); |
| 41 | + mouse.leftButton.WriteValueIntoEvent(pressed, eventPtr); |
| 42 | + NativeInputRuntime.instance.QueueEvent(eventPtr); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +public class OnMouseEventsTest : MonoBehaviour |
| 48 | +{ |
| 49 | + void OnMouseDown() |
| 50 | + { |
| 51 | + gameObject.transform.position = new Vector3(0, 0, 1); |
| 52 | + } |
| 53 | +} |
0 commit comments