To quickly test our implementation in your own instance of Unity, you need to follow these steps:
- Copy all the scripts we just reviewed into your Unity project.
- Create a new scene.
- Add a GameObject to the scene.
- Attach the following ClientVisitor script to the new GameObject:
using UnityEngine;
namespace Pattern.Visitor
{
public class ClientVisitor : MonoBehaviour
{
public PowerUp enginePowerUp;
public PowerUp shieldPowerUp;
public PowerUp weaponPowerUp;
private BikeController _bikeController;
void Start()
{
_bikeController =
gameObject.
AddComponent<BikeController>();
}
void OnGUI()
{
if (GUILayout.Button("PowerUp Shield"))
_bikeController.Accept(shieldPowerUp);
if (GUILayout.Button("PowerUp Engine"))
_bikeController.Accept(enginePowerUp);
if...