Skip to content

Instantly share code, notes, and snippets.

@jacobdufault
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobdufault/51a0525d15f12f633346 to your computer and use it in GitHub Desktop.
Save jacobdufault/51a0525d15f12f633346 to your computer and use it in GitHub Desktop.
Sample of how to serialize objects with Full Inspector
using FullInspector.Serializers.ProtobufNet;
using ProtoBuf;
using UnityEngine;
namespace FullInspector.Samples {
[ProtoContract]
public struct Struct {
[ProtoMember(1)]
public int A;
[ProtoMember(2)]
public string B;
[ProtoMember(3)]
public GameObject GameObject;
[ProtoMember(4)]
public Ref<Transform> ComponentReference;
}
[ProtoContract]
public class ProtobufNetSample : BaseBehavior<ProtobufNetSerializer> {
[ProtoMember(1)]
public Struct Value;
}
}
using FullInspector.Serializers.JsonNet;
using Newtonsoft.Json;
using UnityEngine;
namespace FullInspector.Samples {
[JsonObject(MemberSerialization.OptIn)]
public class JsonNetSample : BaseBehavior<JsonNetSerializer> {
[JsonProperty]
public IInterface MyInterface;
[JsonProperty]
public Vector3 SomeVector;
}
[JsonObject(MemberSerialization.OptIn)]
public interface IInterface {
void Act();
}
[JsonObject(MemberSerialization.OptIn)]
public class Derived1 : IInterface {
[JsonProperty]
public int A;
public void Act() {
Debug.Log("Derived1 says hi with A = " + A);
}
}
[JsonObject(MemberSerialization.OptIn)]
public class Derived2 : IInterface {
[JsonProperty]
public string B;
public void Act() {
Debug.Log("Derived2 says hi with B = " + B);
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using FullInspector.Serializers.EasySave2;
namespace FullInspector.Samples {
public class EasySave2Sample : 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