Let’s first look up what a sprite is:
“Sprites are 2D graphic objects used for characters, props, projectiles and other elments of 2D gameplay. The graphics are obtained from bitmap images - Texture2D.”
If you go to the link I provided above, you’ll see exactly what the Sprite class is composed of. The Texture2D is what determines the image drawn by the SpriteRenderer or Image component that you use to represent the sprite with.
But maybe I went a bit too far ahead. You didn’t mention having any experience with programming, so I’ll just assume you meant how to make your own Sprites in Unity.
Usually, you make an image in a tool like Photoshop - or maybe just Paint if that’s your preference. You save this image on your computer. From there, you have to import it into your Unity project. This is easy, you just have to put the file in your Assets folder. In case you didn’t know this already, here’s a little help:
When you have your image in your assets folder, you can click on it and change some of the settings. (For example, you’ll need to change the filter mode to “Point (no filter)” if you have a low-resolution image, or it’ll go all blurry.)
You’ve imported your image and can now put it in the “Sprite” slot of a SpriteRenderer if you want to.
In case you want to know how to create and edit a Sprite in code, here’s an example (sorry if you’re new to programming):
// To create a Sprite from scratch, we'll need a texture. In the constructor, we can
// tweak the texture a bit more, but we won't worry about that right now. I'm making my
// texture 2 by 2 so that it's easier to imagine.
Texture2D myTexture = new Texture2D(2, 2);
// There are two methods (probably more, but they're all I know about) to edit a
// Texture2D. I'll use both of them to make our Texture2D green.
// Texture.SetPixels(): Use this if you want to change many pixels at once.
Color[] myPixels = myTexture.GetPixels();
for (int i = 0; i < myPixels.Length; i++)
{
myPixels *= Color.green;*
}
myTexture.SetPixels(myPixels);
// This is a representation of our four pixels:
// [2][3]
// [0][1]
// The numbers represent their place in the array of pixels.
// Texture.SetPixel(): Use this if you want to change a few pixels at once.
for (int x = 0; x < myTexture.width; x++)
{
for (int y = 0; y < myTexture.height; y++)
{
myTexture.SetPixel(x, y, Color.green);
}
}
// This is a representation of our four pixels:
// [0,1][1,1]
// [0,0][1,0]
// Here, the Texture2D is represented as pixel coordinates: [x,y]
// Now that we’re done, we need to apply our changes to the Texture2D. If we don’t do
// this, the changes won’t be shown. This is a costly operation, so don’t use it abundantly.
myTexture.Apply();
// Since the Sprite is an Object (Unity’s own Object class), it can’t be created with an
// ordinary “new” constructor like with our Texture2D. We just use “Sprite.Create” instead.
Sprite mySprite = Sprite.Create(myTexture, new Rect(0, 0, myTexture.width, myTexture.height), Vector2.one * 0.5f);
// If we have a reference to a SpriteRenderer, we can now use our Sprite.
// I try to find a SpriteRenderer attached to the GameObject that this script is attached to.
SpriteRenderer sprRend = this.GetComponent();
// I replace the Sprite of the SpriteRenderer with our new Sprite.
sprRend.sprite = mySprite;