38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
using System;
|
|
using UniRx;
|
|
using UnityEngine;
|
|
|
|
namespace SLSFramework.General
|
|
{
|
|
/// <summary>
|
|
/// 指令:从指令上下文中获取一个变量,并将其作为字符串在控制台输出。
|
|
/// </summary>
|
|
public class Cmd_GetAndLogVariable : CommandBase
|
|
{
|
|
private readonly string variableName;
|
|
|
|
public Cmd_GetAndLogVariable(string variableName)
|
|
{
|
|
this.variableName = variableName;
|
|
}
|
|
|
|
protected override IObservable<Unit> OnExecute(CommandContext outerContext)
|
|
{
|
|
// 尝试从 SharedData 中获取变量。
|
|
if (outerContext.context.TryGetValue(variableName, out object value))
|
|
{
|
|
// 获取成功,将其转换为字符串并输出。
|
|
string stringValue = value?.ToString() ?? "null";
|
|
Debug.Log($"[Cmd_GetAndLogVariable] 成功获取变量 '{variableName}',值为: '{stringValue}'");
|
|
}
|
|
else
|
|
{
|
|
// 获取失败,输出警告信息。
|
|
Debug.LogWarning($"[Cmd_GetAndLogVariable] 获取变量 '{variableName}' 失败,该变量未在上下文中定义。");
|
|
}
|
|
|
|
// 同样是瞬时操作。
|
|
return Observable.Return(Unit.Default);
|
|
}
|
|
}
|
|
} |