-
-
Save jacobdufault/f29959427c5588762c4c to your computer and use it in GitHub Desktop.
Demo of EasySave2 integration into Full Inspector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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