Independent Framerate event in Unity3D

I am using the libpd4unity package to communicate with Pure Data. I get a message from Pure Data using LibPD.Bang. In case of an explosion, I play an FMOD sound.

The problem is that I often receive checks, for example, once every 500 ms, but the event does not fire at a certain frame length. Typically, a change in the length of 1 frame is smaller or larger.

Is there a solution to this problem? For example, an event independent of the frame rate? I want to know if an event (delegate) in Unity3D is frequency independent or not.

Because there is a tempo for playing every sound and only 1 rhythm of ruins.

I need to sync sounds to play every single beat.

+4
source share
1 answer

As for your question, if delegates are dependent or independent of Unity frame rate, there is no direct answer. It depends on how delegates are called. Are they called from the stream? Are they running in a thread? Coroutines are independent of frame rate; they run in a Unity loop.

The following script should highlight the difference between delegate handling in coroutines and in threads.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;

public class DelegatesAndFramerate : MonoBehaviour {

    delegate void MyDelegate();
    MyDelegate myDelegate1; // done with coroutines
    MyDelegate myDelegate2; // done with threads

    Thread thread;
    bool threadDone = false;

    private int frameCount = 0;
    private int delegate1CallCount = 0;
    private int delegate2CallCount = 0;
    private int callerLoopsCount_coroutine = 0;
    private int callerLoopsCount_thread = 0;

    void Start () {
        myDelegate1 += Elab1;
        myDelegate2 += Elab2;

        StartCoroutine(CallerCoroutine());

        thread = new Thread(new ThreadStart(CallerThread));
        thread.Start();
    }

    void Update()
    {
        frameCount++;
    }

    void Elab1()
    {
        delegate1CallCount++;
    }

    void Elab2()
    {
        delegate2CallCount++;
    }

    IEnumerator CallerCoroutine()
    {
        while(true)
        {
            callerLoopsCount_coroutine++;
            myDelegate1();
            yield return null;
        }
    }

    void CallerThread()
    {
        while(!threadDone)
        {
            callerLoopsCount_thread++;
            myDelegate2();
        }
    }

    void OnDestroy()
    {
        Debug.Log("Frame Count: " + frameCount);
        Debug.Log("Delegate Call Count (Coroutine): " + delegate1CallCount);
        Debug.Log("Delegate Call Count (Thread): " + delegate2CallCount);
        Debug.Log("Caller Loops Count (Coroutine): " + callerLoopsCount_coroutine);
        Debug.Log("Caller Loops Count (Thread): " + callerLoopsCount_thread);

        threadDone = true;
        thread.Join();
    }
}

If you attach it to GameObject and release Unity within a few seconds, you will see that the time that the delegate called from the coroutine is equal to the number of completed frames, while the time that the delegate called from the stream will be longer.

, Pure Data, , ( ) , Unity Unity Update. libPD , . -.

GUITextScript.cs, libPD . , , ; , , , .

+1

Source: https://habr.com/ru/post/1684552/


All Articles