为什么当我调用一个方法时,我的游戏滞后巨大?

编程入门 行业动态 更新时间:2024-10-21 03:24:03
本文介绍了为什么当我调用一个方法时,我的游戏滞后巨大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在为我的游戏创建一个模拟风格评分系统,我正在用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 <= newScore; 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 <= target) { 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.

更多推荐

为什么当我调用一个方法时,我的游戏滞后巨大?

本文发布于:2023-10-31 06:10:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当我   方法   游戏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!