Jump to content

Mod Loader/Reference/Purchase sound API

From Threadbox

The shop plays one sound after a successful fabric or accessory purchase and another when the player cannot afford it. This API changes those two sound banks; it does not change the soundtrack.

Build an audio AssetBundle containing your purchase sound. Name the bundle sounds and the clip Buy to use it with this example.

Add a sound

The example adds your sound to the successful-purchase sounds, then hides one of the originals. WhenReady waits until the shop sound controller exists before making those changes.

Add references to UnityEngine.AudioModule.dll and UnityEngine.AssetBundleModule.dll from Dressmaker_Data/Managed, alongside the assemblies in the C# starter.

using System;
using System.IO;
using BepInEx;
using DressmakerMods;
using UnityEngine;

[BepInPlugin("example.shop-audio", "Shop Audio", "1.0.0")]
[BepInDependency("yuu.dressmaker.mods", "0.6.71")]
public sealed class ShopAudio : BaseUnityPlugin
{
    private IDisposable ready;
    private IDisposable hidden;
    private IDisposable added;
    private AssetBundle bundle;

    private void Start()
    {
        bundle = AssetBundle.LoadFromFile(
            Path.Combine(Paths.PluginPath, "ShopAudio", "sounds"));
        AudioClip clip = bundle?.LoadAsset<AudioClip>("Buy");
        if (clip == null)
        {
            Logger.LogError("Could not load Buy from ShopAudio/sounds.");
            return;
        }
        ready = PurchaseSoundModApi.WhenReady(() =>
        {
            added = PurchaseSoundModApi.AddClip(PurchaseSound.Success, "example.shop-audio.buy", clip);
            AudioClip[] originals = PurchaseSoundModApi.GetOriginalClips(PurchaseSound.Success);
            if (originals.Length > 0)
                hidden = PurchaseSoundModApi.SuppressClip(PurchaseSound.Success, originals[0]);

        });
    }

    private void OnDestroy()
    {
        ready?.Dispose();
        added?.Dispose();
        hidden?.Dispose();
        bundle?.Unload(false);
    }
}

Methods and values

PurchaseSound.Success
Sound played when the purchase succeeds.
PurchaseSound.NotEnoughGold
Sound played when the player cannot afford the purchase.
bool IsReady
True after the sound controller is ready.
IDisposable WhenReady(Action callback)
Runs the callback when the sound controller is ready, or immediately if it already is. Dispose to cancel a callback that has not run yet.
AudioClip[] GetOriginalClips(PurchaseSound sound)
Returns a copy of the game's original clips for that outcome.
AudioClip[] GetClips(PurchaseSound sound)
Returns a copy of the clips currently available for that outcome.
IDisposable AddClip(PurchaseSound sound, string id, AudioClip clip)
Adds a clip to the bank. IDs must be unique across loaded plugins. Dispose the handle to remove it.
bool RemoveClip(PurchaseSound sound, string id)
Removes a clip registered through this API. Returns false if the ID is unknown.
IDisposable SuppressClip(PurchaseSound sound, AudioClip clip)
Hides a clip until the handle is disposed. Keep one handle for each original clip you suppress.

If every clip in a bank is suppressed, that purchase outcome is silent. Keep custom clips and their AssetBundle loaded while registered.

Back to the Mod Loader guide