Stop Unity Coroutine with DisplayCancelableProgressBar
Yes, you can use the 'DisplayCancelableProgressBar' method in Unity to stop a coroutine. You can set up a loop inside the coroutine that checks for the cancel button in the progress bar and breaks out of the loop if it is clicked. Here is an example:
IEnumerator MyCoroutine()
{
for (int i = 0; i < 100; i++)
{
// Do some work here
// Check if cancel button is clicked
if (EditorUtility.DisplayCancelableProgressBar('Progress', 'Doing work...', i / 100f))
{
// User clicked cancel, break out of loop
break;
}
yield return null;
}
// Clean up progress bar
EditorUtility.ClearProgressBar();
}
In this example, the coroutine does some work in a loop and checks for the cancel button using the 'DisplayCancelableProgressBar' method. If the cancel button is clicked, the loop is broken and the coroutine exits. Finally, the progress bar is cleaned up using the 'ClearProgressBar' method.
原文地址: https://www.cveoy.top/t/topic/lM3f 著作权归作者所有。请勿转载和采集!