Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Foreach value in a Class do a new Foreach.

Discussion in 'Scripting' started by Gapa, Sep 15, 2018.

  1. Gapa

    Gapa

    Joined:
    Dec 11, 2012
    Posts:
    26
    so I have been looking around and don't seem to be able to find the answer to this.

    if you got a class01 and add that in to a class02 as a Array.
    and want to in a for-loop/foreach to go through all the arrays in class02
    and do a foreach on every array found.

    Code (CSharp):
    1.  
    2. public class Sound{
    3.     public string name;
    4.     public AudioClip clip;
    5. }
    6. public class SoundList{
    7.     public Sound[]player;
    8.     public Sound[]music;
    9.     public Sound[]enemy;
    10. }
    11.  
    12. public SoundList soundL;
    13. public List<Sound> allSounds;
    14.  
    15.    foreach (Array x in soundL)
    16.     {
    17.         foreach (Sound i in x)
    18.         {
    19.            allSounds.add(i);
    20.         }
    21.     }
    22.  
    23.  
    anyone know if that is possible?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You could technically do it through reflection, but that over-complicates things significantly. Instead of using Add, use AddRange to add the entire array to your allSounds list.

    Code (csharp):
    1.  
    2. allSounds.AddRange(soundL.player);
    3. allSounds.AddRange(soundL.music);
    4. allSounds.AddRange(soundL.enemy);
    5.  
     
    Gapa likes this.
  3. Gapa

    Gapa

    Joined:
    Dec 11, 2012
    Posts:
    26
    Thanks for the quick reply and that works.
    but I would love to only have to add a new Sound[] under SoundList

    Eks.
    Code (CSharp):
    1. public class SoundList{
    2.     public Sound[]player;
    3.     public Sound[]music;
    4.     public Sound[]enemy;
    5. }
    6. //later
    7. public class SoundList{
    8.     public Sound[]player;
    9.     public Sound[]music;
    10.     public Sound[]enemy;
    11.     public Sound[]enemy02;
    12.     public Sound[]enemy03;
    13. }
    any ideas?
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    There's no real way for code to just magically know about other / new / different code. The best you can get is to use reflection to inspect the object for fields that match your criteria and dynamically bind to them. These articles will get you started:
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection
    https://docs.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/viewing-type-information

    You also may be able to leverage Unity's editor functionality to inspect your project for all sound files and create an array from that, using the AssetDatabase -- https://docs.unity3d.com/ScriptReference/AssetDatabase.html -- but that will require some manual actions on your part still.
     
  5. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    Would something like this work?

    Code (CSharp):
    1. public class SoundList : IEnumerable
    2. {
    3.     public Sound[] player;
    4.     public Sound[] music;
    5.     public Sound[] enemy;
    6.  
    7.     private IEnumerator <Sound[]> SoundArrays()
    8.     {
    9.         yield return player;
    10.         yield return music;
    11.         yield return enemy;
    12.     }
    13.  
    14.     public IEnumerator<Sound> GetEnumerator()
    15.     {
    16.         using (var arrayEnumerator = SoundArrays())
    17.         {
    18.             while (arrayEnumerator.MoveNext())
    19.             {
    20.                 foreach (var sound in arrayEnumerator.Current)
    21.                 {
    22.                     yield return sound;
    23.                 }
    24.             }
    25.         }
    26.     }
    27.  
    28.     IEnumerator IEnumerable.GetEnumerator()
    29.     {
    30.         return GetEnumerator();
    31.     }
    32. }
    33.  
    34. foreach(var sound in soundList)
    35.     // do something with 'sound'
     
  6. Gapa

    Gapa

    Joined:
    Dec 11, 2012
    Posts:
    26
    Thanks alot for you time and help GroZZleR bet it was more if later i want to add new "sources of sound"
    but i will look in to it to links.


    Hey chatrat12 thanks for you time and help but i cant see to get it to work i will try some more and see if i can get it to work.