Manually edit Unity3D collider coordinates?
Asked Answered
A

2

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.

Atp answered 23/4, 2015 at 9:46 Comment(0)
K
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);
    }
}
Kanchenjunga answered 23/4, 2015 at 10:6 Comment(6)
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?Atp
My screenshot is from 4.6.4f1, it seems they disabled this functionality in unity 5. Corrected the answer.Kanchenjunga
Nice! Semms to remove the button for standard graphical edit mode, though. Any way to have both?Atp
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.Kanchenjunga
I get it. This will do. Thanks a lot!Atp
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?'Cropper
Q
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);
    }
}
Quesenberry answered 17/1, 2016 at 3:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.