- Home /
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.
I've managed to get this partly to work by specifying a full path to a UnityEngine.dll copy. But this wont work at runtime in a real life scenario
Try iterating over the current app domain's loaded assembly to get at the assembly, ins$$anonymous$$d of using LoadFrom.
Answer by Lucas Meijer 1 · Aug 01, 2010 at 09:59 PM
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)
Thanks Lucas.
Unfortunately it doesnt seem to work in neither editor or standalone.
System.Type type = System.Type.GetType("UnityEngine.Application"); if (type == null) { Console.WriteLine("Assembly not found!!!!"); }
Print Assembly not found in both situations.
Your answer
Follow this Question
Related Questions
Where can i find the unityengine xml? 0 Answers
MonoBehaviour from DLL in AssetBundle Reflection problem 3 Answers
Batchmode using a DLL that references UnityEngine.dll and UnityEditor.dll crashes 0 Answers
Assembly.Load on iOS 1 Answer
Set Reflected assembly name after dynamically Loading Dll 0 Answers