@@ -80,9 +80,19 @@ namespace UnityEngine.InputSystem
80
80
#if UNITY_EDITOR
81
81
[ InitializeOnLoad ]
82
82
#endif
83
-
84
83
public static partial class InputSystem
85
84
{
85
+ static InputSystem ( )
86
+ {
87
+ GlobalInitialize ( true ) ;
88
+ }
89
+
90
+ [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
91
+ private static void RuntimeInitialize ( )
92
+ {
93
+ GlobalInitialize ( false ) ;
94
+ }
95
+
86
96
#region Layouts
87
97
88
98
/// <summary>
@@ -3102,7 +3112,7 @@ public static InputActionAsset actions
3102
3112
return ;
3103
3113
3104
3114
var valueIsNotNull = value != null ;
3105
- #if UNITY_EDITOR
3115
+ #if UNITY_EDITOR
3106
3116
// Do not allow assigning non-persistent assets (pure in-memory objects)
3107
3117
if ( valueIsNotNull && ! EditorUtility . IsPersistent ( value ) )
3108
3118
throw new ArgumentException ( $ "Assigning a non-persistent { nameof ( InputActionAsset ) } to this property is not allowed. The assigned asset need to be persisted on disc inside the /Assets folder.") ;
@@ -3117,7 +3127,7 @@ public static InputActionAsset actions
3117
3127
3118
3128
// Note that we do not enable/disable any actions until play-mode
3119
3129
}
3120
- }
3130
+ }
3121
3131
3122
3132
/// <summary>
3123
3133
/// Event that is triggered if the instance assigned to property <see cref="actions"/> changes.
@@ -3129,7 +3139,7 @@ public static InputActionAsset actions
3129
3139
/// <seealso cref="actions"/>
3130
3140
/// <seealso cref="InputActionAsset"/>
3131
3141
public static event Action onActionsChange
3132
- {
3142
+ {
3133
3143
add => s_Manager . onActionsChange += value ;
3134
3144
remove => s_Manager . onActionsChange -= value ;
3135
3145
}
@@ -3487,30 +3497,42 @@ private static bool ShouldEnableRemoting()
3487
3497
3488
3498
// The rest here is internal stuff to manage singletons, survive domain reloads,
3489
3499
// and to support the reset ability for tests.
3490
- static InputSystem ( )
3500
+
3501
+ private static bool IsDomainReloadDisabledForPlayMode ( )
3491
3502
{
3492
- #if UNITY_EDITOR
3493
- InitializeInEditor ( ) ;
3494
- #else
3495
- InitializeInPlayer ( ) ;
3503
+ #if UNITY_EDITOR && ! ENABLE_CORECLR
3504
+ if ( ! EditorSettings . enterPlayModeOptionsEnabled || ( EditorSettings . enterPlayModeOptions & EnterPlayModeOptions . DisableDomainReload ) == 0 )
3505
+ return false ;
3496
3506
#endif
3507
+ return true ;
3497
3508
}
3498
3509
3499
- ////FIXME: Unity is not calling this method if it's inside an #if block that is not
3500
- //// visible to the editor; that shouldn't be the case
3501
- [ RuntimeInitializeOnLoadMethod ( loadType : RuntimeInitializeLoadType . SubsystemRegistration ) ]
3502
- private static void RunInitializeInPlayer ( )
3510
+ private static void GlobalInitialize ( bool calledFromCtor )
3511
+ {
3512
+ // This method is called twice: once from the static ctor and again from RuntimeInitialize().
3513
+ // We handle the calls differently for the Editor and Player.
3514
+
3515
+ #if UNITY_EDITOR
3516
+ // If Domain Reloads are enabled, InputSystem is initialized via the ctor and we can ignore
3517
+ // the second call from "Runtime", otherwise (DRs are disabled) the ctor isn't fired, so we
3518
+ // must initialize via the Runtime call.
3519
+
3520
+ if ( calledFromCtor || IsDomainReloadDisabledForPlayMode ( ) )
3503
3521
{
3504
- // We're using this method just to make sure the class constructor is called
3505
- // so we don't need any code in here. When the engine calls this method, the
3506
- // class constructor will be run if it hasn't been run already.
3522
+ InitializeInEditor ( calledFromCtor ) ;
3523
+ }
3524
+ #else
3525
+ // In the Player, simply initialize InputSystem from the ctor and then execute the initial update
3526
+ // from the second call. This saves us from needing another RuntimeInitializeOnLoad attribute.
3507
3527
3508
- // IL2CPP has a bug that causes the class constructor to not be run when
3509
- // the RuntimeInitializeOnLoadMethod is invoked. So we need an explicit check
3510
- // here until that is fixed (case 1014293).
3511
- #if ! UNITY_EDITOR
3512
- if ( s_Manager == null )
3528
+ if ( calledFromCtor )
3529
+ {
3513
3530
InitializeInPlayer ( ) ;
3531
+ }
3532
+ else
3533
+ {
3534
+ RunInitialUpdate ( ) ;
3535
+ }
3514
3536
#endif
3515
3537
}
3516
3538
@@ -3524,18 +3546,24 @@ internal static void EnsureInitialized()
3524
3546
#if UNITY_EDITOR
3525
3547
internal static InputSystemObject s_SystemObject ;
3526
3548
3527
- internal static void InitializeInEditor ( IInputRuntime runtime = null )
3549
+ internal static void InitializeInEditor ( bool calledFromCtor , IInputRuntime runtime = null )
3528
3550
{
3529
3551
Profiler . BeginSample ( "InputSystem.InitializeInEditor" ) ;
3530
3552
3553
+ // This is only necessary after a Domain Reload but otherwise can be skipped.
3554
+ bool globalReset = calledFromCtor || ! IsDomainReloadDisabledForPlayMode ( ) ;
3555
+
3556
+ if ( globalReset )
3531
3557
Reset ( runtime : runtime ) ;
3532
3558
3533
3559
var existingSystemObjects = Resources . FindObjectsOfTypeAll < InputSystemObject > ( ) ;
3534
3560
if ( existingSystemObjects != null && existingSystemObjects . Length > 0 )
3535
3561
{
3562
+ if ( globalReset )
3563
+ {
3536
3564
////FIXME: does not preserve action map state
3537
3565
3538
- // We 're coming back out of a domain reload. We're restoring part of the
3566
+ // If we 're coming back out of a domain reload. We're restoring part of the
3539
3567
// InputManager state here but we're still waiting from layout registrations
3540
3568
// that happen during domain initialization.
3541
3569
@@ -3559,6 +3587,7 @@ internal static void InitializeInEditor(IInputRuntime runtime = null)
3559
3587
// Get rid of saved state.
3560
3588
s_SystemObject . systemState = new State ( ) ;
3561
3589
}
3590
+ }
3562
3591
else
3563
3592
{
3564
3593
s_SystemObject = ScriptableObject . CreateInstance < InputSystemObject > ( ) ;
@@ -3761,7 +3790,6 @@ private static void InitializeInPlayer(IInputRuntime runtime = null, InputSettin
3761
3790
3762
3791
#endif // UNITY_EDITOR
3763
3792
3764
- [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
3765
3793
private static void RunInitialUpdate ( )
3766
3794
{
3767
3795
// Request an initial Update so that user methods such as Start and Awake
@@ -3898,9 +3926,9 @@ private static void Reset(bool enableRemoting = false, IInputRuntime runtime = n
3898
3926
3899
3927
// This is the point where we initialise project-wide actions for the Editor, Editor Tests and Player Tests.
3900
3928
// Note this is too early for editor ! actions is not setup yet.
3901
- #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
3929
+ #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
3902
3930
EnableActions ( ) ;
3903
- #endif
3931
+ #endif
3904
3932
3905
3933
Profiler . EndSample ( ) ;
3906
3934
}
0 commit comments