Files
Continentis/Assets/Scripts/ScriptExtensions/CommandQueue/Examples/Cmd_SetVariable.cs
SoulliesOfficial 61a397dd4c MOD!
2025-10-23 00:49:44 -04:00

33 lines
1.1 KiB
C#

using System;
using UniRx;
using UnityEngine;
namespace SLSFramework.General
{
/// <summary>
/// 指令:在指令上下文 (CommandContext) 中声明并设置一个变量。
/// </summary>
public class Cmd_SetVariable : CommandBase
{
private readonly string variableName;
private readonly object value;
public Cmd_SetVariable(string variableName, object value)
{
this.variableName = variableName;
this.value = value;
}
protected override IObservable<Unit> OnExecute(CommandContext outerContext)
{
Debug.Log($"[Cmd_SetVariable] 正在设置变量 '{variableName}',值为: '{value}'");
// 直接操作 Context 的 SharedData 字典来存入数据。
// 使用索引器会覆盖同名旧值,如果需要更复杂的逻辑可以先用 TryGetValue 检查。
outerContext.context[variableName] = value;
// 这是一个瞬时操作,所以返回一个立即完成的 Observable。
return Observable.Return(Unit.Default);
}
}
}