使用 C# 在 uı 中显示游戏引擎的搜索百分比
Show searching percentage in uı for a game engine using C#
我有一个程序在大型单词列表中搜索单词在游戏引擎中使用 C#。但我的问题是,当单词列表太大时 UI 看起来好像挂了。为了防止它,我使用 UI 这样用户就会知道搜索了多少个词。但问题是 UI 在搜索完成后显示结果。当用户单击某个按钮时,如何让 UI 开始提供信息?提前谢谢你。
public int searchedTextAmount = 0;
void Start()
{
int seacrhedTextAmount = 0;
}
void Update()
{
ui.text = searchedTextAmount; // when buttonclickevent started, I like this value change while searching process in progress supplied by Search_Method
}
void Search_Method()
{
foreach (string word in words)
{
searchedTextAmount++;
//someoperations
}
}
void butonclickevent()
{
Invoke("Search_Method");
}
如果搜索时间过长,您可以使用 Coroutine in combination with a StopWatch
来中断搜索并让一帧渲染。
类似
// Tweak this according to your needs in the Inspector
[SerializeField] private float targetFramerate = 60f;
private StopWatch sw = new StopWatch();
IEnumerator Search_Method()
{
// No need for this to be a field
var searchedTextAmount = 0;
var total = words.Length;
// How long one frame may take before we force to render
// E.g. for 60fps this will 16.66666...
var millisecondsPerFrame = 1000f / targetFramerate;
sw.Restart();
foreach (string word in words)
{
searchedTextAmount++;
//someoperations
if(sw.elapsedMilliseconds >= millisecondsPerFrame)
{
// This will update the text once a frame (so only when needed) and will look like e.g.
// "Searching 50/500 (10%)
ui.text = $"Searching ... {searchedTextAmount}/{total} ({searchedTextAmount/(float)total:P0})";
// This tells Unity to "pause" this routine
// render this frame
// and continue from here in the next frame
yield return null;
sw.Restart();
}
}
sw.Stop();
}
void butonclickevent()
{
StartCoroutine (Search_Method());
}
因此 targetFrameRate
的较低值意味着您在搜索过程中的帧速率将下降(滞后)更多。另一方面,更高的 targetFrameRate
将导致搜索在绝对实时中花费更长的时间。
我有一个程序在大型单词列表中搜索单词在游戏引擎中使用 C#。但我的问题是,当单词列表太大时 UI 看起来好像挂了。为了防止它,我使用 UI 这样用户就会知道搜索了多少个词。但问题是 UI 在搜索完成后显示结果。当用户单击某个按钮时,如何让 UI 开始提供信息?提前谢谢你。
public int searchedTextAmount = 0;
void Start()
{
int seacrhedTextAmount = 0;
}
void Update()
{
ui.text = searchedTextAmount; // when buttonclickevent started, I like this value change while searching process in progress supplied by Search_Method
}
void Search_Method()
{
foreach (string word in words)
{
searchedTextAmount++;
//someoperations
}
}
void butonclickevent()
{
Invoke("Search_Method");
}
如果搜索时间过长,您可以使用 Coroutine in combination with a StopWatch
来中断搜索并让一帧渲染。
类似
// Tweak this according to your needs in the Inspector
[SerializeField] private float targetFramerate = 60f;
private StopWatch sw = new StopWatch();
IEnumerator Search_Method()
{
// No need for this to be a field
var searchedTextAmount = 0;
var total = words.Length;
// How long one frame may take before we force to render
// E.g. for 60fps this will 16.66666...
var millisecondsPerFrame = 1000f / targetFramerate;
sw.Restart();
foreach (string word in words)
{
searchedTextAmount++;
//someoperations
if(sw.elapsedMilliseconds >= millisecondsPerFrame)
{
// This will update the text once a frame (so only when needed) and will look like e.g.
// "Searching 50/500 (10%)
ui.text = $"Searching ... {searchedTextAmount}/{total} ({searchedTextAmount/(float)total:P0})";
// This tells Unity to "pause" this routine
// render this frame
// and continue from here in the next frame
yield return null;
sw.Restart();
}
}
sw.Stop();
}
void butonclickevent()
{
StartCoroutine (Search_Method());
}
因此 targetFrameRate
的较低值意味着您在搜索过程中的帧速率将下降(滞后)更多。另一方面,更高的 targetFrameRate
将导致搜索在绝对实时中花费更长的时间。