- Home /
Best Performance for Background Movement
Hello, I was implementing a background - movement for a little mobile game. There is a sprite that covers the entire display and the floor texture is tiled vertically. In code I use the offset value to move the texture from top to bottom depending on the speed. It works great on new generation phones (iphone 6/7 or S6/7) but on older phones I can see lags.
I was wondering now, what would have better performance.
The way I implemented it is fine
Create static ui-canvas-images and move theses with transform.position and reseting the position once it had been moved one tile.
like 2. but with sprites
Is there an even better way?
Thank you for your help! Best regards
It would help if you add your implementation to your question, this will help finding the correct answer for you specific case.
Hi, actually its nothing more then that:
//Calculate the current movement for the current frame
m_fDelta$$anonymous$$ove = m_fCurrentSpeed * Time.deltaTime;
//adjust the rectangle for the uv settings of the sprite by adding the current delta to the y coordinates
m_rect$$anonymous$$oveBackground.y += m_fDelta$$anonymous$$ove;
//apply the new rect to the uvRect of the sprite
m_rawBackground.uvRect = m_rect$$anonymous$$oveBackground;
The rawBackground is just a Raw Image that has it sprite texture in it.
Answer by beStrange · Mar 28, 2017 at 12:47 PM
Your implementation looks good already, but it might help if you shift the offset if it is more than 1 to prevent some issues with floats:
//...
m_rectMoveBackground.y += m_fDeltaMove;
// shift offeset if required
if (m_rectMoveBackground.y >= 1f)
m_rectMoveBackground.y -= 1f;
That way you can prevent to large floats and in turn so weird behaviour.
Performance wise, there aren't really many differences, depending of the implementation on different methods. There might be something else going on in your game that results in poor performance.
Since you're on mobile, i highly recommend Mark Harkness and Ian Dundore talk from Unite Europe 2016 on "Optimizing Mobile Applications"
Thank you very much for your advice! I will also look into that video!
Your answer
Follow this Question
Related Questions
Texture parameter preview. 0 Answers
Sprites grey when camera zoomed out 3 Answers
Best practices for large background image on mobile? 0 Answers
Sprite renderer, bad perfomance on mobile 1 Answer
How do you crop a Texture2d 1 Answer