I’m using gRPC whith Dialogflow in Unity, and the core of the project is Streaming data in multithread.
I have a background task who run the response and a foreground task who run the request.
Everything work until i need to instantiate gameobjects (text or audioclip) from each tasks, specially from the background tasks.
i tried to use coroutine for instantiate data in gameobjets after .movenext of task but it did’t work well.
I’m a beginner dev, and i did’t found a clear answer to my problem on the web.
Thanks for everything
private void Loop(SessionsClient.StreamingDetectIntentStream duplexStream, CancellationToken cancellationToken)
{
Task.Run(async () =>
{
IAsyncEnumerator responseStream = duplexStream.ResponseStream;
while (await responseStream.MoveNext(cancellationToken))
{
StreamingDetectIntentResponse response = responseStream.Current;
Debug.Log(response);
if (response != null)
{
if (response.OutputAudio.IsEmpty == true)
{
/*
if (response.QueryResult.FulfillmentMessages.Count > 0)
{
foreach (var message in response.QueryResult.FulfillmentMessages)
{
if (message.Text.Text_ != null)
{
string messageText = message.Text.Text_[0].ToString();
//await instantiateObj(messageText);
}
}
}else {}*/
}
else if(response.OutputAudio.IsEmpty == false)
{
byte[] audioBytes = System.Convert.FromBase64String(response.OutputAudio.ToBase64());
await instantiateAudio(audioBytes);
}
}
}
});
}
private IEnumerator instantiateAudio(byte[] audioByte)
{
_audioclip = WavUtility.ToAudioClip(audioByte, 0);
_audio = gameObject.GetComponent<AudioSource>();
_audio.clip = _audioclip;
_audio.Play();
yield return null;
}