问题描述
我一直在为我的游戏创建一个模拟风格评分系统,我正在用Unity3D构建,而且当我从updateScore()函数调用AnalogScoreProcessing()时,我注意到了巨大的滞后: / **************************** ****更新分数******************************** /
public void updateScore (int newScore)
{
//将旧分数/新分数发送到AnalogScoreProcessing()
if(oldScore!= newScore)
{
AnalogScoreProcessing(oldScore, newScore);
}
//显示GUI分数
oldScore = newScore;
guiText.text =+ oldScore;
}
/ ***************************模拟评分系统****** ********************** /
void AnalogScoreProcessing(int oldScore,int newScore){
for(int i = oldScore; i print(i);
$ / code $ / pre
我是否需要创建一个新的线程或者一个co例程完成循环任务,同时执行updateScore()的其余部分?我从来没有做过任何线程,但我已经使用了共同的例程。我只是不确定我应该在这里使用什么。
解决方案最简单的解决方案是在这里使用协程。
基本上是想让玩家看数数的效果吧?
那么说你要显示的数字最多是给用户一个+1的数字,所以所以数了起来将是可感知的。
尝试...
我会这样做
int current = 0;
int target = 0;
float percentateOfTarget = 0;
float step = 0.1f;
IEnumerable AnalogScoreProcessing()
{
while(current {
current =(int)Math.Ceiling(Mathf.Lerp(current,target ,percentateOfTarget));
返回产生新的WaitForSeconds(step);
percentageOfTarget + = step;
//显示GUI分数
guiText.text =+ current;
public void updateScore(int newScore)
{
//将旧得分/新得分发送到AnalogScoreProcessing( )
if(oldScore!= newScore)
{
StopCoroutine(AnalogScoreProcessing);
current = oldScore;
target = newScore;
StartCoroutine(AnalogScoreProcessing);
$ b code
$ b 请注意这段代码被直接从我的头上拉出来,所以你可能不得不在这里或那边唠叨些东西,但是应该给你一些你想要的东西。
你甚至可以/甚至应该扩展GUIText,而协程运行的效果更加显着。让我知道它是怎么回事。
在我注意到,协程并不是单独的线程,它们是在unity的主线程上运行的。
I've been messing around trying to create an analog style scoring system for my game that I am building with Unity3D and I've noticed a huge lag when I call AnalogScoreProcessing() from the updateScore() function:
/******************************** Update Score ********************************/
public void updateScore (int newScore)
{
// Send the old score/new score to AnalogScoreProcessing()
if (oldScore != newScore)
{
AnalogScoreProcessing(oldScore, newScore);
}
// Display GUI score
oldScore = newScore;
guiText.text = "" + oldScore;
}
/*************************** Analog Scoring System ****************************/
void AnalogScoreProcessing(int oldScore, int newScore){
for(int i = oldScore; i <= newScore; i++){
print (i);
}
}
Do I have to create a new thread or a co-routine to complete the looping task while the remaining parts of updateScore() are carried out? I've never done any threading but I have used co-routines. I'm just not sure what I should be using here.
解决方案 The easiest solution is to use coroutines here.
You basically want the players to see the effect of counting up right?
So say you want to display a count up to the user being +1 to the user ever few frames or so so the count up will be perceivable.
Try...
I would do it like this
int current =0;
int target = 0;
float percentateOfTarget = 0;
float step = 0.1f;
IEnumerable AnalogScoreProcessing()
{
while(current <= target)
{
current = (int)Math.Ceiling(Mathf.Lerp(current, target, percentateOfTarget));
return yield new WaitForSeconds(step);
percentageOfTarget += step;
// Display GUI score
guiText.text = "" + current;
}
}
public void updateScore (int newScore)
{
// Send the old score/new score to AnalogScoreProcessing()
if (oldScore != newScore)
{
StopCoroutine("AnalogScoreProcessing");
current = oldScore;
target = newScore;
StartCoroutine("AnalogScoreProcessing");
}
}
Please be aware this code is pulled directly out of my head so youll probably have to tweek some things here and there but should give you something close to what you desire.You could/should even scale the GUIText up while the coroutine running for an even more dramatic effect. Let me know how it goes.
As I side note coroutines are not separate threads in unity they are ran on unity's main thread.
这篇关于为什么当我调用一个方法时,我的游戏滞后巨大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!