清理各个 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;
}
@@ -16,7 +16,7 @@ namespace SepCore.InputModule.Tests
public void SetUp()
{
_gameObject = new GameObject("InputModuleTest");
_component = _gameObject.AddComponent<InputModuleComponent>();
_component = InputModuleTestFactory.CreateComponent(_gameObject);
}
[TearDown]
@@ -17,7 +17,7 @@ namespace SepCore.InputModule.Tests
public void SetUp()
{
_gameObject = new GameObject("InputModuleEventTest");
_component = _gameObject.AddComponent<InputModuleComponent>();
_component = InputModuleTestFactory.CreateComponent(_gameObject);
_component.OnInit();
}
@@ -185,6 +185,9 @@ namespace SepCore.InputModule.Tests
public void RegisterListener_NullCallback_DoesNotThrow()
{
Assert.DoesNotThrow(() => _component.RegisterListener(InputActionId.Pause, null));
Dictionary<InputActionId, Action<InputCommand>> listeners = GetPrivateListenersDictionary();
Assert.That(listeners.ContainsKey(InputActionId.Pause), Is.False);
}
[Test]
@@ -17,7 +17,7 @@ namespace SepCore.InputModule.Tests
{
base.Setup();
_gameObject = new GameObject("InputModuleRebindTest");
_component = _gameObject.AddComponent<InputModuleComponent>();
_component = InputModuleTestFactory.CreateComponent(_gameObject);
_component.OnInit();
}
@@ -202,7 +202,8 @@ namespace SepCore.InputModule.Tests
InputAction confirmAction = _component.RuntimeActions.FindAction($"{nameof(InputContextId.UI)}/{nameof(InputActionId.Confirm)}");
confirmAction.ApplyBindingOverride(0, "<Keyboard>/space");
InputBindingSnapshot snapshot = _component.SaveBindingOverrides();
string overridesJson = _component.RuntimeActions.SaveBindingOverridesAsJson();
InputBindingSnapshot snapshot = new InputBindingSnapshot(overridesJson);
_component.ResetAllBindingsToDefaults();
Assert.That(confirmAction.bindings[0].overridePath, Is.Null);
@@ -17,7 +17,7 @@ namespace SepCore.InputModule.Tests
public void SetUp()
{
_gameObject = new GameObject("InputModuleDebuggerReflectionTest");
_component = _gameObject.AddComponent<InputModuleComponent>();
_component = InputModuleTestFactory.CreateComponent(_gameObject);
}
[TearDown]
@@ -0,0 +1,18 @@
using System.Reflection;
using SepCore.InputModule.Runtime;
using UnityEngine;
namespace SepCore.InputModule.Tests
{
internal static class InputModuleTestFactory
{
public static InputModuleComponent CreateComponent(GameObject gameObject)
{
InputModuleComponent component = gameObject.AddComponent<InputModuleComponent>();
typeof(InputModuleComponent)
.GetField("_loadBindingOverridesOnInit", BindingFlags.Instance | BindingFlags.NonPublic)
.SetValue(component, false);
return component;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1e4066e709bd03646a983938f9b9b525
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -57,6 +57,7 @@ namespace SepCore.Timer
if (timerTask.RepeatCount == 0)
{
Log.Error("Timer task repeat count is zero.");
return TimerHandle.Invalid;
}
@@ -139,6 +140,7 @@ namespace SepCore.Timer
{
if (owner == null)
{
Log.Error("Timer task owner is invalid.");
return 0;
}
@@ -248,6 +250,12 @@ namespace SepCore.Timer
}
float safeInterval = Mathf.Max(0f, newInterval);
if (taskInfo.IsRepeating && safeInterval <= 0f)
{
Log.Error("Timer task interval must be greater than zero for repeated tasks.");
return false;
}
if (adjustRemainingTime && taskInfo.Interval > 0f && safeInterval > 0f)
{
float ratio = safeInterval / taskInfo.Interval;
@@ -443,11 +451,6 @@ namespace SepCore.Timer
_isUpdating = true;
for (int i = taskCount - 1; i >= 0; i--)
{
if (i >= _timerTasks.Count)
{
continue;
}
TimerTaskInfo taskInfo = _timerTasks[i];
if (taskInfo.IsCancelled || taskInfo.IsPaused)
{
@@ -539,6 +542,7 @@ namespace SepCore.Timer
RemainingTime = delay;
Interval = interval;
RemainingRepeatCount = repeatCount;
IsRepeating = repeatCount != 1;
TimeMode = timeMode;
Owner = owner;
Callback = callback;
@@ -569,6 +573,11 @@ namespace SepCore.Timer
set;
}
public bool IsRepeating
{
get;
}
public TimerTimeMode TimeMode
{
get;