Skip to content
Jeppe Zapp edited this page May 27, 2014 · 9 revisions

Plugins are necessary for serialising your classes exactly the way you want. As OpenFile employs the JSONObject class, that's mainly what you will be using.

UnityScript

public class SerializeMyClass : OFPlugin {
	override public function get types () : System.Type [] {
		return [ typeof ( MyClass ) ];
	}

	// Find the object by GUID after all objects have been instantiated
	private function ConnectObject ( MyClass myClass, string id ) : IEnumerator {
		yield WaitForEndOfFrame ();
		
		var so : OFSerializedObject = OFDeserializer.FindObject ( id );
		
		if ( so ) {
			myClass.myObject = so.gameObject;
		}
	}

	// Deserialize the class from JSON
	override public function Deserialize ( input : JSONObject, output : Component ) {
		myClass : MyClass = output as MyClass;
	
		myClass.myInt = (int) input.GetField ( "myInt" ).n;
		myClass.myFloat = input.GetField ( "myFloat" ).n;
		myClass.myBool = input.GetField ( "myBool" ).b;
		myClass.myString = input.GetField ( "myString" ).str;
		OFDeserializer.planner.DeferConnection ( ConnectObject ( myClass, input.GetField ( "myObject" ).str ) );
	}
	
	// Serialize the class to JSON
	override public function Serialize ( input : Component ) : JSONObject {
		var output : JSONObject = new JSONObject ( JSONObject.Type.OBJECT );
		var myClass : MyClass = input as MyClass;

		output.AddField ( "myInt", myClass.myInt );
		output.AddField ( "myFloat", myClass.myFloat );
		output.AddField ( "myBool", myClass.myBool );
		output.AddField ( "myObject", myClass.myObject.GetComponent.< OFSerializedObject > ().id );
		output.AddField ( "myString", myClass.myString );

		return output;
	}
}

C-sharp

using UnityEngine;
using System.Collections;

public class SerializeMyClass : OFPlugin {
	override public System.Type[] types {
		get { return new System.Type [] { typeof ( MyClass ) }; }
	}

	// Find the object by GUID after all objects have been instantiated
	private IEnumerator ConnectObject ( MyClass myClass, string id ) {
		yield return new WaitForEndOfFrame ();
		
		OFSerializedObject so = OFDeserializer.FindObject ( id );
		
		if ( so ) {
			myClass.myObject = so.gameObject;
		}
	}

	// Deserialize the class from JSON
	override public void Deserialize ( JSONObject input, Component output ) {
		MyClass myClass = output as MyClass;
	
		myClass.myInt = (int) input.GetField ( "myInt" ).n;
		myClass.myFloat = input.GetField ( "myFloat" ).n;
		myClass.myBool = input.GetField ( "myBool" ).b;
		myClass.myString = input.GetField ( "myString" ).str;
		OFDeserializer.planner.DeferConnection ( ConnectObject ( myClass, input.GetField ( "myObject" ).str ) );
	}
	
	// Serialize the class to JSON
	override public JSONObject Serialize ( Component input ) {
		JSONObject output = new JSONObject ( JSONObject.Type.OBJECT );
		MyClass myClass = input as MyClass;

		output.AddField ( "myInt", myClass.myInt );
		output.AddField ( "myFloat", myClass.myFloat );
		output.AddField ( "myBool", myClass.myBool );
		output.AddField ( "myObject", myClass.myObject.GetComponent < OFSerializedObject > ().id );
		output.AddField ( "myString", myClass.myString );

		return output;
	}
}

Clone this wiki locally