github ๋งํฌ: https://github.com/thefuntastic/Unity3d-Finite-State-Machine


1. ์›ํ•˜๋Š” ์ƒํƒœ๊ฐ’๋“ค์„ enum ํƒ€์ž…์œผ๋กœ ๋‚˜์—ดํ•œ๋‹ค.


public enum States
{
Init,
Play,
Win,
Lose
}


2. ๋งด๋ฒ„ ๋ณ€์ˆ˜๋กœ ์ œ๋„ค๋ฆญ ํƒ€์ž…์˜ StateMachine์„ ๋ฐฉ๊ธˆ ์„ ์–ธํ•œ enum์œผ๋กœ ์„ ์–ธํ•œ๋‹ค


StateMachine<States> fsm;


3. Awake ์œ ๋‹ˆํ‹ฐ ์ด๋ฒคํŠธ ํ•จ์ˆ˜์—์„œ ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ค€๋‹ค. new๋ฅผ ํ†ตํ•ด์„œ๊ฐ€ ์•„๋‹Œ ์ •ํ•ด์ง„ ์ •์  ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•ด์ค˜์•ผํ•จ.


public void Awake()
{
fsm = StateMachine<States>.Initialize(this);
}


4. Start ์œ ๋‹ˆํ‹ฐ ์ด๋ฒคํŠธ๋ผ๋˜์ง€ ์•„๋ฌด๋Œ€์„œ๋‚˜ ChangeState ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟ”์ค€๋‹ค. ๋ฐ”๊พธ๊ธฐ ์ „ ์ƒํƒœ์—์„  ์•„๋ฌด ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ์ดˆ๊ธฐ ์ง„์ž…์ƒํƒœ๋ฅผ ๊ผญ ์„ค์ •ํ•ด์ค˜์•ผํ•จ.


fsm.ChangeState(States.Init);


5. ๋ฐ”๋€ ์ƒํƒœ์— ๋”ฐ๋ผ ์•„๋ž˜๊ฐ™์€ ํ•จ์ˆ˜๋“ค์ด ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค. ์ดˆ๊ธฐํ™”ํ• ๋•Œ ์ตœ์ดˆ ํ•œ ๋ฒˆ ๋ฆฌํ”Œ๋ ‰์…˜์„ ํ†ตํ•ด enum๊ฐ’๋งˆ๋‹ค ํ•จ์ˆ˜ ํฌ์ธํ„ฐ๋ฅผ ์บ์‹ฑํ•˜๋Š”๋“ฏํ•จ.

- ์ƒํƒœ๋ช…_Enter: ์ƒํƒœ ์ง„์ž…ํ–ˆ์„๋•Œ. void ๋ฆฌํ„ด ํ•จ์ˆ˜๋กœ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‚˜ IEnumerator๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฝ”๋ฃจํ‹ด์œผ๋กœ๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. (์ด๋•Œ๋Š” ์ „ํ™˜๋œ ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ ์ „ํ™˜ ์ค‘์ธ ์ƒํƒœ๋กœ ์ธ์‹ํ•˜๋Š”๋“ฏ)

- ์ƒํƒœ๋ช…_Exit: ์ƒํƒœ ๋‚˜๊ฐˆ๋•Œ. Enter์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฝ”๋ฃจํ‹ด์ฒ˜๋Ÿผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

- ์ƒํƒœ๋ช…_FixedUpdate: ํ•ด๋‹น ์ƒํƒœ์—์„œ FixedUpdate ๊ฐฑ์‹  ์ฃผ๊ธฐ๋งˆ๋‹ค ํ˜ธ์ถœ๋จ

- ์ƒํƒœ๋ช…_Update: ํ•ด๋‹น ์ƒํƒœ์—์„œ Update ๊ฐฑ์‹  ์ฃผ๊ธฐ๋งˆ๋‹ค ํ˜ธ์ถœ๋จ

- ์ƒํƒœ๋ช…_LateUpdate: ์„ค๋ช… ์ƒ๋žต

- ์ƒํƒœ๋ช…_Finally: ํ•ด๋‹น ์ƒํƒœ ๋‚˜๊ฐˆ๋•Œ์™€ ์œ ์‚ฌํ•˜๋‚˜ ChangeState์— ๋‘๋ฒˆ์งธ ์ธ์ž์— Overwrite๊ฐ’์„ ์ „๋‹ฌํ•ด์„œ ์ƒํƒœ๋ฅผ ๊ฐ•์ œ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋•Œ ์ฝ”๋ฃจํ‹ด์œผ๋กœ ์„ ์–ธ๋œ _Exitํ•จ์ˆ˜๊ฐ€ ๋ฌด์‹œ๋  ์ˆ˜๊ฐ€ ์žˆ์ง€๋งŒ _Finally๋Š” ์ƒํƒœ๋ฅผ ๋‚˜๊ฐˆ๋•Œ ๋ฐ˜๋“œ์‹œ ํ˜ธ์ถœ๋จ. ์ž์„ธํ•œ๊ฑด github ๋งํฌ์„ค๋ช… ์ฐธ์กฐ


์•„๋ž˜๋Š” github์— ์“ฐ์—ฌ์ง„ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ


void Init_Enter()
{
Debug.Log("We are now ready");
}

//Coroutines are supported, simply return IEnumerator
IEnumerator Play_Enter()
{
Debug.Log("Game Starting in 3");
yield return new WaitForSeconds(1);

Debug.Log("Game Starting in 2");
yield return new WaitForSeconds(1);

Debug.Log("Game Starting in 1");
yield return new WaitForSeconds(1);

Debug.Log("Start");
}

void Play_Update()
{
Debug.Log("Game Playing");
}

void Play_Exit()
{
Debug.Log("Game Over");
}


์ง„์งœ ๊ฒ.๋‚˜.์‰ฝ.์Šต.๋‹ˆ.๋‹ค.


๋ฌผ๋ก  ํ•œ ๊ฐ์ฒด์—์„œ ์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉ๋„ ๊ฐ€๋Šฅํ•จ.. ๋‹ค๋งŒ ํ•จ์ˆ˜๋ช…์ด ์ค‘๋ณต ๋  ์ˆ˜๋Š” ์—†์œผ๋‹ˆ ๊ทธ๊ฑฐ๋งŒ ์œ ์˜ํ•ด๋‘๋ฉด ๋  ๋“ฏ


github ๋งํฌ: https://github.com/thefuntastic/Unity3d-Finite-State-Machine