Day Three

Another quick one - today I made the volume sliders have audio previews. It’s actually a really simple thing to do, Unity has great support for this sort of thing. I use a couple of interfaces - ISelectHandler and IDeselectHandler. Here’s the code, it’s incredibly straightforward:

public class AudioPreview : MonoBehaviour, ISelectHandler, IDeselectHandler
{
    [SerializeField] private AudioSource source;
    
    public void OnSelect(BaseEventData _)
    {
        source.Play();
    }

    public void OnDeselect(BaseEventData _)
    {
        source.Stop();
    }
}

ISelectHandler and IDeselectHandler are used on MonoBehaviours attached to Selectable GameObjects in your UI (e.g. Buttons, Sliders, etc) when that GameObject gets selected/deselected. It’s nice and straightforward - when the volume slider is selected, start playing the audio source. When it’s deselected, stop! Small classes like this make code incredibly reusable.

There are a bunch of other UI Interfaces like this: IPointerEnter, IPointerExit, IPointerClick, IPointerUp, IPointerDown, IDragStart, IDragEnd, and IDrag. They all follow the same pattern, e.g. IPointerEnter needs a method OnPointerEnter (BaseEventData data).

What’s next?

I’ll be done with UI stuff eventually, I swear 😂 Saturday’s job is finishing off the accessibility stuff.


I hope you enjoyed reading about my Polyfury progress. If you have any questions or thoughts, please feel free to leave a comment below. Thanks for reading, and maybe check out the demo:

Making your own games? Check out Artificer Pro - released this month!

Previous
Previous

Day Four

Next
Next

Day Two