Search Unity

List of all components as their end type

Discussion in 'Scripting' started by VengadoraVG, Nov 14, 2019.

  1. VengadoraVG

    VengadoraVG

    Joined:
    Nov 21, 2015
    Posts:
    13
    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).

    I tried doing this:

    Code (CSharp):
    1.         Behaviour[] allBehaviours = transform.GetComponentsInChildren<Behaviour>();
    2.  
    3.         foreach (Behaviour behaviour in allBehaviours) {
    4.             Undo.RecordObject(behaviour, "mary sue");
    5.         }
    But it won't work, because the Undo is recording the behaviour as Behaviour, ignoring all his properties as implementation.

    What can I do? :(
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Idk, maybe passing it as serialized object instance or it's properties individually will work?
     
  3. Neriad

    Neriad

    Joined:
    Feb 12, 2016
    Posts:
    125
    As a quick fix you can use "is" checks like that :
    Code (CSharp):
    1.     void SomeFunction ()
    2.     {
    3.         foreach (Behaviour b in allBehaviours)
    4.         {
    5.             if (b is TypeA)
    6.                 SomeCode();
    7.             else if (b is TypeB)
    8.                 SomeCode();
    9.             else if (b is TypeC)
    10.                 SomeCode();
    11.         }
    12.     }
    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.
     
  4. VengadoraVG

    VengadoraVG

    Joined:
    Nov 21, 2015
    Posts:
    13
    that would work, but I'd have to make one if for every class that inherits from Behaviour... it would be a very long list n.n'
     
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Maybe an interface or attribute will help?
    With interface, your can just
    Code (CSharp):
    1. foreach (IMyContract b in allBehaviours)
    2. {
    3.     b.SomeMethod();
    4. }
    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
    Code (CSharp):
    1. public CustomProcessingAttribute : Attribute {
    2. }
    3.  
    4. [CustomProcessing(Processing.DoWhatever)]
    5. class SomeBehaviour : MonoBehaviour
    6. {
    7. }
    8.  
    9. foreach (Behaviour b in allBehaviours)
    10. {
    11.    var attribute = b.GetType().GetAttribute<CustomProcessingAttribute>();
    12.    attribute.Process();
    13.   // or
    14.   switch(attribute.ProcessingType)
    15. {
    16. case 1:
    17. dosomth(b);
    18. break;
    19. }
    20. }
    If you'd like to adopt the attribute solution, read this https://www.infoworld.com/article/3006630/how-to-work-with-attributes-in-c.html
    My pseudocode won't even compile, it's here just to show the concept.