Optimization for changing pixels of sprite in Unity3D

放肆的年华 提交于 2019-12-13 20:13:09

问题


Problem :

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.

What do you think, guys ?

Answer :

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.


回答1:


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

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) 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) 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.

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.

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



来源:https://stackoverflow.com/questions/31389580/optimization-for-changing-pixels-of-sprite-in-unity3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!