Implement Constant Size Sprite in Unity2D

Implement Constant Size Sprite in Unity2D

In this tutorial, we will learn to implement a constant size sprite in Unity2D so that zooming in or zooming out won’t change the sprite’s size.

Contact me

Find the GitHub repository of this tutorial at https://github.com/shamim-akhtar/constant-size-sprite.

Click to try the WebGL version below. NOTE: The WebGL version might not work for mobile devices.

The Unity Project

In our previous tutorial, we implemented pan, zoom-in and zoom-out of our camera in 2d. We will continue to use the scene we created in our last tutorial. You can use the code from GitHub or create a new scene and import the package.

After you import the starter_scene.package, you will see the below.

The above picture shows the starter scene for our demo that comes with a 2d camera manipulator to pan and zoom.

The Normal Sprite

First, we create a normal sprite. This sprite won’t scale up or down based on the zoom factor. In other words, this will be our normal sprite. We will use the following picture for our normal sprite. You can find it in the Assets/Resources/Images folder.

The home sprite. We will use this sprite as a normal sprite.

Drag and drop this sprite onto the scene and rename it Normal Sprite. Set the scale to be 0.4, 0.4, 1.0 and the position at 1.5, 2.5, -1.0, as shown below.

The transform properties in the Inspector for Normal Sprite

The Constant Size Sprite

Next, we create another sprite. We want this sprite to scale up or down based on the zoom factor of our camera. We will use the following picture for our normal sprite. You can find it in the Assets/Resources/Images folder.

The location sprite. We will use this sprite for our constant size sprite.

Drag and drop this sprite into the scene, change the name to Constant Size Sprite, and set the location and scale as shown below.

The transform properties in the Inspector for Constant Size Sprite.

At this point, you can click on Play and see the behaviour. You will see that as you zoom in and zoom out, both our sprites change in size depending on the camera size (or the zoom factor).

We shall now implement the necessary script to fix our constant size sprite’s scale even though we change the camera’s size.

The Script

Select the Constant Size Sprite from the hierarchy, go to the Inspector and add a new Script component. call it ConstantScreenSizeForSprite.

Double-click and open in Visual Studio and add the following code.

using UnityEngine; public class ConstantScreenSizeForSprite : MonoBehaviour { // Set the original camera size. // This is the camera size when your // sprite size is just right. [SerializeField] float OriginalCameraSize = 10.0f; // Set the original scale of the sprite. [SerializeField] Vector3 OrigScale = Vector3.one; void LateUpdate() { if (Camera.main.orthographicSize > 0.1f) { transform.localScale = Camera.main.orthographicSize / OriginalCameraSize * OrigScale; } } }
Code language: C# (cs)

Go to the Unity Editor and set the values of the fields of the ConstantScreenSizeForSprite component, as shown below.

The values for the ConstantScreenSizeForSprite component.

Click play and run the application. The scale for Constant Size Sprite now changes as you zoom in and zoom out so that the pixel size of the sprite remains the same. See the video below.

Read My Other Tutorials

  1. Implement Camera Pan and Zoom Controls in Unity2D
  2. Implement Drag and Drop Item in Unity
  3. Graph-Based Pathfinding Using C# in Unity
  4. 2D Grid-Based Pathfinding Using C# and Unity
  5. 8-Puzzle Problem Using A* in C# and Unity
  6. Create a Jigsaw Puzzle Game in Unity
  7. Implement a Generic Pathfinder in Unity using C#
  8. Create a Jigsaw Puzzle Game in Unity
  9. Generic Finite State Machine Using C#
  10. Implement Bezier Curve using C# in Unity
  11. Create a Jigsaw Tile from an Existing Image
  12. Create a Jigsaw Board from an Existing Image
  13. Solving 8 puzzle problem using A* star search
  14. A Configurable Third-Person Camera in Unity
  15. Player Controls With Finite State Machine Using C# in Unity
  16. Finite State Machine Using C# Delegates in Unity
  17. Enemy Behaviour With Finite State Machine Using C# Delegates in Unity
  18. Augmented Reality – Fire Effect using Vuforia and Unity
  19. Implementing a Finite State Machine Using C# in Unity
  20. Solving 8 puzzle problem using A* star search in C++
  21. What Are C# Delegates And How To Use Them
  22. How to Generate Mazes Using Depth-First Algorithm

Leave a Reply

Your email address will not be published. Required fields are marked *