【发布时间】:2023-03-14 19:49:01
【问题描述】:
首先抱歉,如果我会使用混乱的术语,我仍在学习很多关于命令模式和 C# 的知识。
我正在尝试使用 C# 在 Unity3D 中实现命令模式,特别是 this implementation 适合我的情况。
鉴于Command.cs 和GameController.cs 脚本,我创建了一个DoThing 类,继承自Command 类,使用以下代码实现:
public class DoThing : Command
{
public string name;
public int healthPoints;
public DoThing(string name, int healthPoints)
{
this.name = name;
this.healthPoints = healthPoints;
}
}
现在,由于我通过构造函数(name、healthPoints)将一些参数传递给命令,我想从另一个脚本中提取这些参数。
我尝试(成功)将参数传递给以下行中的命令并将命令保存在堆栈中:
var doCommand = new DoThing("asdf", 123);
Stack<Command> listOfCommands = new Stack<Command>();
listOfCommands.Push(doCommand);
我尝试(成功)在代码执行期间在监视窗口中检索这些参数:
listOfCommands.Peek().name //returns "asdf"
但是这在脚本中不起作用,这意味着看不到参数:
Debug.Log(listOfCommands.Peek().name) //throws error
有没有办法提取参数?
【问题讨论】:
标签: c# unity3d command command-pattern