Search Unity

Feature Request Making edge colliders and polygon colliders snap to grid.

Discussion in '2D Experimental Preview' started by Tony-Tonijn, Sep 6, 2016.

  1. Tony-Tonijn

    Tony-Tonijn

    Joined:
    Nov 13, 2013
    Posts:
    3
    I think the title says it all. During development of a platformer type of game I have used the edge collider and polygon collider quite extensively and trough its use I have programmed a little script that automatically snaps all the points on both of the colliders to the nearest grid point. It would be great if the normal functionality of holding "control to snap to whole units" would also work for these components while editing the points on the collider. Maybe a smart check that sees if it should snap to the grid instead if applicable?

    The script I use right now is posted below for everyone else to use:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [RequireComponent(typeof(PolygonCollider2D))]
    7. public class PolygonColliderSnapper : MonoBehaviour {
    8.  
    9.     public PolygonCollider2D polygon;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         if (polygon == null)
    14.             polygon = GetComponent<PolygonCollider2D>();
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.    
    20.     }
    21.  
    22.     public void Snap()
    23.     {
    24.         int counter = 0;
    25.         for (int pathIndex = 0; pathIndex < polygon.pathCount; pathIndex++)
    26.         {
    27.             Vector2[] path = polygon.GetPath(pathIndex);
    28.             for (long pointIndex = 0; pointIndex < path.Length; pointIndex++)
    29.             {
    30.                 counter++;
    31.                 Vector2 point = path[pointIndex];
    32.                 path[pointIndex] = new Vector2(Mathf.RoundToInt(point.x), Mathf.RoundToInt(point.y));
    33.             }
    34.             polygon.points = path;
    35.             polygon.SetPath(pathIndex, path);
    36.         }
    37.         print("Snapped " + counter + " points.");
    38.     }
    39. }
    40.  
    41. // Editor button script
    42. using UnityEngine;
    43. using System.Collections;
    44. using UnityEditor;
    45.  
    46. [CustomEditor(typeof(PolygonColliderSnapper))]
    47. public class ObjectBuilderEditor : Editor
    48. {
    49.     public override void OnInspectorGUI()
    50.     {
    51.         DrawDefaultInspector();
    52.  
    53.         PolygonColliderSnapper snapper = (PolygonColliderSnapper)target;
    54.         if (GUILayout.Button("Snap Points"))
    55.         {
    56.             snapper.Snap();
    57.         }
    58.     }
    59. }
    60.  
     
    Stardog likes this.
  2. Johaness_Reuben

    Johaness_Reuben

    Unity Technologies

    Joined:
    Jan 27, 2016
    Posts:
    253
    Yes, something to look into.
     
  3. legoblaster1234

    legoblaster1234

    Joined:
    Aug 7, 2017
    Posts:
    21
    noob question: how do i use the script?
     
  4. nyanginator

    nyanginator

    Joined:
    Jul 16, 2019
    Posts:
    1
    Thanks for the script, Tony-Tonijn. legoblaster1234, I got it to work like this:
    1. Right click in Project tab to create 2 scripts, "PolygonColliderSnapper" and "ObjectBuilderEditor".
    2. Copy the respective code to their own script files (Lines 1-39 to PolygonColliderSnapper, and Lines 40-60 to ObjectBuilderEditor).
    3. Add the PolygonColliderSnapper script to whatever GameObject you have your collider on. Drag the GameObject from the Hierarchy onto the Polygon slot of the Polygon Collider Snapper (Script) component in the Inspector.
    4. Click on Snap Points and the collider points should update.