Unity中C#模板方法模式Template Method Pattern整理

模板方法模式:
顾名思义,模板方法模式就是有一个“模板方法”,在执行的时候,就按照模板来(模板中一些具体方法有不同的具体操作)例如:去饭店吃饭:点单,吃饭,买单。 这3个流程相对固定,形成一个模板,具体的吃饭细节可以有不同的表现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class DM04TempleMethod:MonoBehaviour
{
    private void Start()
    {
        IPeople people = new NorthPeople();
        people.Eat();
        IPeople p2 = new SouthPeople();
        p2.Eat();
    }

}
public abstract class IPeople
{
    public void Eat()
    {
        OrderFoods();
        EatSomething();
        PayBill();
    }
    private void OrderFoods()
    {
        Debug.Log("点单");
    }
    public abstract void EatSomething();
    private void PayBill()
    {
        Debug.Log("买单");
    }
}

public class NorthPeople : IPeople
{
    public override void EatSomething()
    {
        Debug.Log("我在吃馒头");
    }
}
public class SouthPeople:IPeople
{
    public override void EatSomething()
    {
        Debug.Log("我吃米饭");
    }
}

下面是武器系统中模板方法模式的简单实际应用:入口在抽象类的 模板方法Fire中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public abstract class IWeapon
{
    protected int mAtk;
    protected float mAtkRange;
    protected int mAtkPlusValue;

    protected GameObject mGameObject;
    protected ICharacter mOwner;
    protected ParticleSystem mParticleSystem;
    protected LineRenderer mLine;
    protected Light mLight;
    protected AudioSource mAudio;

    protected float mEffectDisplayTime;
    public void Fire(Vector3 targetPosition)
    {
        PlayMuzzleEffect();
        PlayBulletEffect(targetPosition);
        SetEffectDisplayTime();
        PlaySound();
    }
    public void Update()
    {
        if (mEffectDisplayTime >=0)
        {
            mEffectDisplayTime -= Time.deltaTime;
        }else
        {
            DisableEffect();
        }
    }
    public void DisableEffect()
    {
        mLight.enabled = false;
        mLight.enabled = false;
    }
    public abstract void PlayMuzzleEffect();
    protected void DoPlayMuzzleEffect()
    {
        //显示枪口特效
        mParticleSystem.Stop();
        mParticleSystem.Play();
        mLight.enabled = true;
    }
    public abstract void PlayBulletEffect(Vector3 targetPosition);
    protected void DoPlayBulletEffect(float width, Vector3 targetPosition)
    {
        //显示子弹轨迹特效
        mLine.enabled = true;
        mLine.startWidth = 0.1f; mLine.endWidth = 0.1f;
        mLine.SetPosition(0, mGameObject.transform.position);
        mLine.SetPosition(1, targetPosition);
    }
    public abstract void PlaySound();
    protected void DoPlaySound(string name)
    {
        AudioClip clip = null;
        mAudio.clip = clip;
        mAudio.Play();
    }
    protected abstract void SetEffectDisplayTime();
    
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
public class WeaponGun:IWeapon
{
    public override void PlayBulletEffect(Vector3 targetPosition)
    {
        DoPlayBulletEffect(0.05f,targetPosition);
    }

    public override void PlayMuzzleEffect()
    {
        DoPlayMuzzleEffect();
    }

    public override void PlaySound()
    {
        DoPlaySound("GunShot");
    }

    protected override void SetEffectDisplayTime()
    {
        mEffectDisplayTime = 0.5f;
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class WeaponRocket : IWeapon
{
    public override void PlayBulletEffect(Vector3 targetPosition)
    {
        DoPlayBulletEffect(0.2f,targetPosition);
    }

    public override void PlayMuzzleEffect()
    {
        DoPlayMuzzleEffect();
    }

    public override void PlaySound()
    {
        DoPlaySound("RocketShot");
    }

    protected override void SetEffectDisplayTime()
    {
        mEffectDisplayTime = 0.5f;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值