UnityAction使⽤回调函数进⾏解耦
using UnityEngine;
// 枚举
public enum GameState
{
Start,
Gaming,
End
}
public class GameManager : MonoBehaviour
{
// 单例
public static GameManager Instance;
/
/ 字段
protected GameState state = GameState.Start;
public GameState NowState
{
set
{
if (state != value)
{
state = value;
OnChangeState(state);
}
}
get { return state; }
}
// 委托
public System.Action<GameState> OnChangeState;
private void Awake()
{
Instance = this;
// 添加委托
OnChangeState += ChangeState;
}
private void Start()
{
// ⽰例
NowState = GameState.Gaming;
}
private void ChangeState(GameState gameState) {
Debug.Log(this.name + " " + gameState);
}
}
using System.Collections;
using UnityEngine;
public class UIManager : MonoBehaviour
{
IEnumerator Start()
enum函数{
GameManager.Instance.OnChangeState += ChangeState; yield return new WaitForSeconds(1f);
GameManager.Instance.NowState = GameState.End;
}
private void ChangeState(GameState gameState)
{
Debug.Log(this.name + " " + gameState);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论