Files
Continentis/Assets/Scripts/ScriptExtensions/CommandQueue/Examples/Cmd_SetVariable.cs
SoulliesOfficial 9b1b5ca93f initial
2025-10-03 00:02:43 -04:00

33 lines
1.1 KiB
C#

using System;
using UniRx;
using UnityEngine;
namespace SoulliesFramework.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 context)
{
Debug.Log($"[Cmd_SetVariable] 正在设置变量 '{variableName}',值为: '{value}'");
// 直接操作 Context 的 SharedData 字典来存入数据。
// 使用索引器会覆盖同名旧值,如果需要更复杂的逻辑可以先用 TryGetValue 检查。
context.sharedInfo[variableName] = value;
// 这是一个瞬时操作,所以返回一个立即完成的 Observable。
return Observable.Return(Unit.Default);
}
}
}