Created
July 10, 2016 10:34
-
-
Save Domiii/731cbb628a3bd37ae95934e7aff03584 to your computer and use it in GitHub Desktop.
[Unity v5.3] Comes with an Editor that allows easily working around the Polygon2DCollider bug. WARNING: Make sure to place the Editor script in an Editor folder!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Comes with an Editor that allows easily working around the Polygon2DCollider bug. | |
*/ | |
using UnityEngine; | |
using System.Collections; | |
[RequireComponent(typeof(PolygonCollider2D))] | |
public class ImprovedPolygonCollider2D : MonoBehaviour { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Provides a simple Reset button to quickly fix a broken Polygon2DCollider. | |
* WARNING: Make sure to place this script in an "Editor" folder. | |
*/ | |
using UnityEngine; | |
using UnityEditor; | |
using System.Linq; | |
[CustomEditor(typeof(ImprovedPolygonCollider2D))] | |
public class ImprovedPolygonCollider2DEditor : Editor { | |
public override void OnInspectorGUI() { | |
base.OnInspectorGUI(); | |
var theTarget = (ImprovedPolygonCollider2D)target; | |
var spriteRenderer = theTarget.GetComponent<SpriteRenderer>(); | |
var collider = theTarget.GetComponent<PolygonCollider2D>(); | |
if (spriteRenderer != null && collider != null && GUILayout.Button("Reset")) { | |
ReplaceWithSimpleBox(); | |
} | |
} | |
void ReplaceWithSimpleBox() { | |
var theTarget = (ImprovedPolygonCollider2D)target; | |
var spriteRenderer = theTarget.GetComponent<SpriteRenderer>(); | |
var collider = theTarget.GetComponent<PolygonCollider2D>(); | |
// get bounds | |
var bounds = spriteRenderer.bounds; | |
Vector2 p1 = bounds.min; | |
Vector2 p3 = bounds.max; | |
Vector2 p2 = new Vector2(p1.x, p3.y); | |
Vector2 p4 = new Vector2(p3.x, p1.y); | |
collider.points = new Vector2[] { p1, p2, p3, p4 }; | |
// convert to object space | |
collider.points = collider.points.Select(p => (Vector2)collider.transform.InverseTransformPoint(p)).ToArray(); | |
EditorUtility.SetDirty(target); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment