• Stars
    star
    493
  • Rank 89,306 (Top 2 %)
  • Language
    C#
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Pixel Perfect Camera for 2D Games in Unity.

PixelCamera2D

A Pixel Perfect Camera for 2D Games in Unity.

This is an early work in progress. Please report any issues you find. Also, feel free to contribute fixes or additions.

Why Should I Use PixelCamera2D?

Unity is a great engine, but it isn't perfect for 2D games that use pixel art. This can become evident when you try to rotate sprites or move sprites. Rotating sprites often results in perfectly diagonal edges that leave the pixel grid. Moving sprites can have a similar problem where pixels don't line up as expected. Check out the GIFs below in the Demo section to see examples with and without PixelCamera2D.

How It Works

The setup used in PixelCamera2D uses two camera's, a quad, and a render texture to display your game. The first camera renders an image to a render texture which is sized according to the Base Width and Base Height values. The second camera renders its child quad whose texture is the render texture mentioned above. This child quad is resized and scaled account to the screen width and height.

This method makes pixel art games more consistant by removing perfectly straight diagonal lines and providing better scaling and letterboxing options.

Installation

Copy the PixelCamera2D folder into your Assets folder.

Usage

  1. Drag the PixelCamera2D prefab from the PixelCamera2D/Prefabs folder into your scene. This prefab contains the pixel camera, render camera, and rendered quad.
  2. Set the Camera component's Size value if necessary.
  3. Set the Base Width and Base Height to a value that your pixel art was designed for. This is used to determine how many pixels wide and high your game should be. This is used when determining best fit and scaling sizes.
  4. Click the Create RenderTexture Asset button and choose a location to save the new render texture. This is created using the given Base Width and Base Height, so anytime those change you should use this button. If you have an existing render texture that is the correct size from another scene, you can simply drop that into the camera's Base Texture value in the inspector.

Inspector Options

  • Base Height: The height of your game view.
  • Base Width: The width of your game view.
  • Behaviour: The type of view scaling behaviour to use. There are two options currently.
    • Best Pixel Perfect Fit: Scales your view by pixel perfect increments like 1x, 2x, 3x, etc. This ensures equal pixel sizes. For example for a base size of 200x200, view size will be fixed to sizes like 200x200, 400x400, etc depending on screen size.
    • Scale To Fit: Scales the view to fill the screen as much as possible while maintaining aspect ratio. This fills most of the screen, but may results in some rendering artifacts because of uneven pixel sizes.
  • Quad: The quad the camera will render to. This shouldn't have to be changed.

Notes

  • To change your letterbox color just change the background color on the PixelCamera2DRenderer object.

Demo

This project contains a demo in the PixelCamera2DDemo folder. This includes an example of static sprites, moving sprites, and a line renderer to show how each behaves. It also includes a button in play mode to enable and disable PixelCamera2D so that you can compare what your game would like with or without it.

Without PixelCamera2D

Without PixelCamera2D

Notice the differently sized pixels and smooth diagonal edges on the sprites and line.

With PixelCamera2D

With PixelCamera2D

The pixels are now of equal size and the diagonal lines have been pixelated.

More Repositories

1

awesome-unity

A curated list of awesome Unity assets, resources, and more.
6,390
star
2

InputBinder

Easily bind input events to methods in Unity.
C#
75
star
3

meta

A package that makes it easy to add meta tags to your views with easy Laravel integration.
PHP
31
star
4

Letterboxer

Easily add letterboxing or pillarboxing to your cameras in Unity.
C#
26
star
5

shareable

A Laravel 4 package to make it easy to add social sharing buttons.
PHP
22
star
6

SimpleSpriteAnimator

Simpler 2D sprite animation in Unity
C#
15
star
7

wp-togetherjs

A Wordpress plugin to easily integrate Mozilla's TogetherJS realtime collaboration service
Shell
8
star
8

Bedrock-Unity

A collection Unity scripts to help get your projects off the ground.
C#
6
star
9

PoolBoy

An easy way to pool game objects in Unity.
C#
6
star
10

envious

Easy Rails environment configuration.
Ruby
6
star
11

jquery-progress

JavaScript
6
star
12

UnityExperiments

A collection of small experiments made using the Unity game engine.
C#
6
star
13

unity-helpers

A collection of helpful Unity scripts.
C#
4
star
14

game-jams

A collection of various game jam entries.
ASP.NET
4
star
15

vagrant-rails

A starter Vagrant file for Rails projects.
2
star
16

prez

Simple presenters for your PHP or Laravel projects.
PHP
2
star
17

remove_turbolinks

A Ruby on Rails gem that provides a generator for removing Turbolinks from a project.
Ruby
2
star
18

laravel-facades

An atom.io package to view the source of built-in Laravel 4 facades.
CoffeeScript
1
star
19

pollster

A simple anonymous polling application built with Ruby on Rails and Hotwire
Ruby
1
star
20

filtration

A Wordpress plugin that can filter content and comments for user-defined words.
PHP
1
star