Accessing UnityEngine properties from standalone dll - running in a Unity game

I am working on the next SmartFoxServer 2X API. It is a standalone dll that sometimes will be running inside Unity - and sometimes outside in other environment (Windows Forms apps, console unit tests etc).

Part of the handshake with the server is to send environment and version information. Those are accessible in Application.platform and Application.unityVersion.

My question is now how to dynamically access the UnityEngine.Application assembly - if present.

I tried adding some reflection code, but it fails to execute:

try {
    Type type = System.Reflection.Assembly.LoadFrom("UnityEngine.dll").GetType("UnityEngine.Application");
    PropertyInfo platformInfo = type.GetProperty("platform");
    PropertyInfo versionInfo = type.GetProperty("unityVersion");

    object instance = Activator.CreateInstance(type);
    playerType = (string)platformInfo.GetValue(instance, null);
    version = (string)versionInfo.GetValue(instance, null);

} catch (Exception e) {
    playerType = "Non-Unity";
    version = "Unknown";
}

When run inside a Unity game this code fails with:

at <0x00000> at (wrapper managed-to-native) System.Reflection.Assembly:LoadFrom (string,bool) at System.Reflection.Assembly.LoadFrom (System.String assemblyFile) [0x00000] at Sfs2X.Requests.HandshakeRequest..ctor (System.String apiVersion, System.String reconnectionToken) [0x00000]

So the UnityEngine assembly is not found.

Anyone have a clue on how to do this?

P.S. I know its not possible to embed UnityEngine outside the engine itself. This situation is the other way around. I am running the dll inside the engine, so theoretically should have access to these assemblies.

Try:

 var mytype = Type.GetType("UnityEngine.Application, UnityEngine");

(1st part is the typename, 2nd part is the name of the assembly where to look for it).

From there on you can use reflection to do the rest. It seems your reflection code is incorrect though, you're trying to create a new instance, while the properties you want to get are static ones. So no need to create an instance. Just call a straight GetValue(null,null)