- Introduction
- Key Features
- Getting Started
- Creating Animations
- Animation Controller
- Runtime Implementation
- Event System
- Technical Architecture
- Best Practices
- Troubleshooting
- Contributing
Pixel Animator is a specialized Unity tool designed for frame-by-frame 2D pixel art animations. Inspired by AdamCYounis's RetroBox, this tool focuses on synchronizing frame animations with BoxCollider2D components and events, providing a more intuitive and less complex alternative to Unity's built-in animator.
The primary goal of Pixel Animator is to create a direct causal relationship between sprite animations and collision boxes while adding flexible event triggering that works in perfect sync with your animation frames.
For demo: Click here
- Simplified Animation Workflow: Avoids the complexity of Unity's animation state machine for frame-by-frame animations
- Box Collider Synchronization: Automatically updates BoxCollider2D objects in sync with animation frames
- Flexible Event System: Add events to specific frames or box colliders that trigger at precise moments
- Visual Editor: User-friendly editor window for creating and managing animations
You can add this package to your Unity project in two ways:
-
Using Unity Package Manager:
- Open Window > Package Manager
- Click the "+" button > "Add package from git URL..."
- Enter:
https://github.com/biinci/PixelAnimator.git
-
Manual Installation:
- Clone or download the repository
- Copy the files into your Unity project's Assets folder
Pixel Animator works with three primary components:
-
PixelAnimation (ScriptableObject)
- Contains the animation data including sprites, frame rate, and collider information
- Created via Assets > Create > Pixel Animation > New Animation
-
PixelAnimationController (ScriptableObject)
- Groups animations for performance optimization
- Created via Assets > Create > Pixel Animation > New Animation Controller
-
PixelAnimator (MonoBehaviour)
- The runtime component that plays animations and handles collisions
- Attached to GameObjects with SpriteRenderer components
-
Create a new PixelAnimation asset:
- Right-click in the Project window
- Select Create > Pixel Animation > New Animation
- Name your animation (e.g., "PlayerIdle")
-
Configure basic settings:
- Loop: Toggle whether the animation should loop
- FPS: Set the frames per second rate
-
Add sprites:
- Drag your sprite frames to the Pixel Sprites section
- Ensure sprites are in correct sequential order
You can add events that trigger when specific frames are displayed:
-
Open the Pixel Animator window:
- Navigate to Window > Pixel Animator
-
Select your animation asset
-
Add sprite-based events:
- In the editor window, select the "Sprite" tab
- Click "+" to add a new event
- First dropdown: Select the component type (script) to call a method from
- Second dropdown: Select the method to call when the sprite is shown
Pixel Animator allows you to create and manage BoxCollider2D objects that sync with animation frames:
-
Configure box types:
- In the Pixel Animator window, click the burger menu (☰) and select "Go to preferences"
- Set up box types with their properties (color, name, layer, physics material)
-
Add a box group to your animation:
- Return to the animation editor
- Click the burger menu again and select "Add Box Group"
- Choose the box type you want to use
-
Configure the box group:
- Set the collision type (Trigger or Collider)
- Use the box group buttons to customize its behavior
-
Edit box colliders:
- Select frames in the timeline
- Adjust the size and position of box colliders in the editor
- Use frame type options (KeyFrame, CopyFrame, EmptyFrame) to control how boxes behave across frames
Each box type can have the following properties:
- Color: Visual color in the editor (not visible during runtime)
- Name: Identifier for the box type
- Layer: Unity layer the BoxCollider2D GameObject will use
- Physics Material: Optional PhysicsMaterial2D to apply to the colliders
Frame types for boxes:
- KeyFrame: A frame with a defined box position/size
- CopyFrame: Inherits box properties from the previous KeyFrame
- EmptyFrame: No box collider appears on this frame
The PixelAnimationController improves performance by grouping animations:
-
Create a controller:
- Create > Pixel Animation > New Animation Controller
-
Add animations:
- Drag your PixelAnimation assets to the controller's list
-
Set the controller:
- Assign the controller to your PixelAnimator component
-
Add the PixelAnimator component:
- Attach to a GameObject with a SpriteRenderer
- Assign the SpriteRenderer to the component's reference
- Assign a PixelAnimationController containing your animations
-
When the PixelAnimator initializes:
- It loads animation preferences
- Creates container objects for colliders
- Compiles all event functions
To play an animation in code:
// Reference to your animator component
private PixelAnimator pixelAnimator;
// Reference to animation, could be from controller or direct
public PixelAnimation idleAnimation;
void Start() {
pixelAnimator = GetComponent<PixelAnimator>();
pixelAnimator.Play(idleAnimation);
}
When an animation plays:
- The appropriate sprite is displayed
- Box colliders are created/updated based on the current frame
- Frame events are triggered
Sprite events trigger when a specific frame is shown:
- Events are defined using UnityEvents
- You can select any component on the GameObject and call its methods
- Methods are compiled at runtime for performance
Box colliders can trigger events on collision/trigger interactions:
-
Each box can have three event types:
- OnEnter: Triggered when collision begins
- OnStay: Triggered while collision continues
- OnExit: Triggered when collision ends
-
Event behavior is determined by the box group's collision type:
- Trigger: Uses OnTriggerEnter2D/Stay2D/Exit2D with Collider2D parameter
- Collider: Uses OnCollisionEnter2D/Stay2D/Exit2D with Collision2D parameter
Pixel Animator uses a carefully designed data structure:
- PixelSprite: Contains a sprite and associated method storage
- BoxGroup: Groups related BoxCollider2D objects with shared properties
- BoxLayer: Collection of BoxFrames across the animation timeline
- BoxFrame: Per-frame box collider data including position, size, and events
During runtime:
- The PixelAnimator creates a container GameObject for colliders
- For each box group in the animation, a child GameObject is created
- BoxCollider2D components are added based on the animation data
- Appropriate handlers (ColliderInfo or CollisionInfo) are attached
- As frames change, box collider properties update automatically
Events use a sophisticated serialization system:
- MethodStorage: Contains UnityEvents and MethodData structures
- MethodData: Serializes component references, methods, and parameters
- At runtime, functions are compiled using expression trees for performance
- Parameters are preserved through serialization with SerializableData
-
Organize animations by state:
- Create separate animations for different character states (idle, run, jump, etc.)
-
Group related animations in controllers:
- Put all player animations in one controller, enemy animations in another, etc.
-
Keep FPS consistent:
- Use the same FPS across similar animations to maintain visual coherence
-
Optimize box colliders:
- Use the minimum number of box colliders needed
- Utilize frame types (especially CopyFrame) to reduce redundancy
-
Use layers effectively:
- Assign appropriate layers to different box types to control collision detection
Common issues and solutions:
-
Animation doesn't play:
- Ensure SpriteRenderer is properly assigned
- Check that sprites are added to the PixelAnimation
- Verify that Play() is being called
-
Box colliders not appearing:
- Check if box groups are properly set up
- Make sure boxes are created as KeyFrames
- Verify box size is greater than zero
-
Events not firing:
- Ensure methods have compatible parameters
- Check component references
- Make sure collision layers are set up correctly
-
Performance issues:
- Use PixelAnimationController to group animations
- Minimize the number of box colliders
- Reduce event complexity
I need your help to make Pixel Animator better! (it's very buggy) Here are some ways you can contribute:
If you find a bug or have a feature request, please open an issue on GitHub with the following information:
-
Bug Reports
- Clear description of the bug
- Steps to reproduce
- Expected behavior
- Screenshots if applicable
- Unity version and other relevant environment information
-
Feature Requests
- Clear description of the proposed feature
- Explanation of why this would be valuable to the project
- Any implementation ideas you might have