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
0
Question by Paulo-Henrique025 · Feb 17, 2013 at 03:41 PM · c#editorreflection

Reflection only works in Play Mode?

Hi guys I'm trying to get some types here but the code aways return null inside my Editor Window but if I use the same piece of code on a monobehaviour while playing in unity the code just works. Any word on that?

The code:

 System.Type t = System.Type.GetType("NSNode")
 Debug.Log(t);

t is aways null while in the editor, and aways NSNode inside the 'game'.

Here is the full code with the Instancing method I'm trying to use:

 System.Type t = System.Type.GetType("NSNode")
 Debug.Log(t);
 NSNode n = (NSNode)System.Activator.CreateInstance(t);
 Debug.Log(n);
Comment
Add comment · Show 8
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 numberkruncher · Feb 17, 2013 at 03:55 PM 1
Share

Why not just use System.Type t = typeof(NSNode); ins$$anonymous$$d? On that subject, why not just instantiate the class in the usual way with new NSNode();? Editor scripts can access and use classes, structs, enums, etc from runtime scripts.

The reason that Type.GetType is not working likely means that you need to use the assembly qualified type name ins$$anonymous$$d.

avatar image Paulo-Henrique025 · Feb 17, 2013 at 04:17 PM 0
Share

It's just an example, I actually don't know the type of class that will come, but they will all derive from NSNode through inheritance.

avatar image numberkruncher · Feb 17, 2013 at 04:25 PM 0
Share

In that case you should be using assembly qualified names, then System.Type.GetType will return the correct type (provided that the assembly is in fact loaded!)

avatar image Paulo-Henrique025 · Feb 17, 2013 at 04:28 PM 0
Share

That "provided that the assembly is loaded" is my worst fear! How can I get the qualified with just a string?

avatar image numberkruncher · Feb 17, 2013 at 04:56 PM 1
Share

Okay, so in that case the assembly will be loaded and accessible by editor scripts. I do a similar thing with my scripts and experience no issues whatsoever. Try using System.Type.GetType("NSNode,Assembly-CSharp") and let me know how that goes.

Show more comments

1 Reply

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

Answer by numberkruncher · Feb 17, 2013 at 10:13 PM

To access information for a specific type that is in a different assembly you have two choices:

Choice #1 - Use an assembly qualified name

This can be achieved with and without namespaces:

 // Without a namespace
 System.Type nodeType = System.Type.GetType("NSNode,Assembly-CSharp");

 // With a namespace
 System.Type nodeType = System.Type.GetType("Some.Namespace.NSNode,Assembly-CSharp");

As one might guess, the runtime assembly for UnityScripts is "Assembly-UnityScript".

Choice #2 - Get access to the assembly through a known class

Let's suppose that you have a class called "SomeRuntimeClass" which exists in the runtime assembly. You can then access any other types in that assembly without needing assembly qualified lookups:

 // Start of script, keep local copy of assembly
 private Assembly runtimeAssembly = typeof(SomeRuntimeClass).Assembly;
 // OR
 private Assembly runtimeAssembly = Assembly.Load("Assembly-CSharp");

 ...

 // Lookup class from assembly by name alone:
 System.Type nodeType = runtimeAssembly.GetType("NSNode");

And then use the Activator class to create an instance of nodeType.

Comment
Add comment · Show 13 · 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 Paulo-Henrique025 · Feb 17, 2013 at 10:21 PM 0
Share

@Lea is there a way to instantiate from a string from another assembly without knowing the qualified name? I mean, having just the name "ClassX" is possible to find the the qualified name? I don't think so but a pro must confirm.

avatar image numberkruncher · Feb 17, 2013 at 10:31 PM 1
Share

If you know which assembly then you can just use the second option above where you only need to specify the name "ClassX". If the assembly is unknown then you can enumerate all assemblies to search for the type, though this is relatively slow (so you might need to cache lookups to improve performance using a Dictionary). Also you run the risk at instantiating objects that you don't know about...

avatar image numberkruncher · Feb 17, 2013 at 10:34 PM 1
Share

For best performance you need one known, be it an assembly qualified type name, or a reference to the assembly.

avatar image numberkruncher · Feb 17, 2013 at 10:38 PM 1
Share

Have you considered using an object factory and creator design pattern ins$$anonymous$$d of reflection? When one turns to reflection it is usually because there was no other better solution. If possible reflection should be avoided because it is like black magic.

avatar image Bunny83 · Feb 18, 2013 at 12:12 AM 2
Share

A quite similar question has been asked just yesterday.

Just read the docs on GetType carefully. The string parameter description explains everything. You need an assembly qualified type string if the class you want to access is not part of the current assembly or the mscorelib. I've posted an extension method on the other question which will return a list of all classes which are derived from the given type.

So in your case something like that:

     var types = typeof(NSNode).GetAllDerivedClasses();

This will list all classes from all loaded assemblies which are derived from NSNode.

Show more comments

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

reflection propertyinfo.getvalue compiles fine but gives erros in editor 1 Answer

how to save instance of class trough editor 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to open an Editor Window by using the MethodInfo.Invoke function ? 1 Answer


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