Created
February 8, 2018 05:13
-
-
Save hon454/b500f279ec083aa612337a15a4dd35a0 to your computer and use it in GitHub Desktop.
Good Coroutine Example 1/2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
public partial class PlayerFSM : MonoBehaviour | |
{ | |
public enum PlayerState | |
{ | |
Idle, | |
Run, | |
} | |
public PlayerState currentState; | |
private bool _isNewState; | |
private void OnEnable() | |
{ | |
StartCoroutine("FSMMain"); | |
} | |
private void Update() | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
SetState(PlayerState.Run); | |
} | |
else | |
{ | |
SetState(PlayerState.Idle); | |
} | |
} | |
private void SetState(PlayerState newState) | |
{ | |
_isNewState = true; | |
currentState = newState; | |
} | |
private IEnumerator FSMMain() | |
{ | |
while(Application.isPlaying) | |
{ | |
_isNewState = false; | |
yield return StartCoroutine(currentState.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment