- Home /
Find all components in project
Hello,
I'm writing an editor extension that requires selecting component names out of a list of all available components in the project. Similar to how the "Add Component" dropdown works.
Is there a way to get all the available components in the project? I've tried using AssetDatabase but that doesn't cover built in components like Rigidbody.
Well... it's probably not what you want to hear, but you may want to look into Reflection. Otherwise you'll be brute-forcing it and hoping you didn't forget something. However if you're only looking for certain stuff, you can do it that way. Like..
//Asset database crap
if (myAsset.GetComponent<Rigidbody>())
{
rigidbodies.Add(myAsset.GetComponent<Rigidbody>());
}
if (myAsset.GetComponent<$$anonymous$$eshCollider>())
{
meshColliders.Add(myAsset.GetComponent<Rigidbody>());
}
//etc (obviously this kind of brute force is VERY inefficient)
Although thinking about it you could create a database that scours the assets, and adds a EDITOR ONLY component to every asset that contains an ID and relevant info, you'd just need to make sure to add a Reset function to piggyback off the Reset event of monobehaviours for whenever a new component is added.
Answer by nice_shot · Dec 18, 2018 at 06:50 AM
Indeed it looks like the best way to find all components is using reflections. Here is a snippet that shows how to do this:
var componentTypes = System.AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => type.IsClass && type.IsSubclassOf(typeof(Component)))
;
foreach (var type in componentTypes) {
Debug.Log(type.FullName);
}
Glad to hear you figured it out, if you are doing this dynamically though you may still want to use the internal Reset function for monobehaviours so that you can catch and record when new scripts are added. However if you're acquiring all components every time you need them, then I wouldn't bother with Reset.