Skip to content

Commit 7653046

Browse files
committed
Decouple Unity Remote from IInputRuntime
1 parent 670f071 commit 7653046

6 files changed

Lines changed: 85 additions & 121 deletions

File tree

Assets/Tests/InputSystem/Plugins/UnityRemoteTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal class UnityRemoteTests : CoreTestsFixture
1313
public override void Setup()
1414
{
1515
base.Setup();
16-
runtime.onUnityRemoteMessage = UnityRemoteSupport.GetMessageHandlerForTesting();
16+
UnityRemoteSupport.s_TestMessageDispatch = UnityRemoteSupport.GetMessageHandlerForTesting();
1717
}
1818

1919
public override void TearDown()
@@ -125,7 +125,7 @@ public void Remote_CanReceiveGyroscopeInputFromUnityRemote()
125125
// Now the app enables the gyro.
126126
InputSystem.EnableDevice(Gyroscope.current);
127127

128-
Assert.That(runtime.unityRemoteGyroEnabled, Is.True);
128+
Assert.That(UnityRemoteSupport.unityRemoteGyroEnabled, Is.True);
129129
Assert.That(Gyroscope.current.enabled, Is.True);
130130
Assert.That(AttitudeSensor.current.enabled, Is.False);
131131
Assert.That(GravitySensor.current.enabled, Is.False);
@@ -160,7 +160,7 @@ public void Remote_CanReceiveGyroscopeInputFromUnityRemote()
160160
InputSystem.EnableDevice(GravitySensor.current);
161161
InputSystem.EnableDevice(LinearAccelerationSensor.current);
162162

163-
Assert.That(runtime.unityRemoteGyroEnabled, Is.True);
163+
Assert.That(UnityRemoteSupport.unityRemoteGyroEnabled, Is.True);
164164
Assert.That(AttitudeSensor.current.enabled, Is.True);
165165
Assert.That(GravitySensor.current.enabled, Is.True);
166166
Assert.That(LinearAccelerationSensor.current.enabled, Is.True);
@@ -200,7 +200,7 @@ public void Remote_CanReceiveGyroscopeInputFromUnityRemote()
200200
// Set update interval.
201201
Gyroscope.current.samplingFrequency = 123.456f;
202202

203-
Assert.That(runtime.unityRemoteGyroUpdateInterval, Is.EqualTo(123.456f));
203+
Assert.That(UnityRemoteSupport.unityRemoteGyroUpdateInterval, Is.EqualTo(123.456f));
204204
Assert.That(Gyroscope.current.samplingFrequency, Is.EqualTo(123.456f));
205205

206206
SendUnityRemoteMessage(new UnityRemoteSupport.GoodbyeMessage());
@@ -281,14 +281,14 @@ public void TODO_Remote_CanReceiveJoystickInputFromUnityRemote()
281281
private unsafe void SendUnityRemoteMessage<TMessage>(TMessage message)
282282
where TMessage : unmanaged, UnityRemoteSupport.IUnityRemoteMessage
283283
{
284-
if (runtime.onUnityRemoteMessage == null)
284+
if (UnityRemoteSupport.s_TestMessageDispatch == null)
285285
return;
286286

287287
var ptr = UnsafeUtility.AddressOf(ref message);
288288
*(byte*)ptr = message.staticType;
289289
*(int*)((byte*)ptr + 1) = UnsafeUtility.SizeOf<TMessage>();
290290

291-
runtime.onUnityRemoteMessage(new IntPtr(ptr));
291+
UnityRemoteSupport.s_TestMessageDispatch(new IntPtr(ptr));
292292
}
293293
}
294294
#endif // UNITY_EDITOR

Packages/com.unity.inputsystem/InputSystem/Editor/InputSystemEditorInitializer.cs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -62,48 +62,6 @@ private static void UnregisterWantsToQuit(Func<bool> handler)
6262
EditorApplication.wantsToQuit -= handler;
6363
}
6464

