Unity c# 狀態機的簡單入門


狀態機模式在unity中作用是非常大的,可以實現角色的移動和場景的跳轉,包括一些動畫的播放,在很多unity框架中也是很常見的,發散思維廣闊,下面是簡單的狀態機的實現,有注釋

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum StateType
{
Idle,
Die,
Move,
}

public abstract class StateObject
{
protected StateManger state;
public StateObject(StateManger _sm)
{
state = _sm;
}
//進入方法
public abstract void EnterState();
//離開方法
public abstract void ExiState();
//持續更新方法
public abstract void UpdateState();
}
//站着狀態
public class IdleState : StateObject
{
public IdleState(StateManger state):base(state)
{

}

public override void EnterState()
{
Debug.Log("進入站着狀態");
}

public override void ExiState()
{
Debug.Log("離開站着狀態");
}

public override void UpdateState()
{
Debug.Log("等待站着狀態");
if (Input .GetKey(KeyCode.M))
{
Debug.Log("按下咯");
state.ChangeState("Move");
}
if (Input.GetKey(KeyCode.D))
{
state.ChangeState("Die");
}
}
}
//移動狀態
public class MoveState : StateObject
{
public MoveState(StateManger state):base(state)
{

}
public override void EnterState()
{
Debug.Log("進入移動狀態");
}

public override void ExiState()
{
Debug.Log("離開移動狀態");
}

public override void UpdateState()
{
Debug.Log("進入移動更新狀態");
if (Input.GetKey(KeyCode.D))
{
state.ChangeState("Die");
}
if (Input.GetKey(KeyCode.I))
{
state.ChangeState("Idle");
}
}
}
//死亡狀態
public class DieState : StateObject
{
public DieState(StateManger state) : base(state)
{

}
public override void EnterState()
{
Debug.Log("進入死亡狀態");
}

public override void ExiState()
{
Debug.Log("離開死亡狀態");
}

public override void UpdateState()
{
Debug.Log("進入死亡更新狀態");

if (Input.GetKey(KeyCode.I))
{
state.ChangeState("Idle");
}
}
}
public class StateManger {
//字典存儲狀態
Dictionary<string, StateObject> dic = new Dictionary<string, StateObject>();
//當前狀態
StateObject currentstate;
//注冊狀態
public void Region(string statename,StateObject state)
{
//判斷字典中是否存在這個鍵
if (!dic.ContainsKey(statename))
{
dic.Add(statename,state);
}
}
//設置默認狀態
public void SetDefat(string statename)
{
//判斷字典中是否存在這個狀態
if (dic.ContainsKey(statename))
{
//存在就賦值給currentstate
currentstate = dic[statename];
//調用當前狀態的進入(EnterState)方法
currentstate.EnterState();
}
}
//改變狀態
public void ChangeState(string statename)
{
//判斷字典中是否存在這個狀態
if (dic.ContainsKey(statename))
{
//當前狀態是否為空
if (currentstate!=null)
{
//調用上一個狀態的離開方法
currentstate.ExiState();
//把取到的狀態賦值給currentstate
currentstate = dic[statename];
//調用取到狀態的進入方法
currentstate.EnterState();
}
}
}
//更新狀態
public void UpdateState()
{
Debug.Log("更新狀態");
if (currentstate!=null)
{
//當前狀態的UpdateState方法
currentstate.UpdateState();
}
}
}
public class FMSC : MonoBehaviour {
StateManger sm = new StateManger();
// Use this for initialization
void Start () {
//注冊站着的方法
sm.Region("Idle",new IdleState(sm));
//注冊死亡的方法
sm.Region("Die",new DieState(sm));
//注冊移動的方法
sm.Region("Move",new MoveState(sm));
//設置默認狀態
sm.SetDefat("Idle");
}

// Update is called once per frame
void Update () {
//持續調用當前狀態的UpdateState方法
sm.UpdateState();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

--------------------- 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM