So I’m trying to do a Mary Sue for Undo.RecordObject function… basically, something that records recursively all the behaviours in a game object (please don’t ask why).
As a quick fix you can use “is” checks like that :
void SomeFunction ()
{
foreach (Behaviour b in allBehaviours)
{
if (b is TypeA)
SomeCode();
else if (b is TypeB)
SomeCode();
else if (b is TypeC)
SomeCode();
}
}
Not sure this is what you are looking for, I don’t think this kind of code is good practice, but this could be a solution.
Maybe an interface or attribute will help?
With interface, your can just
foreach (IMyContract b in allBehaviours)
{
b.SomeMethod();
}
with attributes it’s a little harder, but attributes allows you not to move code from editor class to beahviours and this make things easier with builds and such
public CustomProcessingAttribute : Attribute {
}
[CustomProcessing(Processing.DoWhatever)]
class SomeBehaviour : MonoBehaviour
{
}
foreach (Behaviour b in allBehaviours)
{
var attribute = b.GetType().GetAttribute<CustomProcessingAttribute>();
attribute.Process();
// or
switch(attribute.ProcessingType)
{
case 1:
dosomth(b);
break;
}
}