Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demo of EasySave2 integration into Full Inspector
using System.Reflection;
using UnityEngine;
using UnityObject = UnityEngine.Object;
namespace FullInspector.Serializers.EasySave2 {
public class EasySave2Serializer : BaseSerializer {
private const BindingFlags MethodSearchFlags =
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
internal override string Serialize(UnityObject target) {
MethodInfo method = target.GetType().GetMethod("Save", MethodSearchFlags);
if (method == null) {
Debug.LogWarning("Object is missing Save method", target);
return "";
}
method.Invoke(target, null);
return "";
}
internal override void Deserialize(UnityObject target, string serializedState) {
MethodInfo method = target.GetType().GetMethod("Save", MethodSearchFlags);
if (method == null) {
Debug.LogWarning("Object is missing Restore method", target);
return;
}
method.Invoke(target, null);
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using FullInspector.Serializers.EasySave2;
namespace FullInspector.Samples {
public class Player : BaseBehavior<EasySave2Serializer> {
public string playerName; // A unique player name.
public int hitpoints; // How many hitpoints this play has.
public List<int> inventory; // Contains IDs which relate to items the player has.
// FullInspector (via EasySave2Serializer) will automatically invoke this method
private void Save() {
// Save each of our variables to file, using the (unique) playerName plus the variable
// name to identify it.
ES2.Save(hitpoints, playerName + "hitpoints");
ES2.Save(inventory, playerName + "inventory");
ES2.Save(transform, playerName + "transform");
}
// FullInspector (via EasySave2Serializer) will automatically invoke this method
private void Load() {
// If there is no data to load, exit early.
if (!ES2.Exists(playerName + "hitpoints"))
return;
// Now load our data in a similar way to how we saved it.
hitpoints = ES2.Load<int>(playerName + "hitpoints");
inventory = ES2.LoadList<int>(playerName + "inventory");
ES2.Load<Transform>(playerName + "transform", transform);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.