22using System ;
33using System . Collections ;
44using NUnit . Framework ;
5+ using Unity . Collections . LowLevel . Unsafe ;
56using UnityEngine ;
67using UnityEngine . InputSystem ;
78using UnityEngine . InputSystem . LowLevel ;
89using UnityEngine . TestTools ;
910using Is = UnityEngine . TestTools . Constraints . Is ;
1011using UnityEngineInternal . Input ;
12+ using TouchPhase = UnityEngine . InputSystem . TouchPhase ;
1113
1214
1315partial class CoreTests
@@ -17,8 +19,10 @@ public override void TearDown()
1719 {
1820 var mouse = InputSystem . AddDevice < Mouse > ( ) ;
1921 var pen = InputSystem . AddDevice < Pen > ( ) ;
22+ var touchscreen = InputSystem . AddDevice < Touchscreen > ( ) ;
2023 SetMouse ( mouse , Vector2 . zero , 0f ) ;
2124 SetPen ( pen , Vector2 . zero , 0f ) ;
25+ SetTouch ( touchscreen , Vector2 . zero , TouchPhase . Ended ) ;
2226 base . TearDown ( ) ;
2327 }
2428
@@ -268,6 +272,55 @@ unsafe void SetPen(Pen pen, Vector2 pos, float pressed)
268272 }
269273
270274 #endregion
275+
276+ #region Touch
277+
278+ [ UnityTest ]
279+ [ Category ( "MouseEvents" ) ]
280+ public IEnumerator TouchEvents_CanReceiveOnMouseDown ( )
281+ {
282+ var touch = InputSystem . AddDevice < Touchscreen > ( ) ;
283+
284+ var gameObject = SetUpScene ( ) ;
285+ gameObject . AddComponent < OnMouseEventsTest > ( ) ;
286+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
287+ SetTouch ( touch , new Vector2 ( vec . x , vec . y ) , TouchPhase . Began ) ;
288+
289+ yield return null ;
290+
291+ Assert . That ( gameObject . transform . position , Is . EqualTo ( new Vector3 ( 0 , 0 , 1 ) ) , "No MouseDown event received." ) ;
292+ }
293+
294+ private long m_eventSize = UnsafeUtility . SizeOf < StateEvent > ( ) + ( uint ) UnsafeUtility . SizeOf < TouchState > ( ) - StateEvent . kStateDataSizeToSubtract ;
295+
296+ // we need to use the NativeInputRuntime to queue the events in order to get the input events in native
297+ // code, where the touch events are processed.
298+ unsafe void SetTouch ( Touchscreen touch , Vector2 pos , TouchPhase phase )
299+ {
300+ var state = new TouchState
301+ {
302+ touchId = 1 ,
303+ phase = phase ,
304+ position = pos ,
305+ delta = default ,
306+ pressure = 1f ,
307+ displayIndex = 0 ,
308+ } ;
309+
310+ var stateEvent =
311+ new StateEvent
312+ {
313+ baseEvent = new InputEvent ( StateEvent . Type , ( int ) m_eventSize , touch . deviceId , InputState . currentTime ) ,
314+ stateFormat = state . format
315+ } ;
316+
317+ var ptr = stateEvent . stateData ;
318+ UnsafeUtility . MemCpy ( ptr , UnsafeUtility . AddressOf ( ref state ) , m_eventSize ) ;
319+
320+ NativeInputRuntime . instance . QueueEvent ( ( InputEvent * ) UnsafeUtility . AddressOf ( ref stateEvent ) ) ;
321+ }
322+
323+ #endregion
271324}
272325
273326internal class OnMouseEventsTest : MonoBehaviour
0 commit comments