I'm making a ball bouncing game through UNI.T 2D. Make a circular ball and color the ball I want to import and apply a specific image. I'd like to change it. [The image has been modified to fit the size of the ball] The image has a .png extension.
Options in use now
C#
public Color ballColor;
void Start () {
ballColor = Color.black;
}
The code you wrote is a color in the global variable of the color type ballColor.It's a code that substitutes a constant called black. In other words, it does not change the color or image.
To change the image, i.e. sprite and color, in Unity 2D, you need to access a component called SpriteRender.
Implementing this in C# code
public void SetSprite(Sprite sprite)
{
GetComponent<SpriteRenderer>().sprite = sprite;
}
public void SetColor(Color color)
{
GetComponent<SpriteRenderer>().color = color;
}
You can create and use a function that changes the sprite as shown above.
For your information, you must use a function called a mask to fill the sprite of the ball with a square image. You don't have to do it like this, but you can just use an image with a transparent background cut in a circle.
If you want to change the sprite after adjusting the color of the original white ball image, you can change the color to white and the sprite will come out.
© 2024 OneMinuteCode. All rights reserved.