I'm currently working on a Unity project and I decided to use command pattern to manage the buttons of the interface among other things. I'm not sure if I'm applying the patter the way I should because I'm not using an invoker for example.
This is an example of the code I have:
public interface iCommand
{
void Execute();
}
public class GenerateCommand : iCommand
{
private Generator _generator;
private Slider _sliderWidth, _sliderHeight;
public GenerateCommand(Generator generator, Slider sliderWidth, Slider sliderHeight)
{
this._generator = generator;
this._sliderWidth = sliderWidth;
this._sliderHeight = sliderHeight;
}
public void Execute()
{
_generator.Destroy();
_generator.SetWidth((int)_sliderWidth.value);
_generator.SetHeight((int)_sliderHeight.value);
_generator.Generate();
_generator.Build();
}
}
public class AppController : MonoBehaviour {
public Generator generator;
public Slider sliderWidth, sliderHeight;
public GameObject generatorController, playerController, UI, app, room, spawnLocation;
private iCommand _command;
void Start()
{
generator = new DepthFirstSearchGenerator(room: room, spawnLocation: spawnLocation);
Generate();
}
public void Generate()
{
_command = new GenerateCommand(generator: generator, sliderWidth: sliderWidth, sliderHeight: sliderHeight);
_command.Execute();
}
And then i call the Generate() function from the button when clicked.