• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Thomas Hentschel Lund · Aug 01, 2010 at 09:40 AM · dllreflection

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Thomas Hentschel Lund · Aug 01, 2010 at 10:14 AM 0
Share

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

avatar image Lucas Meijer 1 ♦♦ · Oct 06, 2010 at 09:27 PM 0
Share

Try iterating over the current app domain's loaded assembly to get at the assembly, ins$$anonymous$$d of using LoadFrom.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Thomas Hentschel Lund · Aug 03, 2010 at 07:29 AM 0
Share

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.

avatar image Lucas Meijer 1 ♦♦ · Aug 04, 2010 at 04:34 PM 0
Share

fixed post to make the code work.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Set Reflected assembly name after dynamically Loading Dll 0 Answers

Where does 'extern' point in UnityEngine.dll methods? 1 Answer

Call c# script function from managed c# script in dll 1 Answer

Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers

Where can i find the unityengine xml? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges