2

In my game, there are tons of places where I need to play audios after functions complete, for example:

class Player{
    public AudioClip clip;
    void WalkToDestination()
    {
         //walk code here

         AudioManager.Play(clip);
    }
}

class GameManger{  
    public AudioClip clip;
    void AfterCompleteLevel()
    {
          //play level completion animation
           AudioManager.Play(clip);
    }
}

Since there are so many functions requiring playing sound, and every time I have to add public AduioSource clip into the class, and AudioManager.Play(clip); into the body, there are too much repetitive work. Is there any good design pattern for this?

1
  • 1
    This code should not even compile. I think you should modify it to reflect what you have now. Commented Oct 25, 2016 at 1:15

2 Answers 2

2

Define a base class, and put shared functions therein:

public class PlayerBase 
{
     protected SomeClass clip;
     public void PlaySound()
     {
         AudioManager.Play(clip);
     }
}

class Player : PlayerBase {}
class GameManger : PlayerBase {}

The call PlaySound() directly in the derived Player or GameManager class.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that is a solution, but i feel they are not that similar, the only similar behavior is playing music
2

What you're probably looking for is called a Singleton : they are very useful when using audio managers or such things.

You can declare one this way :

public class YourClassName: MonoBehaviour
{
    public static YourClassName instance;

    public void Awake()
    {
        if (!instance)
        {
            instance = this;
        }
    }

    public void YourMethodToCall()
    {
        //Do what you want here
    }
}

Then from another script you can access it using :

if (YourClassName.instance)
{
    YourClassName.instance.YourMethodToCall();
}

4 Comments

Instead of a singleton class it is better to have a "pre-load" scene and load the audio manager there. You drop the static instance and you instead use a Object.FindObjectOfType<YourClassName>();. See this answer for a full explination on how to do it.
@ScottChamberlain Agreed that this is another way to do so : I personally "always" use a kind of preload scene (usually the one containing custom splash screens, intro video, etc...) to set all my managers :)
The preload scene should not be a splash screen IMHO. You want it to quickly run even when debugging, if you make it your splash screen then you have to wait for the splash to load before debugging every time.
@ScottChamberlain That's why on every piece of script that performs unnecessary (for the game) long actions such as video playing, using a defined symbol like DEBUG allows for easy and fast debugging without having the pain to restore everything before final build ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.