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