JsonPrefab Class |
Namespace: Kit.Containers
The JsonPrefab type exposes the following members.
| Name | Description | |
|---|---|---|
| InstantiateT(IEnumerable, Boolean) | Instantiate MonoBehaviours based on a list of objects and populate them. | |
| InstantiateT(Object, Boolean) | Instantiate a MonoBehaviour based on an object and populates it. | |
| InstantiateT(String, JObject, Boolean) | Instantiate a MonoBehaviour based on an JObject and populates it. | |
| InstantiateT(String, IEnumerableJObject, Boolean) | Instantiate MonoBehaviours based on a list of JObjects and populate them. | |
| ReplaceValues | Replace the names enclosed in {} with respective values from a JObject. | |
| Save(MonoBehaviour, JObject) | Save the current values of a MonoBehaviour back to its JObject. | |
| Save(MonoBehaviour, Object) | Save the current values of a MonoBehaviour back to its state. | |
| SaveT(ListT, ListJObject) | Save the current values of a list of MonoBehaviours back to their JObjects. | |
| SaveT(ListT, ListObject) | Save the current values of a list of MonoBehaviours back to their states. |
There are three ways to use this class – Mono-only, State-Mono or JObject-Mono method:
In the Mono-only mode, you put a JsonConverter attribute on a MonoBehaviour with type JsonPrefabConverter and the prefab path as its first argument. On the Json side, you use the actual MonoBehaviour type in the GameState object. Whenever the Json is loaded, the converter will instantiate objects and assign them in the GameState. This way, the MonoBehaviours will be strongly bound to the GameState. The advantage of this is that any changes in the MonoBehaviours will be automatically be reflected in the state. The disadvantage being if MonoBehaviours are destroyed, they'll become or inaccessible in the GameState.
In the State-Mono mode, you put JsonPrefabAttribute on a separate class denoting a MonoBehaviour's state and use that in the GameState. The Json will be loaded normally, and nothing will happen by itself. To instantiate objects, you have to call InstantiateT(IEnumerable, Boolean) on state-objects whenever you want. The advantage of this is that you have more control on the life-cycle of MonoBehaviours and states will not become if MonoBehaviours are destroyed. The disadvantage is that you have to save back state manually if/when MonoBehaviours are changed. To aid this, there is a method called Save(MonoBehaviour, Object) which can be called manually and is automatically called when a Json-created MonoBehaviour is destroyed. This is the slowest method since we have to convert to and from JObject each time we have to populate data.
The JObject mode is very similar to State-Mono mode, except that you put JObject in GameState wherever you want to work with MonoBehaviours and call InstantiateT(String, IEnumerableJObject, Boolean) by providing it the prefab path and JObjects to instantiate directly. This is the faster method and doesn't have problems like having to use JsonSubtypes to create the correct State-object type.
{
"Buildings":
[
{
"Type": "ProducerBuilding",
"Position": {"x": 1, "y": 1}
},
{
"Type": "BankBuilding",
"Position": {"x": 2, "y": 2}
}
]
}[JsonConverter(typeof(JsonPrefabConverter), "Buildings/{Type}")] [JsonObject(MemberSerialization.OptIn)] public class Building: MonoBehaviour { [JsonProperty] public string Type; [JsonProperty] public Vector2 Position; } public class GameState { public List<Building> Buildings; }
[JsonObject(MemberSerialization.OptIn)] public class Building: MonoBehaviour { [JsonProperty] public string Type; [JsonProperty] public Vector2 Position; } [JsonPrefab("Buildings/{Type}")] public class BuildingState { public string Type; public Vector2 Position; } public class GameState { public List<BuildingState> Buildings; } JsonPrefab.Instantiate<Building>(GameState.Buildings);
[JsonObject(MemberSerialization.OptIn)] public class Building: MonoBehaviour { [JsonProperty] public string Type; [JsonProperty] public Vector2 Position; } public class GameState { public List<JObject> Buildings; } JsonPrefab.Instantiate<Building>("Buildings/{Type}", GameState.Buildings);