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 )