- Home /
Unity generate a 2D background
I'm making a game where the background depends on the colors of 2 players. The colors should fade from one side's color to the other. Like a color gradient from left to right:
:
:
This should of course happen at game startup but I'm not sure the best way to generate the background for the Main camera.
Should I:
Generate an image and add it to the scene as a sprite?
Generate and add some material to an object in the scene? If so how?
Is there a way to do this directly to the main camera's default background color?
or
Is there a more efficient way?
Thanks
Answer by dargonknight · Mar 23, 2019 at 12:01 PM
if the color that is changing is simply a solid background color changing camera color would be the most efficient way of dealing with this.
cam.backgroundColor = Color.red;
if you are using a specific background image and that's the only thing changing change the color of that background
m_SpriteRenderer.color = Color.red
while if the color of multiple object change as well create a sprite material and change it's color (have a reference of it in your script don't access it directly from the object as it's a lighter process) but using this case keep in mind that the material color will not switch back on it's own you will need to reset it every time you get in the game.
renderer.material.SetColor("_Color", Color.red);
Sorry, I didn't explain well... I meant that color1 should fade into color2 as in a color gradient from left to right. Edited my question.
@RewForeN i think this would be suitable for you https://answers.unity.com/questions/913898/horizontally-gradient-on-image-ui-element.html
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Changing two different objects renderer colour 1 Answer
How to access SpriteRenderer Color from a shader? 1 Answer
Pixel snap for material made with Unlit Shader Graph? 1 Answer
How to change color of sprite as it moves over different backgrounds 2 Answers