门面模式:将各种不同类型的东西,放在一起聚合形成特定的功能
例:用按钮控制红绿灯交替亮起
using UnityEngine.UI;
public class TestShop : MonoBehaviour
{
public GameObject redObj;
public GameObject greenObj;
public void ShowRed()
{
redObj.SetActive(true);
greenObj.SetActive(false);
}
public void ShowGreen()
{
redObj.SetActive(false);
greenObj.SetActive(true);
}
bool isReding = true;
public void OnClick()
{
isReding = !isReding;
if (isReding)
{
ShowRed();
}
else
{
ShowGreen();
}
}
// Start is called before the first frame update
void Start()
{
Button btn = GetComponent<Button>();
btn.onClick.AddListener(OnClick);
}
// Update is called once per frame
void Update()
{
}
}
组合模式:将相同类型的东西组合在一起形成特有的功能