Skip to content

Instantly share code, notes, and snippets.

@hon454
Created February 8, 2018 05:13
Show Gist options
  • Save hon454/b500f279ec083aa612337a15a4dd35a0 to your computer and use it in GitHub Desktop.
Save hon454/b500f279ec083aa612337a15a4dd35a0 to your computer and use it in GitHub Desktop.
Good Coroutine Example 1/2
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