65-
// Unity Remote support
66-
private static Func<IntPtr, bool> s_CurrentUnityRemoteMessageHandler;
67-
68-
private static void SetUnityRemoteMessageHandler(Func<IntPtr, bool> handler)
69-
{
70-
if (s_CurrentUnityRemoteMessageHandler != null)
71-
{
72-
var removeMethod = GetUnityRemoteAPIMethod("RemoveMessageHandler");
73-
removeMethod?.Invoke(null, new object[] { s_CurrentUnityRemoteMessageHandler });
74-
}
75-
76-
s_CurrentUnityRemoteMessageHandler = handler;
77-
78-
if (handler != null)
79-
{
80-
var addMethod = GetUnityRemoteAPIMethod("AddMessageHandler");
81-
addMethod?.Invoke(null, new object[] { handler });
82-
}
83-
}
84-
85-
private static void SetUnityRemoteGyroEnabled(bool value)
86-
{
87-
var setMethod = GetUnityRemoteAPIMethod("SetGyroEnabled");
88-
setMethod?.Invoke(null, new object[] { value });
89-
}
90-
91-
private static void SetUnityRemoteGyroUpdateInterval(float interval)
92-
{
93-
var setMethod = GetUnityRemoteAPIMethod("SetGyroUpdateInterval");
94-
setMethod?.Invoke(null, new object[] { interval });
95-
}
96-
97-
private static System.Reflection.MethodInfo GetUnityRemoteAPIMethod(string methodName)
98-
{
99-
var editorAssembly = typeof(EditorApplication).Assembly;
100-
var genericRemoteClass = editorAssembly.GetType("UnityEditor.Remote.GenericRemote");
101-
if (genericRemoteClass == null)
102-
return null;
103-
104-
return genericRemoteClass.GetMethod(methodName);
105-
}
106-
10765
private static void SendEditorAnalytic(InputAnalytics.IInputAnalytic analytic)
10866
{
10967
#if ENABLE_CLOUD_SERVICES_ANALYTICS
@@ -343,10 +301,6 @@ private static void RegisterNativeInputRuntimeHooks()
343301
nativeRuntime.m_RegisterWantsToQuit = RegisterWantsToQuit;
344302
nativeRuntime.m_UnregisterWantsToQuit = UnregisterWantsToQuit;
345303

346-
nativeRuntime.m_SetUnityRemoteMessageHandler = SetUnityRemoteMessageHandler;
347-
nativeRuntime.m_SetUnityRemoteGyroEnabledCallback = SetUnityRemoteGyroEnabled;
348-
nativeRuntime.m_SetUnityRemoteGyroUpdateIntervalCallback = SetUnityRemoteGyroUpdateInterval;
349-
350304
nativeRuntime.m_SendEditorAnalytic = SendEditorAnalytic;
351305
}
352306

