千锋:Unity RPG游戏从入门到实战全套教程_哔哩哔哩_bilibili
门面模式:
将各种不同类型的东西放在一起聚合形成特定的功能。
场景中物体:看成一片女生
组件:看成女生特定功能
材质球:看成衣服
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class TestShop : MonoBehaviour {
public GameObject redObj;
public GameObject greenObj;public void ShowRedLighting() {
redObj.SetActive(true);
greenObj.SetActive(false);
}
public void ShowGreenLighting() {
redObj.SetActive(false);
greenObj.SetActive(true);
}bool isReady = true;
public void OnClick() {
isReady = !isReady;
if (isReady) {
ShowRedLighting();
}
else {
ShowGreenLighting();
}
}
// Use this for initialization
void Start () {
Button btn = GetComponent<Button>();btn.onClick.AddListener(OnClick);
}
// Update is called once per frame
void Update () {
}
}
组合模式
将相同类型的东西聚合在一起形成特有的功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TankBase {
public virtual void WalkFront() {}
public virtual void WalkBack() {
}
}public class Tower : TankBase {
public override void WalkFront()
{
//base.WalkFront();
}public override void WalkBack()
{
//base.WalkBack();
}
}
public class TankWheel : TankBase {
//roate
public override void WalkFront()
{
//base.WalkFront();
}public override void WalkBack()
{
//base.WalkBack();
}}
public class TankEngin : TankBase {
//roate 播放声音
public override void WalkFront()
{
//base.WalkFront();
}public override void WalkBack()
{
//base.WalkBack();
}
}public class TestCombine : MonoBehaviour {
List<TankBase> tank;
// Use this for initialization
void Start () {
tank = new List<TankBase>();
Tower tower = new Tower();tank.Add(tower);
TankEngin egine = new TankEngin();
tank.Add(egine);TankWheel wheel = new TankWheel();
tank.Add(wheel);}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.W)) {
for (int i = 0; i < tank.Count; i++) {
tank[i].WalkFront();
}
}if (Input.GetKey(KeyCode.S))
{
for (int i = 0; i < tank.Count; i++)
{
tank[i].WalkBack();
}
}
}
}
状态者模式:
FSM:有限状态机
animator.setInteger()