Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering a Rive file with sound effects in Unity does not play the effects #61

Open
trdroid opened this issue Jul 16, 2024 · 8 comments

Comments

@trdroid
Copy link

trdroid commented Jul 16, 2024

Hi,

I rendered a Rive file with sound effects in Unity and noticed that the sound effects are not played. I was wondering if this would be supported in the future.

Thank you.

@trdroid trdroid changed the title Rendering a Rive file with sound effects in Unity does not play the sound Rendering a Rive file with sound effects in Unity does not play the effects Jul 16, 2024
@damzobridge
Copy link
Contributor

This should be supported already. Could you share an example of the script you're using to display the Rive file?

@trdroid
Copy link
Author

trdroid commented Jul 28, 2024

Hi @damzobridge,

Here are the steps I've followed:

  1. Downloaded a Rive file that plays Audio right away; I've used this file: https://rive.app/community/files/11056-21179-jet-plane/
  2. Duplicated the DrawToCameraScene from the getting-started Unity Project from the Rive-Unity Examples Repo. I named the scene DrawToCameraSceneARiveAudioFile, which can be found in the attached zip file
  3. On hitting Play in the Unity Editor, I do not hear the Audio from the Rive asset but I just see the Rive Animation played in the Unity Editor

getting-started.zip

@damzobridge
Copy link
Contributor

damzobridge commented Jul 29, 2024

Thanks for sending over the project. It looks like the example projects are using an older version of the RiveScreen script. You'll need to update yourRiveScreen script to include the audio initialization code:

private Rive.AudioEngine m_audioEngine;

void OnAudioFilterRead(float[] data, int channels)
{
        if (m_audioEngine == null)
        {
            return;
        }
        m_audioEngine.Sum(data, channels);

}
    
private void Awake()
    {

        if (asset != null)
        {
            m_file = Rive.File.Load(asset);
            m_artboard = m_file.Artboard(0);
            m_stateMachine = m_artboard?.StateMachine();
            int channelCount = 1;
            switch (AudioSettings.speakerMode)
            {
                case AudioSpeakerMode.Mono:
                    channelCount = 1;
                    break;
                case AudioSpeakerMode.Stereo:
                    channelCount = 2;
                    break;
                case AudioSpeakerMode.Quad:
                    channelCount = 4;
                    break;
                case AudioSpeakerMode.Surround:
                    channelCount = 5;
                    break;
                case AudioSpeakerMode.Mode5point1:
                    channelCount = 6;
                    break;
                case AudioSpeakerMode.Mode7point1:
                    channelCount = 8;
                    break;
                case AudioSpeakerMode.Prologic:
                    channelCount = 2;
                    break;
            }

            m_audioEngine = Rive.AudioEngine.Make(channelCount, AudioSettings.outputSampleRate);
            m_artboard.SetAudioEngine(m_audioEngine);

        }

        Camera camera = gameObject.GetComponent<Camera>();
        Assert.IsNotNull(camera, "RiveScreen must be attached to a camera.");

        // Make a RenderQueue that doesn't have a backing texture and does not
        // clear the target (we'll be drawing on top of it).
        m_renderQueue = new Rive.RenderQueue(null, false);
        m_riveRenderer = m_renderQueue.Renderer();
        m_commandBuffer = m_riveRenderer.ToCommandBuffer();

        if (!Rive.RenderQueue.supportsDrawingToScreen())
        {
            m_helper = new CameraTextureHelper(camera, m_renderQueue);
            m_commandBuffer.SetRenderTarget(m_helper.renderTexture);
        }
        camera.AddCommandBuffer(cameraEvent, m_commandBuffer);

        DrawRive(m_renderQueue);
    }

We'll update the example projects soon to include audio support.

@trdroid
Copy link
Author

trdroid commented Jul 30, 2024

I've updated the RiveScreen.cs with the aforementioned changes and I am able to hear the audio now. Thank you @damzobridge

@trdroid trdroid closed this as completed Jul 30, 2024
@trdroid trdroid reopened this Jul 30, 2024
@trdroid
Copy link
Author

trdroid commented Jul 30, 2024

Hi @damzobridge, with the aforementioned update, I am able to hear the audio when played in the Unity Editor but when I built the same to WebGL, the audio in the Rive file is not played; I even tried clicking in the browser once but that did not play the audio either.

@damzobridge
Copy link
Contributor

Thanks, you're right, audio currently works on the other platforms, but not webGL. We're looking into the issue now.

In the meantime, I recommend using Rive Events, that way you can trigger audio natively in Unity in response.

@damzobridge
Copy link
Contributor

@trdroid This is happening because Unity doesn't support OnAudioFilterRead on WebGL which we rely on at the moment. We're looking into ways around this. As mentioned, using Rive events would let you integrate your audio more natively using AudioClips and other audio-related plugins you might be using already.

@trdroid
Copy link
Author

trdroid commented Jul 31, 2024

Thank you @damzobridge for specifying the reason as to why it's currently broken in WebGL. Like you've mentioned, I'd resort to Rive Events and Unity Audio to get around this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants