Compilation Order Quandaries - Cross Language Game-To-Library Communication

I have a library of JS scripts which need to be included in Unity’s main compilation pass, so they are stored under the /Extensions folder. (I already spent a day trying to get the library running smoothly in a higher compilation pass, but between AssetBundles referencing scripts in the library and other arcane dependencies, it just isn’t working out…)

I am now rewriting my game in C#, and a fair amount of functions and members of the library need to be accessible to my game. Note: in case this makes any difference, I have already redesigned the library so that everything which the game needs to access is contained within a couple Singleton monobehaviours which will always be present (their GameObject is DontDestroyOnLoad.

What is the best way to enable Game > Library function calls and Game < Library state variable access? I am planning to set up an “interface” script for my library in the /Plugins folder, which will be used for all communication between my game and the library. My options from there seem to be threefold:

(1) Store a static reference to each singleton monobehaviour in the Interface script. If this is possible, it would be awesome! I could work with the library directly from my game. Haven’t yet figured out how to overcome the compilation pass barrier and achieve this.

(2) Use some form of reflection in the interface script to act as a go-between for each function and state variable in the library. I’ve been able to find a couple examples of reflection (such as here: http://forum.unity3d.com/threads/26762-Dynamic-typing-in-C?p=176056&viewfull=1#post176056 ), but am still not totally familiar with what it is capable of. (Would Eval() be of any help?)

(3) Use a bunch of SendMessage calls in the interface script to relay to functions calls in the library. Not sure how to read state variables back from the library. I have heard people speak of passing object references with SendMessage, but can’t find many examples of this. Could a passed ObjectReference overcome the compilation pass barrier? (This exact question is posed - but never answered - in the bottom of this thread: http://forum.unity3d.com/threads/13858-Combining-Javascript-with-C )

The more I think about this (it can really help to write out the problems you are facing, then come back a day later and analyze them afresh), it seems that I can solve everything with Occam’s razor. Instead of trying to use advanced coding techniques to reference state variables from inside the library using my interface script, I should just move all state variables into the interface script to start with! The library can update them there as necessary, and the rest of my game will be able to read them effortlessly as well. I’ll use SendMessage to forward requisite interface calls to internal parts of the library as necessary, and live happily ever after.

If anyone would like to chime in with some other ways I could have accomplished this using more advanced coding techniques, I would love to hear them. If noone does, I’ll mark this as answered in a couple days.

Regardless, this answers system forced me to write down my problem as clearly as I could, without which prompting I know not how long it would have taken me to perceive the obvious. For this, I am truly thankful.