I am looping through, and evaluating all the components on GameObjects and I need to check to see if a component was added or dependent on [RequireComponen (typeof (Type))] from another component. I assume I will be needing to use some reflection to get at the attribute.
There is 2 ways I see this happening. Check if the current component I am evaluating is required by another, or (less preferable, but understandably the more likely answer) check if the current component I am evaluating is requiring another component, and building a dependency list.
Why not just create a simple ComponentManager that keeps track of all your created components and what happens to them?
Because they are not Components within my control. Either from other team members, or even some Unity made components use the require.
Sadly, I had to settle for the latter, but here is the code to check for required scripts
void CheckRequired(Type myType) {
Attribute[] attributes = Attribute.GetCustomAttributes(myType);
foreach (Attribute attribute in attributes)
{
if (attribute is RequireComponent)
{
RequireComponent req = (RequireComponent)attribute;
Debug.Log(req.m_Type0+" is required by "+myType);
RegisterRequired(myType, req.m_Type0);
}
}
}
1 Like