Sprites
Sprites are 2D Graphic objects. If you are used to working in 3D, Sprites are essentially just standard textures but there are special techniques for combining and managing sprite textures for efficiency and convenience during development.
Unity provides a placeholder Sprite Creator, a built-in Sprite Editor, a Sprite Renderer A component that lets you display images as Sprites for use in both 2D and 3D scenes. More info
See in Glossary and a Sprite Packer A facility that packs graphics from several sprite textures tightly together within a single texture known as an atlas. Unity provides a Sprite Packer utility to automate the process of generating atlases from the individual sprite textures. More info
See in Glossary
See Importing and Setting up Sprites below for information on setting up assets as Sprites in your Unity project.
Sprite Tools
Sprite Creator
Use the Sprite Creator to create placeholder sprites in your project, so you can carry on with development without having to source or wait for graphics.
Sprite Editor
The Sprite Editor lets you extract sprite graphics from a larger image and edit a number of component images within a single texture in your image editor. You could use this, for example, to keep the arms, legs and body of a character as separate elements within one image.
Sprite Renderer
Sprites are rendered with a Sprite Renderer component rather than the Mesh Renderer A mesh component that takes the geometry from the Mesh Filter and renders it at the position defined by the object’s Transform component. More info
See in Glossary used with 3D objects A 3D GameObject such as a cube, terrain or ragdoll. More info
See in Glossary . Use it to display images as Sprites for use in both 2D and 3D scenes A Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary .
Sprite Packer
Use Sprite Packer to optimize the use and performance of video memory by your project.
Importing and Setting Up Sprites
Sprites are a type of Asset Any media or data that can be used in your game or project. An asset may come from a file created outside of Unity, such as a 3D Model, an audio file or an image. You can also create some asset types in Unity, such as an Animator Controller, an Audio Mixer or a Render Texture. More info
See in Glossary in Unity projects. You can see them, ready to use, via the Project In Unity, you use a project to design and develop a game. A project stores all of the files that are related to a game, such as the asset and Scene files. More info
See in Glossary view.
There are two ways to bring Sprites into your project:
In your computer’s Finder (Mac OS X) or File Explorer (Windows), place your image directly into your Unity Project’s Assets folder.
Unity detects this and displays it in your project’s Project view.
In Unity, go to Assets > Import New Asset to bring up your computer’s Finder (Mac OS X) or File Explorer (Windows).
From there, select the image you want, and Unity puts it in the Project view.
See Importing for more details on this and important information about organising your Assets folder.
Setting your Image as a Sprite
If your project mode is set to 2D, the image you import is automatically set as a Sprite. For details on setting your project mode to 2D, see 2D or 3D Projects.
However, if your project mode is set to 3D, your image is set as a Texture An image used when rendering a GameObject, Sprite, or UI element. Textures are often applied to the surface of a mesh to give it visual detail. More info
See in Glossary , so you need to change the asset’s Texture Type:
- Click on the asset to see its Import Inspector.
- Set the Texture Type to Sprite (2D and UI):
For details on Sprite Texture Type settings, see Texture type: Sprite (2D and UI).
Sorting Sprites
Renderers in Unity are sorted by several criteria, such as their Layer order or their distance from the Camera. Unity’s GraphicsSettings (menu: Edit > Project Settings A broad collection of settings which allow you to configure how Physics, Audio, Networking, Graphics, Input and many other areas of your project behave. More info
See in Glossary , then select the Graphics category) provide a setting called Transparency Sort Mode, which allows you to control how Sprites are sorted depending on where they are in relation to the Camera. More specifically, it uses the Sprite’s position on an axis to determine which ones are transparent against others, and which are not.
An example of when you might use this setting is to sort Sprites along the Y axis. This is quite common in 2D games, where Sprites that are higher up are sorted behind Sprites that are lower, to make them appear further away.
There are four Transparency Sort Mode options available:
Default — Sorts based on whether the Camera A component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary ’s Projection mode is set to Perspective or Orthographic
Perspective — Sorts based on perspective view. Perspective view sorts Sprites based on the distance from the Camera’s position to the Sprite’s center.
Orthographic — Sorts based on orthographic view. Orthographic view sorts Sprites based on the distance along the view direction.
Custom Axis — Sorts based on the given axis set in Transparency Sort Axis
If you have set the Transparency Sort Mode to Custom, you then need to set the Transparency Sort Axis:
If the Transparency Sort Mode is set to Custom Axis, renderers in the Scene view An interactive view into the world you are creating. You use the Scene View to select and position scenery, characters, cameras, lights, and all other types of Game Object. More info
See in Glossary are sorted based on the distance of this axis from the camera. Use a value between –1 and 1 to define the axis. For example: X=0, Y=1, Z=0 sets the axis direction to up. X=1, Y=1, Z=0 sets the axis to a diagonal direction between X and Y.
For example, if you want Sprites to behave like the ones in the image above (those higher up the y axis standing behind the Sprites that are lower on the axis), set the Transparency Sort Mode to Custom Axis, and set the Y value for the Transparency Sort Axis to a value higher than 0.
Sorting Sprites using script
You can also sort Sprites per camera through scripts A piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary , by modifying the following properties in Camera:
TransparencySortMode (corresponds with Transparency Sort Mode)
TransparencySortAxis (corresponds with Transparency Sort Axis)
Browse and load local images in Unity
This article will show you how to view a local image file, load it untiy, and display it in Unity. Basically, there are two steps: browse the local file, load the local file (other formats file principle is the same)
Thought analysis
The whole idea is:
- Call the file window of the system (open the resource manager to browse the image resources)
- Image path obtained through resource manager, and the image is loaded out
How to load local images:
- Image loading using WWW (this method works with older Unity versions)
- Image loading using Unity WebreQuest (this method is used with higher versions of Unity)
- Load the image in IO mode
Function implementation
Call the system file window
Reference comDlg32. DLL system class library, call the GetOpenFileName method to achieve the window open function. The specific implementation code is as follows:
This code alone is not enough. You also need to declare the prototype, set the parameters, create the OpenFileName class, and declare a class member for each structure element. Specific can refer to Microsoft official. NET framework advanced development documentation, this article uses the following parameters:
At this point, call system file window file is implemented.
Open a window to obtain the file path
Now that we’ve written the open window method above, we need to write a button click event. Using the button, we call the above method to open the image to load and get its location:
In the above code, there are several main parameters:
- Ofn. initialDir: is the default path of the open window
- Ofn. defExt: The type of file to display in the open window
- Ofn. file: this is the main, this is the path parameter returned by the selected image, including path + filename all together.
The other parameters are easy to understand, as long as they are set to the corresponding parameters.
Image loading method one — use WWW to load
This method is used with earlier versions of Unity. Higher versions of Unity can be used, but will be warned of obsolescence. The WWW loading method mainly loads the image as Texture2D. And we’re going to display the Image on an Image, which uses a Sprite, so we need to convert the Image to a Sprite and assign a value to the Image. The following method is to load the image as Texture2D.
After obtaining the texture, create a Sprite based on the obtained texture
Then you can assign the Image to the Image, sometimes the Image will be larger or smaller than the Image, it is possible that someone needs to use the original Image size, it is necessary to set the Image size to the original size
Image loading method two — use UnityWebRequest to load
The first method to load local image files is to load them via WWW, but higher versions of Unity no longer support this API and use a more advanced API. According to the official document, UnityWebRequest is adopted instead. The namespace is also NetWorking. It is almost the same as WWW loading, but the code is slightly different. The specific method is as follows
Loading image method three — read the file in IO mode
Stream: The definition of stream: stream is in memory to open up memory address, and then the data in the file into the stream, in memory (stream) to change the text content, and then save back to the file. The keyword for a stream is FileStream. To apply FileStream to a stream, you first need to reference the namespace using System.IO; using System.IO.Compression; The specific code is:
Note: If you Read the contents of the file and assign it to the Syte array, it will be stored in the stream, and if you write the same contents to the stream, it will double the contents of the original file, So by reading the data in the stream, you can write the new data into the stream and write both the data in the file and the new data from the stream to the file.
Results show
All three loading methods have the same effect
Write in the last
All the shared content are the author in the daily development process used a variety of small function points, share out also in a disguised review, if there is a bad place to write also please give advice. The source code of the Demo will be sorted out and shared later.
How to Load a Picture from File in Unity
Loading from a file into a Texture2D to show on UI in Unity
In a previous article, I showed how to encode a Texture2D into a byte array and save it to a file.
How to Save a Picture to File in Unity
Texture2D encoding into bytes.
Of course, it is possible also the other way around: from file to Unity asset. This is particularly useful and many can be the applications: a part of the savegame information, dynamic loading of non-preloaded textures…
I’m going to show two possible ways, one requiring just Unity libraries and one which uses a free asset you can find on the asset store.
First of all, we need to read the file and get a byte array:
Texture2D method
The first way is to exploit a method from the Texture2D class. The idea is to instantiate a Texture2D with default size and load the data into it. The default size is not important since it will be overridden by the loading method.
Add an image to object in Unity editor
How do I add an image to an empty object? Which component should I add?
I’ve tried adding an «Image» component but I can’t figure out what to do afterwards.
4 Answers 4
Drag the image you want into the project directory and this will create a sprite from that image. Then drag that image onto the empty gameobject and it will create a sprite renderer component and will automatically add the image as the sprite for the sprite renderer. You can also do this manually by adding the sprite renderer and selecting your image as the sprite for the sprite renderer.
Note that if you are talking about Unity’s UI system, simply do this.
click «Add Canvas»
tip, be sure to select «Scale with screen size» there. (In 99.99999% of cases, you want that option. It’s bizarre Unity don’t make it the default; just one of those whacky things about Unity.)
simply click «Add Image»
Add images like the crazy — it’s that easy. You’ll have to learn to use Unity’s (superb) reactive layout system to position stuff on the screen in an advanced way. (Allow 3-4 months of study on that to become a hobbyist-level engineer on that system.)