Packages/com.unity.inputsystem/InputSystem/Editor/Plugins/UnityRemote/UnityRemoteSupport.cs

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if UNITY_EDITOR
22
using System;
3+
using System.Reflection;
34
using System.Runtime.InteropServices;
45
using UnityEditor;
56
using UnityEngine.InputSystem.LowLevel;
@@ -18,27 +19,87 @@ internal static class UnityRemoteSupport
1819
{
1920
public static bool isConnected => s_State.connected;
2021

22+
private static Func<IntPtr, bool> s_RegisteredNativeHandler;
23+
24+
/// <summary>
25+
/// When set (by tests), this delegate receives messages instead of the native path.
26+
/// Cleared in <see cref="ResetGlobalState"/>.
27+
/// </summary>
28+
internal static Func<IntPtr, bool> s_TestMessageDispatch;
29+
30+
/// <summary>
31+
/// Set whenever gyro commands are forwarded to the editor native API (for test observability).
32+
/// </summary>
33+
internal static bool? unityRemoteGyroEnabled;
34+
/// <summary>
35+
/// Set whenever the gyro update interval is forwarded to the editor native API (for test observability).
36+
/// </summary>
37+
internal static float? unityRemoteGyroUpdateInterval;
38+
2139
/// <summary>
22-
/// Used by tests that run with a test runtime; the editor sets the handler on the native runtime
23-
/// at init, but the test runtime needs it installed explicitly.
40+
/// Used by tests that run with a test runtime; the test fixture installs
41+
/// <see cref="s_TestMessageDispatch"/> to deliver messages into <see cref="ProcessMessageFromUnityRemote"/>.
2442
/// </summary>
2543
internal static Func<IntPtr, bool> GetMessageHandlerForTesting() => ProcessMessageFromUnityRemote;
2644

2745
public static void Initialize()
2846
{
29-
InputRuntime.s_Instance.onUnityRemoteMessage = ProcessMessageFromUnityRemote;
47+
SetNativeMessageHandler(ProcessMessageFromUnityRemote);
3048

31-
InputSystem.onSettingsChange += () =>
49+
InputSystem.onSettingsChange += OnSettingsChanged;
50+
}
51+
52+
private static void OnSettingsChanged()
53+
{
54+
if (InputSystem.settings.IsFeatureEnabled(InputFeatureNames.kDisableUnityRemoteSupport))
3255
{
33-
if (InputSystem.settings.IsFeatureEnabled(InputFeatureNames.kDisableUnityRemoteSupport))
34-
{
35-
InputRuntime.s_Instance.onUnityRemoteMessage = null;
36-
if (s_State.connected)
37-
Disconnect();
38-
}
39-
else
40-
InputRuntime.s_Instance.onUnityRemoteMessage = ProcessMessageFromUnityRemote;
41-
};
56+
SetNativeMessageHandler(null);
57+
if (s_State.connected)
58+
Disconnect();
59+
}
60+
else
61+
SetNativeMessageHandler(ProcessMessageFromUnityRemote);
62+
}
63+
64+
private static MethodInfo GetUnityRemoteAPIMethod(string methodName)
65+
{
66+
var editorAssembly = typeof(EditorApplication).Assembly;
67+
var genericRemoteClass = editorAssembly.GetType("UnityEditor.Remote.GenericRemote");
68+
if (genericRemoteClass == null)
69+
return null;
70+
71+
return genericRemoteClass.GetMethod(methodName);
72+
}
73+
74+
private static void SetNativeMessageHandler(Func<IntPtr, bool> handler)
75+
{
76+
if (s_RegisteredNativeHandler != null)
77+
{
78+
var removeMethod = GetUnityRemoteAPIMethod("RemoveMessageHandler");
79+
removeMethod?.Invoke(null, new object[] { s_RegisteredNativeHandler });
80+
}
81+
82+
s_RegisteredNativeHandler = handler;
83+
84+
if (handler != null)
85+
{
86+
var addMethod = GetUnityRemoteAPIMethod("AddMessageHandler");
87+
addMethod?.Invoke(null, new object[] { handler });
88+
}
89+
}
90+
91+
private static void SetNativeGyroEnabled(bool value)
92+
{
93+
unityRemoteGyroEnabled = value;
94+
var setMethod = GetUnityRemoteAPIMethod("SetGyroEnabled");
95+
setMethod?.Invoke(null, new object[] { value });
96+
}
97+
98+
private static void SetNativeGyroUpdateInterval(float interval)
99+
{
100+
unityRemoteGyroUpdateInterval = interval;
101+
var setMethod = GetUnityRemoteAPIMethod("SetGyroUpdateInterval");
102+
setMethod?.Invoke(null, new object[] { interval });
42103
}
43104

44105
private static unsafe bool ProcessMessageFromUnityRemote(IntPtr messageData)
@@ -250,7 +311,7 @@ private static void OnDeviceChange(InputDevice device, InputDeviceChange change)
250311
if (command->type == SetSamplingFrequencyCommand.Type)
251312
{
252313
s_State.gyroUpdateInterval = ((SetSamplingFrequencyCommand*)command)->frequency;
253-
InputRuntime.s_Instance.SetUnityRemoteGyroUpdateInterval(s_State.gyroUpdateInterval);
314+
SetNativeGyroUpdateInterval(s_State.gyroUpdateInterval);
254315
return InputDeviceCommand.GenericSuccess;
255316
}
256317

@@ -270,7 +331,7 @@ private static void SyncGyroEnabledInRemote()
270331
if (enabled != s_State.gyroEnabled)
271332
{
272333
s_State.gyroEnabled = enabled;
273-
InputRuntime.s_Instance.SetUnityRemoteGyroEnabled(enabled);
334+
SetNativeGyroEnabled(enabled);
274335
}
275336
}
276337

