Getting Unity c # simple state machine

The state machine model in the role of unity is very large, you can move and jump achieve scene of roles, including some of the animation, a lot of unity in the framework is also very common, broad divergent thinking, here is a simple state machine implementation, annotated

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

stateObject class public abstract
{
protected StateManger State;
public stateObject (StateManger _sm)
{
State = _sm;
}
// entry method
public abstract void enterState ();
// methods leave
public abstract void ExiState ();
// method of continuously updated
public abstract UpdateState void ();
}
// standing state
public class IdleState: stateObject
{
public IdleState (StateManger state): Base (state)
{

}

the override void enterState public ()
{
Debug.Log ( "enter standing state");
}

the override void ExiState public ()
{
Debug.Log ( "leaving standing state");
}

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 void enterState the override ()
{
Debug.Log ( "enter the movement state");
}

the override void ExiState public ()
{
Debug.Log ( "moving away state");
}

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 void enterState the override ()
{
Debug.Log ( "enter dead state");
}

the override void ExiState public ()
{
Debug.Log ( "leave dead state");
}

the override void UpdateState public ()
{
Debug.Log ( "Death enters update state");

IF (Input.GetKey (KeyCode.I))
{
state.ChangeState ( "the Idle");
}
}
}
public class StateManger {
// dictionary the storage state of
the Dictionary <String, stateObject> DIC = new new the Dictionary <String, stateObject> ();
// current state
stateObject currentState;
// status register
public void Region (String stateName, stateObject state)
{
// this is determined whether the dictionary is present button
iF (dic.ContainsKey (stateName)!)
{
dic.Add (stateName, state);
}
}
// set default
public void SetDefat (String stateName)
{
// determines whether this state exists dictionary
IF (dic.ContainsKey (stateName))
{
// assign to the presence currentState
currentState DIC = [stateName];
// current status of the calls into (enterState) Method
currentstate.EnterState ();
}
}
// change state
public void ChangeState (String stateName)
{
// determines whether this state exists in the dictionary
iF (dic.ContainsKey (stateName))
{
// if the current state is empty
iF (currentState! = null)
{
a leaving call process state on //
currentstate. ExiState ();
// get to the state assigned to currentState
currentState DIC = [stateName];
// call to the Access method taken
currentstate.EnterState ();
}
}
}
// update status
public void UpdateState ()
{
Debug.Log ( "Update status");
IF (currentState = null!)
{
// current state UpdateState method
currentstate.UpdateState ();
}
}
}
public class FMSC,: {MonoBehaviour
StateManger new new StateManger SM = ();
// the this for the Initialization the Use
void Start () {
// method to register standing
sm.Region ( "Idle", new new IdleState (SM));
// registration method of death
sm.Region ( "Die", new DieState (sm) );
// registration method of a mobile
sm.Region ( "the move", new new MoveState (SM));
// set default state
sm.SetDefat ( "the Idle");
}

// Called the Update Once per iS Frame
void the Update () {
// current status of the call duration UpdateState method
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

Each state has its own methods, there are different things to do in each state, to reduce coupling of the code
--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/11001406.html
Recommended