清理各个 Component 里的判断以及调用规范

This commit is contained in:
2026-09-14 18:35:02 +08:00
parent 5fbeec2476
commit 17ee268416
30 changed files with 769 additions and 1326 deletions
@@ -30,7 +30,6 @@ namespace SepCore.InputModule.Runtime
private SettingComponent _settingComponent = null;
private bool _isInitialized = false;
private bool _onInitCalled = false;
private bool _ownsRuntimeActions = false;
private InputActionRebindingExtensions.RebindingOperation _activeRebindOperation;
private bool _rebindCanceledByUser;
private IInputPromptMap _promptMap;
@@ -75,11 +74,6 @@ namespace SepCore.InputModule.Runtime
}
}
protected override void Awake()
{
base.Awake();
}
private void OnEnable()
{
if (_isInitialized)
@@ -112,29 +106,21 @@ namespace SepCore.InputModule.Runtime
UnsubscribeFromGlobalDeviceTracking();
UnsubscribeFromActions();
if (_ownsRuntimeActions && _runtimeActions != null)
if (Application.isPlaying)
{
if (Application.isPlaying)
{
Destroy(_runtimeActions);
}
else
{
DestroyImmediate(_runtimeActions);
}
Destroy(_runtimeActions);
}
else
{
DestroyImmediate(_runtimeActions);
}
_runtimeActions = null;
_actionMaps.Clear();
_listeners.Clear();
_isInitialized = false;
_onInitCalled = false;
}
public void RegisterListener(InputActionId actionId, Action<InputCommand> listener)
{
if (listener == null)
{
Log.Error("InputModuleComponent.RegisterListener() listener is invalid.");
return;
}
@@ -160,7 +146,7 @@ namespace SepCore.InputModule.Runtime
if (_listeners.TryGetValue(command.ActionId, out Action<InputCommand> listener))
{
listener?.Invoke(command);
listener.Invoke(command);
}
}
@@ -343,11 +329,14 @@ namespace SepCore.InputModule.Runtime
EnsureInitialized();
if (!TryGetAction(contextId, actionId, out InputAction action))
{
Log.Error("InputModuleComponent.ResetBindingToDefault() action '{0}' is not found in context '{1}'.",
actionId, contextId);
return;
}
if (bindingIndex < 0 || bindingIndex >= action.bindings.Count)
{
Log.Error("InputModuleComponent.ResetBindingToDefault() binding index '{0}' is invalid.", bindingIndex);
return;
}
@@ -364,6 +353,8 @@ namespace SepCore.InputModule.Runtime
EnsureInitialized();
if (!TryGetAction(contextId, actionId, out InputAction action))
{
Log.Error("InputModuleComponent.ResetActionToDefaults() action '{0}' is not found in context '{1}'.",
actionId, contextId);
return;
}
@@ -392,11 +383,7 @@ namespace SepCore.InputModule.Runtime
string overridesJson = _runtimeActions.SaveBindingOverridesAsJson();
string storedData = BindingOverridePersistence.Serialize(overridesJson);
SettingComponent settingComponent = GetSettingComponent();
if (settingComponent != null)
{
settingComponent.SetString(_bindingOverrideSettingKey, storedData);
}
GetSettingComponent().SetString(_bindingOverrideSettingKey, storedData);
return new InputBindingSnapshot(overridesJson);
}
@@ -409,13 +396,8 @@ namespace SepCore.InputModule.Runtime
private void LoadBindingOverridesCore()
{
if (_runtimeActions == null)
{
return;
}
SettingComponent settingComponent = GetSettingComponent();
if (settingComponent == null || !settingComponent.HasSetting(_bindingOverrideSettingKey))
if (!settingComponent.HasSetting(_bindingOverrideSettingKey))
{
return;
}
@@ -423,6 +405,7 @@ namespace SepCore.InputModule.Runtime
string storedData = settingComponent.GetString(_bindingOverrideSettingKey);
if (!BindingOverridePersistence.TryDeserialize(storedData, out string overridesJson))
{
Log.Error("InputModuleComponent failed to deserialize stored binding overrides.");
return;
}
@@ -455,7 +438,6 @@ namespace SepCore.InputModule.Runtime
_runtimeActions = _inputActionsAsset != null
? Instantiate(_inputActionsAsset)
: InputModuleDefaultActions.Create();
_ownsRuntimeActions = true;
CacheActionMaps();
SubscribeToActions();
@@ -470,7 +452,7 @@ namespace SepCore.InputModule.Runtime
int mapCount = _actionMaps.Count;
if (mapCount == 0)
{
Log.Warning("[InputModule] Initialized but no action maps were cached. Check that action map names match InputContextId enum values.");
Log.Error("[InputModule] Initialized but no action maps were cached. Check that action map names match InputContextId enum values.");
}
else
{
@@ -504,7 +486,7 @@ namespace SepCore.InputModule.Runtime
}
else
{
Log.Warning("[InputModule] ActionMap '{0}' does not match any InputContextId and will be ignored.", map.name);
Log.Error("[InputModule] ActionMap '{0}' does not match any InputContextId and will be ignored.", map.name);
}
}
}
@@ -521,11 +503,6 @@ namespace SepCore.InputModule.Runtime
private void UnsubscribeFromActions()
{
if (_runtimeActions == null)
{
return;
}
foreach (InputAction action in _runtimeActions)
{
action.started -= OnActionTriggered;
@@ -559,11 +536,6 @@ namespace SepCore.InputModule.Runtime
// context is active at any time, preventing overlapping bindings from stacked maps.
private void ApplyContextState()
{
if (_runtimeActions == null)
{
return;
}
_runtimeActions.Disable();
if (_actionMaps.TryGetValue(InputContextId.Global, out InputActionMap globalMap))
@@ -600,7 +572,7 @@ namespace SepCore.InputModule.Runtime
if (_listeners.TryGetValue(actionId, out Action<InputCommand> listener))
{
listener?.Invoke(command);
listener.Invoke(command);
}
}
@@ -725,7 +697,7 @@ namespace SepCore.InputModule.Runtime
continue;
}
if (string.Equals(binding.effectivePath, newPath, System.StringComparison.OrdinalIgnoreCase))
if (string.Equals(binding.effectivePath, newPath, StringComparison.OrdinalIgnoreCase))
{
return true;
}