Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created December 7, 2015 20:25
Show Gist options
  • Select an option

  • Save selfsame/0e031ffeb5e4a4fc5986 to your computer and use it in GitHub Desktop.

Select an option

Save selfsame/0e031ffeb5e4a4fc5986 to your computer and use it in GitHub Desktop.
ArcadiaComponents.cs
using UnityEngine;
using clojure.lang;
public class ArcadiaBehaviour : MonoBehaviour, ISerializationCallbackReceiver
{
private IFn _fn;
public IFn fn
{
get { return _fn; }
set
{
_fn = value;
serializedVar = null;
OnBeforeSerialize();
}
}
[SerializeField]
public string serializedVar;
// if fn is a var, store in serializedVar
public void OnBeforeSerialize()
{
Var v = fn as Var;
if(v != null)
{
serializedVar = v.Namespace.Name + "/" + v.Symbol.Name;
}
}
// if serializedVar not null, set fn to var
public void OnAfterDeserialize()
{
if(serializedVar != "")
{
Symbol sym = Symbol.intern(serializedVar);
try {
RT.var("clojure.core", "require").invoke(Symbol.intern(sym.Namespace));
fn = RT.var(sym.Namespace, sym.Name);
}
catch {
try {
string libName = sym.Namespace.Replace(".", "/").Replace("-", "_");
Debug.Log("Loading " + libName);
RT.load(libName);
fn = Var.intern(Symbol.intern(sym.Namespace), Symbol.intern(sym.Name));
}
catch {
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment