├── .gitattributes ├── README.md └── Scripts ├── State.cs ├── State.cs.meta ├── StateManager.cs ├── StateManager.cs.meta ├── TestDataContainer.cs ├── TestDataContainer.cs.meta ├── TestInputState.cs ├── TestInputState.cs.meta ├── TestStringState.cs └── TestStringState.cs.meta /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Component State Machine 2 | A component based state machine for Unity 3 | 4 | # How to use 5 | Use the State class to create your logic, use the StateManager to handle the active state and create your own data container according to your needs. 6 | 7 | You can watch this video to get the full details on how to make your own or how to use this one further 8 | 9 | https://youtu.be/N0rD1wExelE 10 | 11 | # Support 12 | If you want more tutorials and tools, these projects are completely patreon funded. Be one here : https://www.patreon.com/csharpaccent? 13 | 14 | # License 15 | CC-BY-4.0 If you use this in your project provide proper attribution to: 16 | 17 | Athos Kele www.sharpaccent.com 18 | 19 | Feel free to contact me if you can't work with this license 20 | -------------------------------------------------------------------------------- /Scripts/State.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | namespace SA.CSM 6 | { 7 | public abstract class State : MonoBehaviour 8 | { 9 | 10 | public abstract void Tick(float delta); 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Scripts/State.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c85e4c748ca1651419eeb1c8bc6296b5 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Scripts/StateManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | 6 | namespace SA.CSM 7 | { 8 | public class StateManager : MonoBehaviour 9 | { 10 | [SerializeField] 11 | State currentState; 12 | [SerializeField] 13 | State defaultState; 14 | 15 | 16 | private void Start() 17 | { 18 | 19 | } 20 | 21 | private void Update() 22 | { 23 | if (currentState != null) 24 | { 25 | currentState.Tick(Time.deltaTime); 26 | } 27 | } 28 | 29 | public void ChangeState(State targetState) 30 | { 31 | if (currentState != null) 32 | { 33 | //currentState.Exit() 34 | } 35 | 36 | currentState = targetState; 37 | 38 | if (currentState != null) 39 | { 40 | //currentState.OnEnter() 41 | } 42 | } 43 | 44 | public void BackToDefaultState() 45 | { 46 | ChangeState(defaultState); 47 | } 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Scripts/StateManager.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2af2a904ed2551a42b5692ed1d8db375 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Scripts/TestDataContainer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | namespace SA 5 | { 6 | public class TestDataContainer : MonoBehaviour 7 | { 8 | public float horizontal; 9 | public float vertical; 10 | public bool space; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Scripts/TestDataContainer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: aadd3c4ceee06ea4782626db6dabc6cc 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Scripts/TestInputState.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | namespace SA.CSM 5 | { 6 | public class TestInputState : State 7 | { 8 | TestDataContainer dc; 9 | StateManager states; 10 | TestStringState stringState; 11 | 12 | private void Start() 13 | { 14 | dc = GetComponentInParent(); 15 | states = GetComponentInParent(); 16 | stringState = GetComponent(); 17 | } 18 | 19 | public override void Tick(float delta) 20 | { 21 | dc.horizontal = Input.GetAxis("Horizontal"); 22 | dc.vertical = Input.GetAxis("Vertical"); 23 | dc.space = Input.GetKeyDown(KeyCode.Space); 24 | 25 | if (dc.space) 26 | { 27 | states.ChangeState(stringState); 28 | } 29 | 30 | Debug.Log("input state"); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Scripts/TestInputState.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1935767115955524db8fd3bae58014fa 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Scripts/TestStringState.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | namespace SA.CSM 5 | { 6 | public class TestStringState : State 7 | { 8 | public float timerMax = 2; 9 | float timer; 10 | 11 | StateManager states; 12 | 13 | TestInputState inputState; 14 | 15 | private void Start() 16 | { 17 | states = GetComponent(); 18 | inputState = GetComponent(); 19 | 20 | } 21 | 22 | public override void Tick(float delta) 23 | { 24 | timer += delta; 25 | 26 | if (timer > timerMax) 27 | { 28 | timer = 0; 29 | Debug.Log("String State"); 30 | } 31 | 32 | if (Input.GetKeyDown(KeyCode.Space)) 33 | { 34 | states.ChangeState(inputState); 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Scripts/TestStringState.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b086b3b47a9125b439be4206ec28840d 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | --------------------------------------------------------------------------------