Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Fonserbc
Last active April 19, 2020 10:48
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fonserbc/ed789e02ca98fc3f4ed9 to your computer and use it in GitHub Desktop.
Save Fonserbc/ed789e02ca98fc3f4ed9 to your computer and use it in GitHub Desktop.
A small editor script for Unity3D to edit EdgeCollider2D points on editor
using UnityEditor;
using UnityEngine;
using System;
public class EdgeCollider2DEditor : EditorWindow {
[MenuItem("Window/EdgeCollider2D Snap")]
public static void ShowWindow() {
EditorWindow.GetWindow (typeof(EdgeCollider2DEditor));
}
EdgeCollider2D edge;
Vector2[] vertices = new Vector2[0];
void OnGUI()
{
GUILayout.Label ("EdgeCollider2D point editor", EditorStyles.boldLabel);
edge = (EdgeCollider2D) EditorGUILayout.ObjectField("EdgeCollider2D to edit", edge, typeof(EdgeCollider2D), true);
if (vertices.Length != 0) {
for (int i = 0; i < vertices.Length; ++i) {
vertices[i] = (Vector2) EditorGUILayout.Vector2Field("Element "+i, vertices[i]);
}
}
if (GUILayout.Button ("Retrieve")) {
vertices = edge.points;
}
if (GUILayout.Button ("Set")) {
edge.points = vertices;
}
}
/*
void OnSelectionChange() {
if (Selection.gameObjects.Length == 1) {
EdgeCollider2D aux = Selection.gameObjects[0].GetComponent<EdgeCollider2D>();
if (aux) {
edge = aux;
vertices = edge.points;
}
}
}
*/
}
@Enfyve
Copy link

Enfyve commented Dec 23, 2015

Recommend encapsulating the vertices in a scroll view. eg:
Vector2 scrollPos;
...
scrollPos = EditorGUILayout.BeginScrollView (scrollPos);
...
EditorGUILayout.EndScrollView();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment