10

Trying to create a 2D game where I need a 2D polygon collider with exact symmetry, so I'd like to set the coordinates by hand/numerically, rather than using a mouse.

How can this be done?

I suppose the game could adjust the coordinates at start-up, but I'd prefer to have them correct "design time", if possible. Also, if I'm to do it programmatically at start-up, I'd appreciate a how-to or suitable link to help on that.

Kjell Rilbe
  • 1,331
  • 14
  • 39

2 Answers2

13

You can set collider vertices in script using PolygonCollider2D.points or you can enable debug mode in inspector and enter them manually, but this is for unity 4 only:

enter image description here

For Unity 5 you can use this workaround. Place script below to the Editor folder.

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PolygonCollider2D))]
public class PolygonCollider2DEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        var collider = (PolygonCollider2D)target;
        var points = collider.points;
        for (int i = 0; i < points.Length; i++)
        {
            points[i] = EditorGUILayout.Vector2Field(i.ToString(), points[i]);
        }
        collider.points = points;
        EditorUtility.SetDirty(target);
    }
}
Olivia
  • 1,521
  • 11
  • 7
  • Uhm... Tried Debug mode, but still can't access the points. I'm using Unity 5.0.1f1. Version difference or do I need to do something more than set Debug mode? – Kjell Rilbe Apr 23 '15 at 11:50
  • My screenshot is from 4.6.4f1, it seems they disabled this functionality in unity 5. Corrected the answer. – Olivia Apr 23 '15 at 12:17
  • Nice! Semms to remove the button for standard graphical edit mode, though. Any way to have both? – Kjell Rilbe Apr 23 '15 at 14:54
  • 1
    To do that you will need to inherit from internal UnityEditor.PolygonCollider2DEditor class, which is impossible. If you really-really want it, you can decompile it and save as your own, but then you will need Collider2DEditorBase, which inherits from ColliderEditorBase, which requires another class... You get the idea. Another possibility is to license source code from Unity. – Olivia Apr 23 '15 at 16:25
  • I get it. This will do. Thanks a lot! – Kjell Rilbe Apr 23 '15 at 18:33
  • Any idea what coordinate space these are in? I'm guessing its -1 to 1 but how does that map to the actual texture pixels?' – jjxtra Dec 18 '15 at 23:12
2

I solve this by creating other script to be added with PolygonCollider2D. This extra script that edit polygon points. So, it's a script to edit other and "Edit Collider" button stay.

print: https://i.stack.imgur.com/UN2s8.jpg

[RequireComponent(typeof(PolygonCollider2D))]
public class PolygonCollider2DManualPoins : MonoBehaviour { }

[UnityEditor.CustomEditor(typeof(PolygonCollider2DManualPoins))]
public class PolygonCollider2DManualPoinsEditor : UnityEditor.Editor {
    public override void OnInspectorGUI() {
        base.OnInspectorGUI();
        var collider = ((PolygonCollider2DManualPoins)target).GetComponent<PolygonCollider2D>();
        var points = collider.points;
        for (int i = 0; i < points.Length; i++){
            points[i] = UnityEditor.EditorGUILayout.Vector2Field(i.ToString(), points[i]);
        }
        collider.points = points;
        UnityEditor.EditorUtility.SetDirty(collider);
    }
}