Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges