Skip to content

Commit

Permalink
Don't require state components to be on the same game object as the s…
Browse files Browse the repository at this point in the history
…tate machine.
  • Loading branch information
richardlord committed Dec 31, 2017
1 parent 7cc9926 commit f2660e3
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Assets/StateMachine/StateMachine.cs
Expand Up @@ -80,7 +80,7 @@ private void SerializeComponents( MonoBehaviour[] components )
MonoBehaviour component = components[i];
if( component != null )
{
componentsDictionary[component.GetInstanceID()] = new ComponentData( component.GetInstanceID(), component.GetType(), JsonUtility.ToJson( component ) );
componentsDictionary[component.GetInstanceID()] = new ComponentData( component.GetInstanceID(), component.GetType(), JsonUtility.ToJson( component ), component.gameObject );
Destroy( component );
}
}
Expand All @@ -93,7 +93,7 @@ private void SerializeComponents( List<ActiveComponentData> components )
ActiveComponentData data = components[i];
if( data.component != null )
{
componentsDictionary[data.id] = new ComponentData( data.id, data.component.GetType(), JsonUtility.ToJson( data.component ) );
componentsDictionary[data.id] = new ComponentData( data.id, data.component.GetType(), JsonUtility.ToJson( data.component ), data.component.gameObject );
Destroy( data.component );
}
}
Expand All @@ -105,7 +105,7 @@ private void DeserializeComponents( List<int> ids, List<ActiveComponentData> sto
{
int id = ids[i];
ComponentData data = componentsDictionary[id];
MonoBehaviour component = gameObject.AddComponent( data.type ) as MonoBehaviour;
MonoBehaviour component = data.gameObject.AddComponent( data.type ) as MonoBehaviour;
JsonUtility.FromJsonOverwrite( data.data, component );
store.Add( new ActiveComponentData( id, component ) );
}
Expand All @@ -123,12 +123,14 @@ class ComponentData
public int id;
public Type type;
public string data;
public GameObject gameObject;

public ComponentData( int id, Type type, string data )
public ComponentData( int id, Type type, string data, GameObject gameObject )
{
this.id = id;
this.type = type;
this.data = data;
this.gameObject = gameObject;
}
}

Expand Down

0 comments on commit f2660e3

Please sign in to comment.