Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Singleton or observer which design pattern is good for audiomanagement

Discussion in 'Audio & Video' started by mfatihbarut, Jan 22, 2021.

  1. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,058
    Hi all,
    In a TD game. I have at most 30 monsters in a scene (same time)
    They have hurt, attack, and die sounds.
    Which is the best way to implement (performance-wise)
    Put an audiosource to all monster prefabs. Playoneshot them by a script.
    Or implement observer mode (learn it first :) and play only 1 (or 2-3) audiosource in the scene and playoneshot all audios whenever it is needed via that only source.
    Which is the right way to go?
     
  2. MrPaparoz

    MrPaparoz

    Joined:
    Apr 14, 2018
    Posts:
    157
  3. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,058
    thanks. the only thing in that way. 3D audio effect (far and near) makes me considered. The audio source will not be natural... (don't know that much difference for a mobile game)
     
  4. MrPaparoz

    MrPaparoz

    Joined:
    Apr 14, 2018
    Posts:
    157
    Well, you can create a AudioManager script that takes event data and act accordingly to source's distance to audio listener etc. It can save you from doing same thing again and again throughout different scripts.
     
  5. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,058
    thanks. Just because of curiosity. What is wrong with singleton? Memory leak or something like that?
     
  6. MrPaparoz

    MrPaparoz

    Joined:
    Apr 14, 2018
    Posts:
    157
    Nothing actually, it's an option. Personally, singleton becomes a tangled web of references after a while and it's hard to maintain if you're not careful. It becomes more apparent as project grows.
     
  7. yashubhati

    yashubhati

    Joined:
    Mar 28, 2017
    Posts:
    8
    Please go with observer pattern. :D