File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717using UnityEngine ;
1818using UnityEngine . Assertions ;
1919using UnityEngine . UIElements ;
20- using Debug = UnityEngine . Debug ;
2120
2221namespace Nonatomic . VSM2 . Editor . StateGraph . Nodes
2322{
@@ -136,11 +135,12 @@ protected virtual void UpdatePosition()
136135 {
137136 var position = NodeModel . Position ;
138137
139- var rect = this . GetPosition ( ) ;
138+ var rect = GetPosition ( ) ;
140139 rect . position = position ;
141140
142141 SetPosition ( rect ) ;
143142
143+ if ( StateMachineModel == null ) return ;
144144 EditorUtility . SetDirty ( StateMachineModel ) ;
145145 EditorApplication . delayCall += ( ) => AssetDatabase . SaveAssets ( ) ;
146146 }
@@ -403,6 +403,9 @@ protected virtual VisualElement CreatePropertyContainer()
403403
404404 protected virtual void AddProperties ( VisualElement container )
405405 {
406+ if ( NodeModel == null ) return ;
407+ if ( NodeModel . State == null ) return ;
408+
406409 var scrollView = new ScrollView ( ) ;
407410 container . Add ( scrollView ) ;
408411
Original file line number Diff line number Diff line change @@ -14,22 +14,29 @@ public class StateNodeGraphStateManager : NodeGraphStateManager
1414
1515 public StateNodeGraphStateManager ( string id ) : base ( id )
1616 {
17-
17+ // ...
1818 }
1919
2020 public void LoadModelFromStateController ( )
2121 {
22- if ( string . IsNullOrEmpty ( StateControllerId ) ) return ;
23-
22+ // Look for the controller that matches the stored ID.
2423 var stateMachines = GameObject . FindObjectsByType < StateMachineController > ( FindObjectsInactive . Exclude , FindObjectsSortMode . None ) ;
25- var stateMachineController = stateMachines . FirstOrDefault ( smc => smc . Id == StateControllerId ) ;
26- if ( ! stateMachineController ) return ;
27-
28- SetModel ( stateMachineController . Model ) ;
24+ var controller = stateMachines . FirstOrDefault ( smc => smc . Id == StateControllerId ) ;
25+
26+ if ( controller )
27+ {
28+ SetModel ( controller . Model ) ;
29+ }
30+ else
31+ {
32+ Debug . LogWarning ( $ "StateMachineController with ID { StateControllerId } not found.") ;
33+ }
2934 }
3035
3136 public void SetStateControllerId ( string id )
3237 {
38+ if ( string . IsNullOrEmpty ( id ) ) return ;
39+
3340 StateControllerId = id ;
3441 SaveState ( ) ;
3542 }
@@ -43,16 +50,14 @@ protected override void ResetState()
4350 public override void LoadState ( )
4451 {
4552 base . LoadState ( ) ;
46-
4753 StateControllerId = EditorPrefs . GetString ( GetKey ( StateMachineControllerIdKey ) ) ;
4854 }
4955
5056 public override void SaveState ( )
5157 {
5258 base . SaveState ( ) ;
53-
5459 if ( ! Model ) return ;
55-
60+
5661 EditorPrefs . SetString ( GetKey ( StateMachineControllerIdKey ) , StateControllerId ) ;
5762 }
5863 }
Original file line number Diff line number Diff line change @@ -221,16 +221,8 @@ private void HandlePlayModeStateChanged(PlayModeStateChange stateChange)
221221 var stateManager = ( StateNodeGraphStateManager ) StateManager ;
222222 var gridPos = stateManager . GridPosition ;
223223
224- switch ( stateChange )
225- {
226- case PlayModeStateChange . EnteredPlayMode :
227- case PlayModeStateChange . EnteredEditMode :
228- case PlayModeStateChange . ExitingEditMode :
229- case PlayModeStateChange . ExitingPlayMode :
230- stateManager . LoadModelFromStateController ( ) ;
231- PopulateGraph ( StateManager . Model , recentre : true ) ;
232- break ;
233- }
224+ stateManager . LoadModelFromStateController ( ) ;
225+ PopulateGraph ( StateManager . Model , recentre : true ) ;
234226
235227 EditorApplication . delayCall += ( ) =>
236228 {
Original file line number Diff line number Diff line change @@ -148,5 +148,10 @@ private void CreateStateMachine()
148148 if ( ! _model ) return ;
149149 _stateMachine = new StateMachine ( _model , this ) ;
150150 }
151+
152+ private void OnValidate ( )
153+ {
154+ CreateUniqueId ( ) ;
155+ }
151156 }
152157}
You can’t perform that action at this time.
0 commit comments