So I've written a program in C# that gets the current available audio output devices. So when i run the process I get the names of the devices inside the DataReceived event. When it receives "DONE" it kills the process and adds the saved names to the TMP_Dropdown options. but the problem is that when it gets to dropdown.ClearOptions() the program just stops without any error messages. When i add a breakpoint and just keep stepping through that function the yellow bar just dissapears and the function just stops. But when i just add some random strings to devices and don't run GetDevices() it works like a charm.
here is the code I was referencing above:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using TMPro;
public class GetAudioOutputDevices : MonoBehaviour
{
private Process process = null;
public List<string> devices = new List<string>();
[SerializeField]
private TMP_Dropdown dropdown;
private string selected;
private void Start()
{
GetDevices();
}
//start new process that gets returns the current audio devices
private void GetDevices()
{
devices.Clear();
try
{
process = new Process();
process.EnableRaisingEvents = false;
process.StartInfo.FileName = Application.streamingAssetsPath + "/GetAudioDevices/GetAllAudioDevices.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = false;
process.OutputDataReceived += new DataReceivedEventHandler(DataReceived);
process.Start();
process.BeginOutputReadLine();
UnityEngine.Debug.Log("successfully started app");
}
catch (Exception e)
{
UnityEngine.Debug.LogError("unable to launch app:" + e.Message);
}
}
//event that recieves the data from the process
void DataReceived(object sender, DataReceivedEventArgs eventArgs)
{
// check if process is done
if (eventArgs.Data == "DONE")
{
process.Kill();
DoneReadingDevices();
}
else
{
if (!string.IsNullOrEmpty(eventArgs.Data))
{
if (eventArgs.Data.Contains("SELECTED:"))
{
string dat = eventArgs.Data;
dat = dat.Replace("SELECTED:", "");
selected = dat;
}
else
{
UnityEngine.Debug.Log(eventArgs.Data);
devices.Add(eventArgs.Data);
}
}
}
}
//adds the devices to a textmesh pro dropdown and selects the one that was passed as selected by the process
public void DoneReadingDevices()
{
dropdown.ClearOptions();
TMP_Dropdown.OptionData selectedOpDat = null;
dropdown.AddOptions(devices);
UnityEngine.Debug.Log(dropdown.options.Count);
foreach (TMP_Dropdown.OptionData d in dropdown.options)
{
if(d.text == selected)
{
selectedOpDat = d;
break;
}
}
if(selectedOpDat != null)
{
dropdown.value = dropdown.options.IndexOf(selectedOpDat);
}
else
{
UnityEngine.Debug.Log("didn't find matching data");
}
}
}
and here is the c# program i wrote to get the audio devices:
using System;
using NAudio.CoreAudioApi;
namespace GetAllAudioDevices
{
class Program
{
static void Main(string[] args)
{
var enumerator = new MMDeviceEnumerator();
MMDevice active = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
foreach (var endpoint in enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
{
Console.WriteLine(endpoint.FriendlyName);
}
Console.WriteLine("SELECTED:" + active.FriendlyName);
Console.WriteLine("DONE");
Console.ReadLine();
}
}
}
Your issue is most probably multi-threading!
Most of the Unity API (anything immediately dependent on or influencing the Scene) can only be used in the Unity main thread, not from any background thread/task.
Your callback to process.OutputDataReceived
most probably happens on a separate thread.
You would rather need to "dispatch" the received data back into the Unity main thread.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using TMPro;
using System.Linq;
public class GetAudioOutputDevices : MonoBehaviour
{
[SerializeField]
private TMP_Dropdown dropdown;
private Process process = null;
// Thread-safe public read-only access to the _devices list
public IReadOnlyList<string> devices
{
get
{
lock(_lock)
{
return _devices;
}
}
}
// Here we actually let the thread/process write devices to
// -> only access via the lock
private readonly List<string> _devices = new List<string>();
// Our lock object for thread-safe read-write
// see https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/lock-statement
private readonly object _lock = new object();
// This will be set by the process handler
// -> only access via the lock
private string selected;
// This will be set by the process handler
// -> only access via the lock
private bool done;
// If you change the return type of Start to IEnumerator
// Unity automatically runs it as a Coroutine
// See https://docs.unity3d.com/Manual/Coroutines.html
private IEnumerator Start()
{
GetDevices();
// wait until we finished receiving the devices
// see https://docs.unity3d.com/ScriptReference/WaitUntil.html
yield return new WaitUntil(CheckIfDone);
// will now be executed in the Unity main thread
DoneReadingDevices();
}
private bool CheckIfDone()
{
lock(_lock)
{
return done;
}
}
//start new process that gets returns the current audio devices
private void GetDevices()
{
lock(_lock)
{
devices.Clear();
}
try
{
process = new Process();
process.EnableRaisingEvents = false;
process.StartInfo.FileName = Application.streamingAssetsPath + "/GetAudioDevices/GetAllAudioDevices.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = false;
process.OutputDataReceived += new DataReceivedEventHandler(DataReceived);
process.Start();
process.BeginOutputReadLine();
UnityEngine.Debug.Log("successfully started app");
}
catch (Exception e)
{
UnityEngine.Debug.LogError("unable to launch app:" + e.Message);
}
}
//event that recieves the data from the process
void DataReceived(object sender, DataReceivedEventArgs eventArgs)
{
// check if process is done
if (eventArgs.Data.Equals("DONE"))
{
process.Kill();
lock(_lock)
{
done = true;
}
}
else
{
if (!string.IsNullOrEmpty(eventArgs.Data))
{
if (eventArgs.Data.Contains("SELECTED:"))
{
var dat = eventArgs.Data;
dat = dat.Replace("SELECTED:", "");
lock(_lock)
{
selected = dat;
}
}
else
{
UnityEngine.Debug.Log(eventArgs.Data);
lock(_lock)
{
_devices.Add(eventArgs.Data);
}
}
}
}
}
//adds the devices to a textmesh pro dropdown and selects the one that was passed as selected by the process
public void DoneReadingDevices()
{
dropdown.ClearOptions();
TMP_Dropdown.OptionData selectedOpDat = null;
lock(_lock)
{
dropdown.AddOptions(devices);
// Using Linq instead of the foreach loop
// see https://docs.microsoft.com/dotnet/api/system.linq.enumerable.firstordefault
selectedOpDat = dropdown.options.FirstOrDefault(d => d.text.Equals(currentSelected));
}
UnityEngine.Debug.Log(dropdown.options.Count);
if(selectedOpDat != null)
{
dropdown.value = dropdown.options.IndexOf(selectedOpDat);
}
else
{
UnityEngine.Debug.Log("didn't find matching data");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.