Files
Continentis/Assets/Scripts/ScriptExtensions/CommandQueue/Examples/Cmd_SetVariable.cs

33 lines
1.1 KiB
C#
Raw Normal View History

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