0
点赞
收藏
分享

微信扫一扫

【设计模式】Unity3D 状态模式

想溜了的蜗牛 2022-04-13 阅读 40

Unity3D 状态模式

提示:个人学习总结,如有错误,尽请指正。


文章目录


一、状态模式是什么?

  1. 经典定义:当一个对象的内在状态改变时允许改变其行为,这个对象看起来是改变了其类
  2. 简单来说,状态决定行为

二、实现方式

1.枚举

1.用枚举去设置各种状态
2.使用switch case去转换状态

    public enum LightState
    {
        Off,
        Warm,
        White,
        WarmWhite
    }
    public LightState CurrentState = LightState.Off;
    public void OnChangeState()
    {
        //状态转换
        switch (CurrentState)
        {
            case LightState.Off:
                CurrentState = LightState.Warm;
                break;
            case LightState.Warm:
                CurrentState = LightState.White;
                break;
            case LightState.White:
                CurrentState = LightState.WarmWhite;
                break;
            case LightState.WarmWhite:
                CurrentState = LightState.Off;
                break;
            default:
                CurrentState = LightState.Off;
                break;
        }
        //状态行为
        switch (CurrentState)
        {
            case LightState.Off:
                mLight.color = Color.black;
                mMat.SetColor("_EmissionColor", mLight.color);
                break;
            case LightState.Warm:
                mLight.color = new Color(0.8f, 0.5f, 0);
                mMat.SetColor("_EmissionColor", mLight.color);
                break;
            case LightState.White:
                mLight.color = new Color(0.8f, 0.8f, 0.8f);
                mMat.SetColor("_EmissionColor", mLight.color);
                break;
            case LightState.WarmWhite:
                mLight.color = new Color(1, 0.85f, 0.6f);
                mMat.SetColor("_EmissionColor", mLight.color);
                break;
            default:
                mLight.color = Color.black;
                mMat.SetColor("_EmissionColor", mLight.color);
                break;
        }
    }

2.多态、虚函数

1.状态接口类

using UnityEngine.UI;
public abstract class IState 
{
    public float Duration;
    //状态行为
    public abstract void SwitchState(ImgState imgState);
    //切换状态
    public abstract void StateStart(ImgState imgState);
}

2.具体状态类

using UnityEngine;
public class RedState : IState
{
    public RedState(float Duration)
    {
        this.Duration = Duration;
    }

    public override void StateStart(ImgState imgState)
    {
        imgState.Img.color = Color.red;
        imgState.timer = Duration;
    }

    public override void SwitchState(ImgState imgState)
    {
        imgState.SetState(new GreenState(2.0f));
    }
}


using UnityEngine;
public class GreenState : IState
{
    public GreenState(float Duration)
    {
        this.Duration = Duration;
    }
    public override void StateStart(ImgState imgState)
    {
        imgState.Img.color = Color.green;
        imgState.timer = Duration;
    }
    public override void SwitchState(ImgState imgState)
    {
        imgState.SetState(new RedState(1.0f));
    }
}

3.对象状态切换

using UnityEngine;
using UnityEngine.UI;
public class ImgState : MonoBehaviour
{
    [SerializeField] Image img;
    public Image Img
    {
        get
        {
            return img;
        }
    }
    public IState state;
    public float timer = 1.0f;

    void Start()
    {
        SetState(new RedState(1.5f));
    }

    void Update()
    {
        timer-= Time.deltaTime;
        if (timer <= 0)
        {
            state.SwitchState(this);
        }
    }
    //设置当前状态
    public void SetState(IState state)
    {
        this.state = state;
        state.StateStart(this);
    }
}

三、优缺点

1.优点

2.缺点

3.适用场景

附:参考链接

游戏设计模式
大话设计模式

举报

相关推荐

0 条评论