门面---组合模式

千锋: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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值