C# 为什么当我调用一个方法时,我的游戏落后了?
我一直在尝试为我正在使用Unity3D构建的游戏创建一个模拟式评分系统,我注意到当我从updateScore()函数调用AnalogScoreProcessing()时出现了巨大的滞后:C# 为什么当我调用一个方法时,我的游戏落后了?,c#,for-loop,unity3d,C#,For Loop,Unity3d,我一直在尝试为我正在使用Unity3D构建的游戏创建一个模拟式评分系统,我注意到当我从updateScore()函数调用AnalogScoreProcessing()时出现了巨大的滞后: /******************************** Update Score ********************************/ public void updateScore (int newScore) { // Send the old score/new sco
/******************************** 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);
}
}
/***************************更新分数********************************/
公共void updateScore(int newScore)
{
//将旧分数/新分数发送到AnalogScoreProcessing()
if(oldScore!=新闻核心)
{
模拟评分处理(oldScore、newScore);
}
//显示GUI分数
oldScore=newScore;
guiText.text=”“+oldScore;
}
/***************************模拟计分系统****************************/
无效模拟分数处理(int oldScore、int newScore){
对于(int i=oldScore;i如果oldScore
和newScore
之间存在较大差异,则AnalogScoreProcessing
中的print
功能将多次运行
您可能希望使用tween来更改显示的值,因为每帧不能显示多个值。或者,如果要打印到控制台,那么…为什么要这样做
编辑:已经移动了我不理解函数背后的逻辑,但是一旦分数发生变化,你就不能直接打印出来吗
if (oldScore != newScore)
{
//AnalogScoreProcessing(oldScore, newScore);
Debug.log(newScore);
}
您还必须在
翁圭()
函数。最简单的解决方案是在这里使用协程
你基本上是想让玩家看到计数的效果,对吗
因此,假设您希望显示一个计数,直到用户的计数为+1为止,并且只有几帧左右,这样计数才会被感知到
试试看
我会这样做
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");
}
}
int电流=0;
int目标=0;
浮动百分比目标=0;
浮动步长=0.1f;
IEnumerable模拟处理()
{
虽然(当前导致问题的oldScore
和newScore
的示例值是什么?每次角色到达新平台时,我都会将分数增加大约150。当循环打印到控制台时,播放器会延迟或暂停。因此,你之所以延迟是因为你正在向控制台写入150行?是的,我的角色会在中途停止跳跃并停止等待行写入控制台。然后不要向控制台打印150行。而是打印一行,如string.Format(“oldScore:{0};newScore:{1}”,oldScore,newScore)
或者,如果您必须将150行打印到控制台,请在StringBuilder
中构建它们,并打印这一长字符串。打印时的差异通常在150左右。我将在GUI中显示oldScore和newScore之间的每个值,而不是打印到控制台。在60 fps时,每增加150点将花费2。5秒完成。这在游戏中是相当长的一段时间。@Lomeenin我在我的答案中添加了一个快速而肮脏的解决方案。@Qubei请将此作为一个独立的答案,而不是链接到另一个答案。是的,我的游戏现在就是这样做的。但是我希望它将旧分数的每个值打印到新分数,以获得戏剧性的效果。那么为什么要创建新分数呢函数?for(inti=oldScore;i