在Unity3D中更改子画面像素的优化 [英] Optimization for changing pixels of sprite in Unity3D

查看:581
本文介绍了在Unity3D中更改子画面像素的优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

一个月后,我回到了为我的迷你游戏在Sprite上绘画的原型.正如您在 线程 中看到的那样,我已经找到了解决方案用于在精灵上绘画,并且由于脚本中存在较大的嵌套循环,因此迷你游戏的状况不佳(我的意思是FPS较低).因此,我尝试使用另一种方法代替Texture2D.SetPixel(),然后使用Texture2D.SetPixels(),但是就像FPS较低之前一样.

After a month, I'm coming back to the prototype for painting on sprite for my mini game. As you see in this thread I've figured out the solution for painting on sprite and because of big nested loop in the script, the mini game isn't at the good situation (I mean low FPS). So I've tried to use another method instead of Texture2D.SetPixel() then I used Texture2D.SetPixels() but like before the FPS was low.

所以现在,我没有找到解决此问题的任何方法.我想到的最后一件事是平行.

So right now, I don't find any solutions for this problem. Last thing which comes to my mind is parallel for.

伙计们,你怎么看?

答案:

如您所见,在 PockeTiger 的帮助下.我在这个迷你游戏中使用了Texture2D.SetPixelsTexture2D.GetPixels,所以对于256 * 256的精灵,游戏的速度几乎为60 FPS,对于512 * 512的精灵,游戏的速度为25 FPS.它比使用Texture2D.SetPixelTexture2D.GetPixel更好,但是仍然不好,我需要使用OpenGl命令.但是,这种出色的FPS而不是Texture2D.SetPixel,这不仅是因为使用了Texture2D.SetPixels ,这是因为,我将循环语句限制为较小的正方形,而不是整个精灵,因此在循环中移动的顺序不是很多. /strong>

With PockeTiger help as you see. I used Texture2D.SetPixels and Texture2D.GetPixels for this mini game so for 256 * 256 sprite, The game is almost on 60 FPS and for 512 * 512 sprite, It is on 25 FPS. It's better than use Texture2D.SetPixel and Texture2D.GetPixel but still It's not good and I need to use OpenGl commands. But this good FPS rather than Texture2D.SetPixel It's not just because of using Texture2D.SetPixels It's because, I limit my loop statement to smaller square rather than whole the sprite so order for moving in the loop It's not that much.

推荐答案

在使用SetPixels()时,需要注意一些事项.

There's a few things you have to look out for when you are using SetPixels().

1)首先,如果您需要更新多个像素,则不要使用SetPixel(),而要使用SetPixel s (),因为这样可以更好地提高性能.

1) First of all if you need to update multiple pixels then never use SetPixel() but SetPixels() instead because that's way better for performance.

2)Apply()函数在性能上也很繁重,每帧调用它也会大大降低性能.如果要循环,请确保从循环中排除Apply函数.

2) The Apply() function is also pretty heavy on performance and calling it every frame will draw back your performance a lot as well. If you are looping then be sure to exclude the Apply function from the loop.

for(var i:int = 0 ... blahblahblah)
{
 setPixel on i;
}
Apply();

3)如果您的纹理尺寸是2的幂(32x32//64x64//128x128//512x512 ...等),这也会有很大帮助.否则,SetPixels()和Apply()命令的执行方式会更差.

3) It also helps a lot if your texturesizes are on the power of 2 (32x32 // 64x64 // 128x128 // 512x512 .... etc.). The SetPixels() and Apply() commands perform way more poorly otherwise.

如果您尝试了我的所有建议并且FPS计数仍然很低,那么您仍然可以尝试减小纹理的大小或环顾四周,并尝试以更快的方式进行本机OpenGL调用,但需要深度深入着色器等等.

If you've tried all what I've suggested and the FPS count is still low then you can still try to decrease the size of your textures or look around and experiment with native OpenGL calls which are way faster but require a deep dive into shaders and whatnot.

这是我收集信息的源线程,如果您有兴趣了解更多详细信息. Unity社区的荣誉.

Here's the source thread where I gathered my information if you are interested in it for more details. Kudos for the Unity community.

这篇关于在Unity3D中更改子画面像素的优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