Unity 状态切换

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// 所有状态的基类 
/// 如果子类不用挂在物体上,这个类可以不继承MonoBehaviour
/// </summary>
public abstract  class FSMStateBase :MonoBehaviour {


    public virtual void DoBeforeEntering()
    {

    }

    public abstract void DoAct();
   
    public virtual void DoAfterLeaving()
    {

    }



}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
///  对当前状态进行更改
/// </summary>
public class FSMStatePerform {

    private static FSMStatePerform _Instance=null;

    public static FSMStatePerform GetInstance()
    {
        if (_Instance == null) {
            _Instance = new FSMStatePerform();
        }
        return _Instance;
    }

    private  FSMStateBase _CurrentState=null;


    //Change state
    public   void ChangeCurrentState(FSMStateBase TargetState)
    {
        if (_CurrentState == TargetState|| TargetState == null)
        {
            return;
        }
        if (_CurrentState!=null)
        {
            _CurrentState.DoAfterLeaving();
            
        }
      
            _CurrentState = TargetState;
            _CurrentState.DoBeforeEntering();
           

    }


void  Update()
{
 _CurrentState.DoAct();
}


}

 

 

用法:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class State1 :  FSMStateBase
{

    //public State1()
    //{
    //  //  _StateId = StateId.state1;
    //}


    public override void DoBeforeEntering()
    {
        Debug.Log("State1 DoBeforeEntering");
    }

    public override void DoAct()
    {
        Debug.Log("State1 DoAct");
    }

    public override void DoAfterLeaving()
    {
        Debug.Log("State1 DoAfterLeaving");
    }

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class State2 : FSMStateBase {

    //public State2()
    //{
    //  //  _StateId = StateId.state2;
    //}


    public override void DoBeforeEntering()
    {
        Debug.Log("State2 DoBeforeEntering");
    }

    public override void DoAct()
    {
        Debug.Log("State2 DoAct");
    }

    public override void DoAfterLeaving()
    {
        Debug.Log("State2 DoAfterLeaving");
    }



    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnEvent : MonoBehaviour {

    [SerializeField]
    private  State1 state1;
    [SerializeField]
    private State2 state2;

    // Use this for initialization
    void Start () {
        //state1 = new State1();
       // state2 = new State2();
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    public void OnButtonClick1()
    {
        FSMStatePerform.GetInstance().ChangeCurrentState(state1);
    }
    public void OnButtonClick2()
    {
        FSMStatePerform.GetInstance().ChangeCurrentState(state2);
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值