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();
            _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);
    }


}

猜你喜欢

转载自blog.csdn.net/m0_37981386/article/details/84988608