重构
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace Architecture.Variables
|
||||
{
|
||||
/// <summary>
|
||||
/// 共享布尔变量(ScriptableObject 资产)。常用于暂停、开关等状态。
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "Architecture/Variables/Bool", fileName = "NewBoolVariable")]
|
||||
public class BoolVariable : ScriptableObject, Architecture.IVariable
|
||||
{
|
||||
[SerializeField] private bool _value;
|
||||
|
||||
[Tooltip("进入 Play Mode 时的初始值,由 VariableRegistry 在每次启动游戏时自动恢复,防止跨局状态泄漏")]
|
||||
[SerializeField] private bool _defaultValue;
|
||||
|
||||
public bool Value
|
||||
{
|
||||
get => _value;
|
||||
set
|
||||
{
|
||||
if (_value == value) return;
|
||||
_value = value;
|
||||
OnValueChanged?.Invoke(value);
|
||||
}
|
||||
}
|
||||
|
||||
public event Action<bool> OnValueChanged;
|
||||
|
||||
public void SetValue(bool value) => Value = value;
|
||||
|
||||
/// <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