|
15 | 15 |
|
16 | 16 | #if UNITY_EDITOR
|
17 | 17 | using UnityEngine.InputSystem.Editor;
|
| 18 | +using UnityEngine.Tilemaps; |
| 19 | + |
18 | 20 | #endif
|
19 | 21 |
|
20 | 22 | #if UNITY_EDITOR
|
@@ -54,13 +56,85 @@ namespace UnityEngine.InputSystem
|
54 | 56 | ///
|
55 | 57 | /// Manages devices, layouts, and event processing.
|
56 | 58 | /// </remarks>
|
57 |
| - internal partial class InputManager |
| 59 | + internal partial class InputManager : IDisposable |
58 | 60 | {
|
| 61 | + private InputManager() { } |
| 62 | + |
| 63 | + public static InputManager CreateAndInitialize(IInputRuntime runtime, InputSettings settings, bool fakeRemove = false) |
| 64 | + { |
| 65 | + var newInst = new InputManager(); |
| 66 | + |
| 67 | + // If settings object wasn't provided, create a temporary settings object for now |
| 68 | + if (settings == null) |
| 69 | + { |
| 70 | + settings = ScriptableObject.CreateInstance<InputSettings>(); |
| 71 | + settings.hideFlags = HideFlags.HideAndDontSave; |
| 72 | + } |
| 73 | + newInst.m_Settings = settings; |
| 74 | + |
| 75 | +#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS |
| 76 | + newInst.InitializeActions(); |
| 77 | +#endif // UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS |
| 78 | + |
| 79 | + newInst.InitializeData(); |
| 80 | + newInst.InstallRuntime(runtime); |
| 81 | + |
| 82 | + // Skip if initializing for "Fake Remove" manager (in tests) |
| 83 | + if (!fakeRemove) |
| 84 | + newInst.InstallGlobals(); |
| 85 | + |
| 86 | + newInst.ApplySettings(); |
| 87 | + |
| 88 | +#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS |
| 89 | + newInst.ApplyActions(); |
| 90 | +#endif |
| 91 | + return newInst; |
| 92 | + } |
| 93 | + |
| 94 | +#region Dispose implementation |
| 95 | + protected virtual void Dispose(bool disposing) |
| 96 | + { |
| 97 | + if (!disposedValue) |
| 98 | + { |
| 99 | + if (disposing) |
| 100 | + { |
| 101 | + // Notify devices are being removed but don't actually removed them; no point when disposing |
| 102 | + for (var i = 0; i < m_DevicesCount; ++i) |
| 103 | + m_Devices[i].NotifyRemoved(); |
| 104 | + |
| 105 | + m_StateBuffers.FreeAll(); |
| 106 | + UninstallGlobals(); |
| 107 | + |
| 108 | + // If we're still holding the "temporary" settings object make sure to delete it |
| 109 | + if (m_Settings != null && m_Settings.hideFlags == HideFlags.HideAndDontSave) |
| 110 | + Object.DestroyImmediate(m_Settings); |
| 111 | + |
| 112 | + // Project-wide Actions are never temporary so we do not destroy them. |
| 113 | + } |
| 114 | + |
| 115 | + disposedValue = true; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + ~InputManager() |
| 120 | + { |
| 121 | + Dispose(false); |
| 122 | + } |
| 123 | + |
| 124 | + public void Dispose() |
| 125 | + { |
| 126 | + Dispose(true); |
| 127 | + GC.SuppressFinalize(this); |
| 128 | + } |
| 129 | + private bool disposedValue; |
| 130 | +#endregion |
| 131 | + |
59 | 132 | public ReadOnlyArray<InputDevice> devices => new ReadOnlyArray<InputDevice>(m_Devices, 0, m_DevicesCount);
|
60 | 133 |
|
61 | 134 | public TypeTable processors => m_Processors;
|
62 | 135 | public TypeTable interactions => m_Interactions;
|
63 | 136 | public TypeTable composites => m_Composites;
|
| 137 | + internal IInputRuntime runtime => m_Runtime; |
64 | 138 |
|
65 | 139 | public InputMetrics metrics
|
66 | 140 | {
|
@@ -90,14 +164,17 @@ public InputSettings settings
|
90 | 164 | {
|
91 | 165 | get
|
92 | 166 | {
|
93 |
| - Debug.Assert(m_Settings != null); |
94 | 167 | return m_Settings;
|
95 | 168 | }
|
96 | 169 | set
|
97 | 170 | {
|
98 | 171 | if (value == null)
|
99 | 172 | throw new ArgumentNullException(nameof(value));
|
100 | 173 |
|
| 174 | + // Delete the "temporary" settings if necessary |
| 175 | + if (m_Settings != null && m_Settings.hideFlags == HideFlags.HideAndDontSave) |
| 176 | + ScriptableObject.DestroyImmediate(m_Settings); |
| 177 | + |
101 | 178 | if (m_Settings == value)
|
102 | 179 | return;
|
103 | 180 |
|
@@ -1768,45 +1845,6 @@ public void Update(InputUpdateType updateType)
|
1768 | 1845 | m_Runtime.Update(updateType);
|
1769 | 1846 | }
|
1770 | 1847 |
|
1771 |
| - internal void Initialize(IInputRuntime runtime, InputSettings settings) |
1772 |
| - { |
1773 |
| - Debug.Assert(settings != null); |
1774 |
| - |
1775 |
| - m_Settings = settings; |
1776 |
| - |
1777 |
| -#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS |
1778 |
| - InitializeActions(); |
1779 |
| -#endif // UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS |
1780 |
| - InitializeData(); |
1781 |
| - InstallRuntime(runtime); |
1782 |
| - InstallGlobals(); |
1783 |
| - |
1784 |
| - ApplySettings(); |
1785 |
| - #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS |
1786 |
| - ApplyActions(); |
1787 |
| - #endif |
1788 |
| - } |
1789 |
| - |
1790 |
| - internal void Destroy() |
1791 |
| - { |
1792 |
| - // There isn't really much of a point in removing devices but we still |
1793 |
| - // want to clear out any global state they may be keeping. So just tell |
1794 |
| - // the devices that they got removed without actually removing them. |
1795 |
| - for (var i = 0; i < m_DevicesCount; ++i) |
1796 |
| - m_Devices[i].NotifyRemoved(); |
1797 |
| - |
1798 |
| - // Free all state memory. |
1799 |
| - m_StateBuffers.FreeAll(); |
1800 |
| - |
1801 |
| - // Uninstall globals. |
1802 |
| - UninstallGlobals(); |
1803 |
| - |
1804 |
| - // Destroy settings if they are temporary. |
1805 |
| - if (m_Settings != null && m_Settings.hideFlags == HideFlags.HideAndDontSave) |
1806 |
| - Object.DestroyImmediate(m_Settings); |
1807 |
| - |
1808 |
| - // Project-wide Actions are never temporary so we do not destroy them. |
1809 |
| - } |
1810 | 1848 |
|
1811 | 1849 | #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
|
1812 | 1850 | // Initialize project-wide actions:
|
@@ -2112,12 +2150,12 @@ internal struct AvailableDevice
|
2112 | 2150 | private bool m_HaveSentStartupAnalytics;
|
2113 | 2151 | #endif
|
2114 | 2152 |
|
2115 |
| - internal IInputRuntime m_Runtime; |
2116 |
| - internal InputMetrics m_Metrics; |
2117 |
| - internal InputSettings m_Settings; |
2118 |
| - #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS |
| 2153 | + private IInputRuntime m_Runtime; |
| 2154 | + private InputMetrics m_Metrics; |
| 2155 | + private InputSettings m_Settings; |
| 2156 | +#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS |
2119 | 2157 | private InputActionAsset m_Actions;
|
2120 |
| - #endif |
| 2158 | +#endif |
2121 | 2159 |
|
2122 | 2160 | #if UNITY_EDITOR
|
2123 | 2161 | internal IInputDiagnostics m_Diagnostics;
|
@@ -2563,6 +2601,8 @@ private void OnBeforeUpdate(InputUpdateType updateType)
|
2563 | 2601 | /// </summary>
|
2564 | 2602 | internal void ApplySettings()
|
2565 | 2603 | {
|
| 2604 | + Debug.Assert(m_Settings != null); |
| 2605 | + |
2566 | 2606 | // Sync update mask.
|
2567 | 2607 | var newUpdateMask = InputUpdateType.Editor;
|
2568 | 2608 | if ((m_UpdateMask & InputUpdateType.BeforeRender) != 0)
|
@@ -3865,8 +3905,16 @@ internal void RestoreStateWithoutDevices(SerializedState state)
|
3865 | 3905 | m_Metrics = state.metrics;
|
3866 | 3906 | m_PollingFrequency = state.pollingFrequency;
|
3867 | 3907 |
|
3868 |
| - if (m_Settings != null) |
| 3908 | + // Cached settings might be null if the ScriptableObject was destroyed; create new default instance in this case. |
| 3909 | + if (state.settings == null) |
| 3910 | + { |
| 3911 | + state.settings = ScriptableObject.CreateInstance<InputSettings>(); |
| 3912 | + state.settings.hideFlags = HideFlags.HideAndDontSave; |
| 3913 | + } |
| 3914 | + |
| 3915 | + if (m_Settings != null && m_Settings != state.settings) |
3869 | 3916 | Object.DestroyImmediate(m_Settings);
|
| 3917 | + |
3870 | 3918 | m_Settings = state.settings;
|
3871 | 3919 |
|
3872 | 3920 | #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
|
@@ -4024,7 +4072,6 @@ private bool RestoreDeviceFromSavedState(ref DeviceState deviceState, InternedSt
|
4024 | 4072 |
|
4025 | 4073 | return true;
|
4026 | 4074 | }
|
4027 |
| - |
4028 | 4075 | #endif // UNITY_EDITOR || DEVELOPMENT_BUILD
|
4029 | 4076 | }
|
4030 | 4077 | }
|
0 commit comments