@@ -503,6 +564,9 @@ internal static void ResetGlobalState()
503564
if (s_State.deviceCommandHandler != null)
504565
InputSystem.onDeviceCommand -= s_State.deviceCommandHandler;
505566
s_State = default;
567+
s_TestMessageDispatch = null;
568+
unityRemoteGyroEnabled = null;
569+
unityRemoteGyroUpdateInterval = null;
506570
}
507571
}
508572
}

Packages/com.unity.inputsystem/InputSystem/Runtime/IInputRuntime.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,6 @@ internal unsafe interface IInputRuntime
199199
bool isInPlayMode { get; }
200200
bool isEditorActive { get; }
201201
bool isEditorPaused { get; }
202-
203-
// Functionality related to the Unity Remote.
204-
Func<IntPtr, bool> onUnityRemoteMessage { set; }
205-
void SetUnityRemoteGyroEnabled(bool value);
206-
void SetUnityRemoteGyroUpdateInterval(float interval);
207202
#endif
208203
}
209204

Packages/com.unity.inputsystem/InputSystem/Runtime/NativeInputRuntime.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -354,35 +354,6 @@ public float scrollWheelDeltaPerTick
354354
public bool isEditorActive => m_IsEditorActive?.Invoke() ?? true;
355355
public bool isEditorPaused => m_IsEditorPaused?.Invoke() ?? false;
356356

357-
// Unity Remote callbacks - set by Editor
358-
internal Action<Func<IntPtr, bool>> m_SetUnityRemoteMessageHandler;
359-
internal Action<bool> m_SetUnityRemoteGyroEnabledCallback;
360-
internal Action<float> m_SetUnityRemoteGyroUpdateIntervalCallback;
361-
362-
private Func<IntPtr, bool> m_UnityRemoteMessageHandler;
363-
364-
public Func<IntPtr, bool> onUnityRemoteMessage
365-
{
366-
set
367-
{
368-
if (m_UnityRemoteMessageHandler == value)
369-
return;
370-
371-
m_UnityRemoteMessageHandler = value;
372-
m_SetUnityRemoteMessageHandler?.Invoke(value);
373-
}
374-
}
375-
376-
public void SetUnityRemoteGyroEnabled(bool value)
377-
{
378-
m_SetUnityRemoteGyroEnabledCallback?.Invoke(value);
379-
}
380-
381-
public void SetUnityRemoteGyroUpdateInterval(float interval)
382-
{
383-
m_SetUnityRemoteGyroUpdateIntervalCallback?.Invoke(interval);
384-
}
385-
386357
private Action<InputPlayModeChange> m_OnPlayModeChanged;
387358
private Action m_OnProjectChanged;
388359
/// <summary>

Packages/com.unity.inputsystem/Tests/TestFixture/InputTestRuntime.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -400,25 +400,6 @@ public double currentTimeOffsetToRealtimeSinceStartup
400400
public bool isInPlayMode { get; set; } = true;
401401
public bool isEditorActive { get; set; } = true;
402402
public bool isEditorPaused { get; set; }
403-
public Func<IntPtr, bool> onUnityRemoteMessage
404-
{
405-
get => m_UnityRemoteMessageHandler;
406-
set => m_UnityRemoteMessageHandler = value;
407-
}
408-
409-
public bool? unityRemoteGyroEnabled;
410-
public float? unityRemoteGyroUpdateInterval;
411-
412-
public void SetUnityRemoteGyroEnabled(bool value)
413-
{
414-
unityRemoteGyroEnabled = value;
415-
}
416-
417-
public void SetUnityRemoteGyroUpdateInterval(float interval)
418-
{
419-
unityRemoteGyroUpdateInterval = interval;
420-
}
421-
422403
public Action<InputPlayModeChange> onPlayModeChanged { get; set; }
423404
public Action onProjectChange { get; set; }
424405
#endif
@@ -438,7 +419,6 @@ public void SetUnityRemoteGyroUpdateInterval(float interval)
438419
private List<KeyValuePair<int, DeviceCommandCallback>> m_DeviceCommandCallbacks;
439420
private object m_Lock = new object();
440421
private double m_CurrentTimeOffsetToRealtimeSinceStartup;
441-
private Func<IntPtr, bool> m_UnityRemoteMessageHandler;
442422

443423
#if UNITY_ANALYTICS || UNITY_EDITOR
444424

0 commit comments

Comments
 (0)