123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using System;
- using UnityEngine;
- namespace Animancer.FSM
- {
-
- partial class StateMachine<TState>
- {
-
-
-
-
-
-
-
-
- [Serializable]
- public class WithDefault : StateMachine<TState>
- {
-
- [SerializeField]
- private TState _DefaultState;
-
-
-
-
-
-
-
- public TState DefaultState
- {
- get => _DefaultState;
- set
- {
- _DefaultState = value;
- if (_CurrentState == null && value != null)
- ForceSetState(value);
- }
- }
-
-
-
- public readonly Action ForceSetDefaultState;
-
-
- public WithDefault()
- {
-
- ForceSetDefaultState = () => ForceSetState(_DefaultState);
- }
-
-
- public WithDefault(TState defaultState)
- : this()
- {
- _DefaultState = defaultState;
- ForceSetState(defaultState);
- }
-
-
- public override void InitializeAfterDeserialize()
- {
- if (_CurrentState != null)
- {
- using (new StateChange<TState>(this, null, _CurrentState))
- _CurrentState.OnEnterState();
- }
- else if (_DefaultState != null)
- {
- using (new StateChange<TState>(this, null, CurrentState))
- {
- _CurrentState = _DefaultState;
- _CurrentState.OnEnterState();
- }
- }
-
- }
-
-
-
-
-
-
-
- public bool TrySetDefaultState() => TrySetState(DefaultState);
-
-
-
-
-
-
- public bool TryResetDefaultState() => TryResetState(DefaultState);
-
- #if UNITY_EDITOR
-
-
- public override int GUILineCount => 2;
-
-
- public override void DoGUI(ref Rect area)
- {
- area.height = UnityEditor.EditorGUIUtility.singleLineHeight;
- UnityEditor.EditorGUI.BeginChangeCheck();
- var state = StateMachineUtilities.DoGenericField(area, "Default State", DefaultState);
- if (UnityEditor.EditorGUI.EndChangeCheck())
- DefaultState = state;
- StateMachineUtilities.NextVerticalArea(ref area);
- base.DoGUI(ref area);
- }
-
- #endif
-
- }
- }
- }
|