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);
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
.