Search Unity

Precise Polygon Collider 2D Editing

Discussion in '2D' started by Deleted User, May 12, 2015.

  1. Deleted User

    Deleted User

    Guest

    I just wanted to share some code that might make some lives easier down the road. It is not enough of a script to deserve its own asset in the store but I still think it needs to be shared. I couldn't find anything similar on the web so I decided to create this post.

    This is a custom editor script that I wrote to allow precise editing of polygon collider's vertices. If you are like me and have a problem with your PolygonCollider2D's vertices being off by fractions of a unit then this might help you. As well this script allows you to remove/add extra paths and vertices with relative ease.



    Just add the following code into a file called "PolygonEditorExtensions.cs" and place it in an "Editor" folder.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(PolygonCollider2D))]
    6. public class PolygonEditorExtensions : Editor
    7. {
    8.     private Vector2[][] _paths;
    9.     private bool[] _pathFoldouts = new bool[0];
    10.     private bool _precisionPathEdit;
    11.  
    12.     public override void OnInspectorGUI()
    13.     {
    14.         PolygonCollider2D collider = (PolygonCollider2D)target;
    15.  
    16.         DrawDefaultInspector();
    17.  
    18.         _precisionPathEdit = EditorGUILayout.Foldout(_precisionPathEdit, "Precision Path Editing");
    19.  
    20.         if (_precisionPathEdit)
    21.         {
    22.             _paths = new Vector2[collider.pathCount][];
    23.             for (int i = 0; i < collider.pathCount; i++)
    24.             {
    25.                 var path = collider.GetPath(i);
    26.                 _paths[i] = path;
    27.             }
    28.  
    29.             int size = EditorGUILayout.IntField("Size", _paths.Length);
    30.             if (size != _paths.Length)
    31.             {
    32.                 Vector2[][] newPaths = new Vector2[size][];
    33.                 for (int i = 0; i < size; i++)
    34.                 {
    35.                     if (_paths.Length > i)
    36.                     {
    37.                         newPaths[i] = _paths[i];
    38.                     }
    39.                     else
    40.                     {
    41.                         newPaths[i] = new Vector2[0];
    42.                     }
    43.                 }
    44.                 _paths = newPaths;
    45.             }
    46.  
    47.             if (_pathFoldouts.Length != _paths.Length)
    48.             {
    49.                 var newFoldouts = new bool[_paths.Length];
    50.  
    51.                 for (int i = 0; i < newFoldouts.Length; i++)
    52.                 {
    53.                     if (_pathFoldouts.Length > i)
    54.                         newFoldouts[i] = _pathFoldouts[i];
    55.                     else
    56.                         newFoldouts[i] = false;
    57.                 }
    58.  
    59.                 _pathFoldouts = newFoldouts;
    60.             }
    61.  
    62.             EditorGUI.indentLevel++;
    63.             for (int i = 0; i < size; i++)
    64.             {
    65.                 _pathFoldouts[i] = EditorGUILayout.Foldout(_pathFoldouts[i], string.Concat("Path ", i));
    66.  
    67.                 if (_pathFoldouts[i])
    68.                 {
    69.                     var path = _paths[i];
    70.                     int pathSize = EditorGUILayout.IntField("Size", path.Length);
    71.                     if (pathSize != path.Length)
    72.                     {
    73.                         Vector2[] newPath = new Vector2[pathSize];
    74.                         for (int j = 0; j < pathSize; j++)
    75.                         {
    76.                             if (path.Length > j)
    77.                             {
    78.                                 newPath[j] = path[j];
    79.                             }
    80.                             else
    81.                             {
    82.                                 newPath[j] = new Vector2();
    83.                             }
    84.                         }
    85.                         _paths[i] = newPath;
    86.                     }
    87.  
    88.                     for (int j = 0; j < pathSize; j++)
    89.                     {
    90.                         _paths[i][j] = EditorGUILayout.Vector2Field(string.Concat("Point ", j), _paths[i][j]);
    91.                     }
    92.                 }
    93.             }
    94.             EditorGUI.indentLevel--;
    95.  
    96.             collider.pathCount = _paths.Length;
    97.             for (int i = 0; i < _paths.Length; i++)
    98.             {
    99.                 collider.SetPath(i, _paths[i]);
    100.             }
    101.         }
    102.     }
    103. }

    Note: The code is really messy and I haven't bothered to clean it up or optimize it but it works. If you encounter an error and/or decided to rewrite some of the code feel free to post it below!
     
    Last edited by a moderator: May 12, 2015
  2. nicksantan

    nicksantan

    Joined:
    Mar 14, 2016
    Posts:
    1
    This is fantastic and should be a part of Unity! Shocked no one has commented yet!
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
  4. KeepTrying

    KeepTrying

    Joined:
    Feb 23, 2011
    Posts:
    119
    Any release date ?
    I'm running into serious trouble with the 2D Collision of a game.
    It used to be a 3D Collider game and the creation of the mazes was easy as pie.
    Looks like the 2D Collider is not precise as 3D Collider, when you need very small parts things get out of control.
     
    Last edited: Mar 16, 2016
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492