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.
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:
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);
}
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With