重构
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace Architecture.Variables
|
||||
{
|
||||
/// <summary>
|
||||
/// 共享整数变量(ScriptableObject 资产)。常用于血量、得分、魂灵数等。
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "Architecture/Variables/Int", fileName = "NewIntVariable")]
|
||||
public class IntVariable : ScriptableObject, Architecture.IVariable
|
||||
{
|
||||
[SerializeField] private int _value;
|
||||
|
||||
[Tooltip("进入 Play Mode 时的初始值,由 VariableRegistry 在每次启动游戏时自动恢复,防止跨局状态泄漏")]
|
||||
[SerializeField] private int _defaultValue;
|
||||
|
||||
public int Value
|
||||
{
|
||||
get => _value;
|
||||
set
|
||||
{
|
||||
if (_value == value) return;
|
||||
_value = value;
|
||||
OnValueChanged?.Invoke(value);
|
||||
}
|
||||
}
|
||||
|
||||
public event Action<int> OnValueChanged;
|
||||
|
||||
public void SetValue(int value) => Value = value;
|
||||
public void ApplyChange(int amount) => Value += amount;
|
||||
|
||||
/// <summary>将运行时值重置为 _defaultValue(每次进入 Play Mode 时由 VariableRegistry 自动调用)。</summary>
|
||||
public void ResetToDefault()
|
||||
{
|
||||
_value = _defaultValue;
|
||||
OnValueChanged?.Invoke(_value);
|
||||
}
|
||||
|
||||
[ContextMenu("Reset To Default")]
|
||||
public void Reset() => ResetToDefault();
|
||||
|
||||
private void OnEnable() => VariableRegistry.Register(this);
|
||||
private void OnDisable()
|
||||
{
|
||||
VariableRegistry.Unregister(this);
|
||||
OnValueChanged = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user