Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually edit Unity3D collider coordinates?

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.

like image 865
Kjell Rilbe Avatar asked Apr 23 '15 09:04

Kjell Rilbe


2 Answers

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);
    }
}
like image 91
Olivia Avatar answered Oct 06 '22 13:10

Olivia


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: http://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);
    }
}
like image 40
Raphael Marques Avatar answered Oct 06 '22 14:10

Raphael Marques