Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by JCaL · Jul 22, 2016 at 08:49 AM · sprite2d-physicspixelcollider 2dpolygon collider 2d

Pixel Perfect 2D Polygon Collider

There has to be a way to generate a pixel perfect 2D polygon collider. Even with an image as simple as a 20x20 pixel black circle surrounded by a transparent backround Unitys default polygon collider fails to generate around the corners of the pixels. I don't want an answer saying "You can hold shift + ctrl to add and remove vertices." I want an answer with results.

Preforming the tedious task of moving vertices to match up with grid lines is already a painful task. But becomes that much worse when you want to make a 360*240 custom sprite. Along with the fact that there is no snap settings for collider verticies, the job is a waste of time.

I've looked through post after post and I have seen paid assets, broken links, and the old "Add and remove vertices using shift and ctrl." There has to be a better way using scripting or even a free asset. I have tried making a 3d mesh of the object which doesn't work due to the fact that the 3d and 2d physics engine do not work the same.

There needs to be a solution to a pixel perfect 2d polygon collider generator.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Jul 22, 2016 at 12:15 PM 1
Share

Yes, it's a reasonable feature request. I think the only way to make it happen atm is to write your own Editor extension. It wouldn't be too hard to write an Editor window that replicates the polygon collider editor functionality but includes snapping. Even simpler would be a simple function that goes through each point in the polygon collider and snaps it to the nearest pixel grid point. With this, you could still use Unity's built-in editor and only have to put the points at the approximate positions.

avatar image JCaL Cherno · Jul 23, 2016 at 05:27 AM 0
Share

As of now I threw together a messy scipt that snaps the collider points to the nearest tenth of a unity by rounding each point. Though this works it still requires points to be placed and removed close to corners on the map. This is the script as of now:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ColliderSnap : $$anonymous$$onoBehaviour {
 
     PolygonCollider2D col;
 
     void Start () {
         
     }
 
     void Update () {
         col = GetComponent<PolygonCollider2D> ();
         List<Vector2[]> NewPath = new List<Vector2[]> ();
         for (int i = 0; i < col.pathCount; i++) {
             NewPath.Add (col.GetPath (i));
         }
 
         int PathCount = 0;
         float x = 0f;
         float y = 0f;
 
         foreach (Vector2[] Path in NewPath) {
             for (int j = 0; j < Path.Length; j++) {
 
                 x = Path [j].x * 10;
                 y = Path [j].y * 10;
 
                 Path [j].x = ($$anonymous$$athf.Round (x)) / 10;
                 Path [j].y = ($$anonymous$$athf.Round (y)) / 10;
 
                 //Debug.Log ("Path " + PathCount + ": " + Path [j].x + "," + Path [j].y);
             }
 
             col.SetPath (PathCount, Path);
             PathCount++;
         }
     }
 }

avatar image Cherno JCaL · Jul 23, 2016 at 06:01 PM 0
Share

If it works, it's a good start I'd say. You could expose the snap functionality so you can input different values, not only 1/10th. Often, sprite resolution will be 1 pixel per unit, or 16 pixels per unit.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Nov 16, 2018 at 01:16 AM

I created a free c# script that does this automatically... https://github.com/RandomiaGaming/Unity-PixelPerfectPolygonCollider2D

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ppehkone · Feb 01, 2019 at 02:27 AM

@jchrist0922 Can you show how to use that script or give an example or tutorial?

When I attach the scripts you provided to an object, unity gives an error stating that it can't read the sprite I feed the script, and when I set the sprite to read/write in the advanced options, your script crashes unity every time (I need to close unity from task manager to get it working again).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trouble with composite and polygon collider in player 0 Answers

2D game - Sprite deformation when add frce 2 Answers

Can I use the Sprite Mode Polygon Editor to outline multiple sprites in one image atlas? Ifso, howso? 0 Answers

Avoid/Minimize Sprites extrapolation in 2D while camera changes size 0 Answers

2D Box Collider doesn't flip with Sprite 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges