- Home /
CustomEditor PolygonCollider2D, edit button disappear
Hello
I thought I'd just add a quick and easy editor script to snap my PolygonCollider2D points to a desired value. Turns out I'm having some trouble.
The script works fine and does exactly what I want, but when my script is active the "edit collider" button disappears from the inspector.
Any help with this is much appreciated, it's 2am here so hopefully it's not just me being stupid. This is my editor script.
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor (typeof(PolygonCollider2D))]
[CanEditMultipleObjects]
public class Polygon2DSnap : Editor
{
private float snap = 0.32f;
public override void OnInspectorGUI ()
{
EditorGUILayout.Space ();
EditorGUILayout.Space ();
snap = EditorGUILayout.FloatField ("Snap Value", snap);
if (GUILayout.Button ("Snap Points")) {
for (int t = 0; t < targets.Length; t++) {
PolygonCollider2D collider = (PolygonCollider2D)targets [t];
for (int c = 0; c < collider.pathCount; c++) {
var path = collider.GetPath (c);
for (int p = 0; p < path.Length; p++) {
float x = Mathf.RoundToInt (path [p].x / snap) * snap;
float y = Mathf.RoundToInt (path [p].y / snap) * snap;
path [p] = new Vector2 (x, y);
collider.SetPath (c, path);
}
}
}
}
EditorUtility.SetDirty (target);
EditorGUILayout.Space ();
EditorGUILayout.Space ();
EditorGUILayout.Space ();
DrawDefaultInspector ();
}
}
Ok, so I did some more research. Seems I will have to recreate this button if I don't put this function on a separate script ins$$anonymous$$d.
I really want the button on the PolygonCollider2D because I will use it on most objects, and I don't want to add a script every time. Any help is still appreciated (with the recreation of the "Edit Collider" button).
Did you ever figure this out? I've not found any way to do this.
Actually no, it event too deep and was too much work. So I went with adding my editor script to the gameobjects and having the button there ins$$anonymous$$d. Turned out pretty well since you could have the polygon collider automatically added when you add the editor script.
New in Unity 5.4.0 (actually a feature retrieved after a regression): Physics2D: Point editing is now allowed in Inspector for Edge/PolygonCollider2D.
This doesn't directly solve your problem but you can at least input the coordinates you want manually, and therefore snap them easily to integers. Now, if you have many points and you want to snap them, say, to the closest multiple of 0.25 it's another story... In this case, do as you said, by adding another script to your object. Until 5.4.0 I was using a script to edit coordinate manually and it was working pretty well.
Your answer
Follow this Question
Related Questions
Script to create instance from in inspector 2 Answers
Lining up GUI 1 Answer
Set the Rect of PopupWindowContent 1 Answer
How to record hideFlags for Undo/Redo 0 Answers
How To Force an Inspector to Repaint 3 Answers