Search Unity

Best practices for in-game and editor to play audio files through custom Audio Manager

Discussion in 'Timeline' started by mgregoirelds, Sep 8, 2017.

  1. mgregoirelds

    mgregoirelds

    Joined:
    Feb 3, 2017
    Posts:
    22
    Hello, I just started using Timeline and so far, it is working great. I do have a problem however that I am pretty sure other people got, but I'm not sure that would be the best solution. In the game I am making, I created an AudioHandler singleton which is used to play BGM and SFX (all AudioSources are created from this singleton). As an example, when the singleton is created, I attach an AudioSource for BGM playing in the Awake method.

    I created a custom playable to call my PlayBGM method on my AudioHandler singleton and it is working great in-game, however when trying to preview the BGM in the Editor, it does not work. The BGM AudioSource is not created since Awake() is not called in Editor mode for my AudioHandler. What would be the best way for me to make it work and to be able to hear in Editor mode the selected BGM file through my AudioHandler ?

    Thanks!
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
  3. senkal_

    senkal_

    Joined:
    May 22, 2018
    Posts:
    86
    Hi all,
    Thank you seant_unity.
    I come across similar issue(calling some script on scene from custom playable in editor mode) and your suggestion works.
    Still have some things to figure it out but in general I made a big progress thanks to your advice.
    For example currently Awake is called multiple times on the same object, for example when I start the game.
    I guess at least one call is from playable and one when I play the game.
    I control it by some private bool property which doesn't allow to call Awake logic twice and more.

    Does the fact that script is called(awake and others) in editor as well, has any impact when the game is running, for example on performance?
     
    Last edited: Aug 23, 2018
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Not that I am aware of.

    For Awake being called twice, it should be called once in Editor, then once going into PlayMode (and again when going back to EditMode). If you are seeing different behaviour, then something isn't right.
     
  5. senkal_

    senkal_

    Joined:
    May 22, 2018
    Posts:
    86
    Yes, you are right.
    Still learning few bits here and there so I found the issues in my code. Good learning experience.
    Thank you for all the help.

    Sorry for late reply btw, will try to be more agile here.