Search Unity

Audio 2D Game

Discussion in '2D' started by dilmerval, Jan 8, 2014.

  1. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    Guys,

    So in my previous project I did all my game audio using Garage Band, I thought it came up pretty good, but one thing I hate the most is how the audio stops when you jump from one scene to the next one, I found a lot of articles about having a singleton audio source but having had any luck or probably need to try other ways. Have anyone done anything like that? To me audio is one of the key aspects of a game and I want a clean background music, fun music, and non-interruptive experience for my players.

    Dilmer
     
  2. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Well one way to deal with this is... how about you use only one scene for all of your levels? I still haven't decided for myself which way I want to go... either one scene per level, or one scene for all levels. There are pros/cons to each, mostly in regards to performance.
     
  3. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    hehe That's another decision I'm trying to make, right now I have a Menu Scene and a Game Scene, I do keep all my levels in the Game scene however when going from menu to game it stops the audio, I may end up just lowering the volume so it does some kind of a fade effect with the audio and then play another song in the game.
     
  4. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Actually you could possibly make your entire game (menu included) with just one scene.
     
  5. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    Yes I'm debating on that still, but I like the idea. Thanks
     
  6. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    When I was finding out about singletons there was this video showing how to use one for audio playing between levels.

    It used a don't destroy on load.

    I have just been searching for it to give you the link but I can't find it.
     
  7. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Heres what you are looking for:
    Code (csharp):
    1.  
    2. DontDestroyOnLoad(transform.gameObject);
    3.  
    Basically you pass whatever object you want to persist through a scene load to this function and it will not be destroyed.
     
  8. login4donald

    login4donald

    Joined:
    Jan 3, 2012
    Posts:
    462
    Create a single gameobject that handles the audio and allow it to persist scene loading via
    Code (csharp):
    1. DontDestroyOnLoad(this);
    and handle all the audio via scripting and if wanted you can lerp the audio volume for a smooth transition.
     
  9. login4donald

    login4donald

    Joined:
    Jan 3, 2012
    Posts:
    462
    If you do that I would recommend, the one scene route, that you have a switch statement controlling all the elements. I'm sure there's a better way but you could make a single gameobject, preserve that and control it runtime via script.
     
  10. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    Excellent guys thanks, ok sorry for asking so the DontDestroyOnLoad would work this way?

    In my Menu scene I have a Menu.cs file, based on what you said I will add this DontDestroyOnLoad(this); to my Awake() method? This would be the object containing my AudioSource correct? then from My second scene Game.cs how do I access my prev object? Would it still be available in my hierarchy? can I do something like GameObject.find("MyAudioObject")?

    Thanks!
     
  11. TaewYn

    TaewYn

    Joined:
    Jan 30, 2013
    Posts:
    19
    Yeah you could do it that way. A singleton would be easier to work with though:

    Code (csharp):
    1. public class SoundController : MonoBehaviour {
    2.  
    3.     public static SoundController Instance { get; private set; }
    4.  
    5.     void Awake(){
    6.         //make sure only one Instance exists
    7.         if (Instance != null  Instance != this)
    8.         {
    9.             Destroy(gameObject);
    10.         }
    11.         Instance = this;
    12.         DontDestroyOnLoad(this);
    13.     }
    14. .....
    15. }
    16.  
    You can access this from anywhere like this:

    Code (csharp):
    1. SoundController.Instance.Whatever();
     
  12. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    Excellent thank you very much looks straightforward.
     
  13. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    I'm curious though if line 9 (where you destroy an old instance) would serve to interrupt the music that is playing?
    @dilmer please let us know how it works out once you get it going :)
     
  14. TaewYn

    TaewYn

    Joined:
    Jan 30, 2013
    Posts:
    19
    Line 9 only get's called if you accidentally create a second SoundController, which should actually never happen.
    Even so, all it does is destroy the new Instance while the original is left untouched. The music shouldn't be interrupted.
     
  15. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    Absolutely I will let you know, this is in my backlog of stuff in trello.com to do :) FYI what I have done vs I have left for the game:

    $Trello.png
     
  16. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    I get very crazy with my stories in trello hehe
     
  17. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    LOL you are a very serious indie developer! Using Trello?! :)
     
  18. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    Love trello, I like simple, I've used various tools in the past and when it comes to tracking Trello works for me. :)