Hi.
I have some custom classes defined. These are defined by c-sharp files located under ‘Assets/scripts’.
My goal is to write an Editor tool which is able to create new nodes in the scene assembly and assigning specific components based on some of my custom classes.
Basically I have been trying to get this done using something like:
newNode = new GameObject("NewNode");
string myClassName = "CustomClass";
Type myScriptType = Type.GetType(myClassName);
newNode.AddComponent(myScriptType);
So, the objective is end up with a new node (named “NewNode”) with an added Component of type CustomClass.
However, the Editor tool is unable to locate the requested Type.
And yes, there is a c-sharp file named “CustomClass.cs” describing the class “CustomClass”.
The string parameter description states: “The assembly-qualified name of the type to get.”
The link has more info on what an assembly-qualified name is.
Mind you, there are always better ways than using strings. For example you could give all your components a base type and use UnityEditor.TypeCache to get them with one line of code.
The second option is no longer available. It has been deprecated.
That leaves me with the first option. However, in my script I only have a Class name (string).
To explain, the corresponding c-sharp code was generated and the Editor tool receives the class name from an import file.
So for the first option I need to get the corresponding Type using the class name.
As @jvo3dc already pointed out there’s an AddComponent version that takes a string argument. This was usually the easiest solution when you want to add a component based on a string value. However in recent Unity versions the method has been completely deprecated and does not work anymore.
As @spiney199 has pointed out and as you can read in the documentation, GetType without an assembly qualified name only searches in the current executing assembly. Since you do this in an editor script, the assembly in question is the editor assembly that gets created for your editor scripts. Your runtime scripts (unless you use an assembly definition file for a folder) live inside the Assembly-CSharp.dll assembly. So GetType should work when you do:
Of course, components could be defined in other assemblies. So you need to specify the exact name including the assembly and potential namespaces of course. Alternatively you can use the AppDomain class to get hold of all loaded assemblies, iterate through them and search for the type in each of them. Of course this has many potential issues as certain type names may appear several times, though usually in seperate namespaces. It’s also quite slow as you would iterate through literally thousands of types.
Unity now has the TypeCache class which provides some useful methods which simplify the search for a certain class. Unfortunately it doesn’t have a search method itself. You can do something like this:
foreach(var t in TypeCache.GetTypesDerivedFrom<Component>())
{
if(t.Name == "CustomClass")
{
// yey, found it
}
}
This is probably more performant than manually iterating through all assemblies.
The link gave me the information I needed to get this working! Thank you for that!
Under other circumstances I would not follow this route. However the string is the only thing I have to work with at that point. The corresponding c-sharp code was generated and the Editor tool receives the class name from an import file.