From 6094a70c4aff41d54b6ac0f22bec756357e201c1 Mon Sep 17 00:00:00 2001 From: Adam Metcalf Date: Sat, 18 Apr 2015 22:39:49 -0700 Subject: [PATCH] Support PolygonCollider2d in 4.3 or higher. Add support for PolygonCollider2D for Checking agaisnt 2D Colliders. --- .../PolyMesh/Scripts/Editor/PolyMeshEditor.cs | 43 ++++++++++++++++--- Assets/PolyMesh/Scripts/PolyMesh.cs | 25 ++++++++++- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Assets/PolyMesh/Scripts/Editor/PolyMeshEditor.cs b/Assets/PolyMesh/Scripts/Editor/PolyMeshEditor.cs index 2d94fa6..c0fcd70 100644 --- a/Assets/PolyMesh/Scripts/Editor/PolyMeshEditor.cs +++ b/Assets/PolyMesh/Scripts/Editor/PolyMeshEditor.cs @@ -1,4 +1,8 @@ -using UnityEngine; +#if UNITY_2_6 || UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2 +#define UNITY_4_2_OR_LOWER +#endif + +using UnityEngine; using UnityEditor; using System.Collections; using System.Collections.Generic; @@ -112,6 +116,10 @@ public override void OnInspectorGUI() //Create collider if (colliderSettings = EditorGUILayout.Foldout(colliderSettings, "Collider")) { +#if !UNITY_4_2_OR_LOWER + //Polygon Collider for 2D stuff. + var buildColliderPolygonCollider = EditorGUILayout.Toggle("PolygonCollider2D", polyMesh.buildColliderPolygonCollider); +#endif //Collider depth var colliderDepth = EditorGUILayout.FloatField("Depth", polyMesh.colliderDepth); colliderDepth = Mathf.Max(colliderDepth, 0.01f); @@ -123,6 +131,9 @@ public override void OnInspectorGUI() polyMesh.colliderDepth = colliderDepth; polyMesh.buildColliderEdges = buildColliderEdges; polyMesh.buildColliderFront = buildColliderFront; +#if !UNITY_4_2_OR_LOWER + polyMesh.buildColliderPolygonCollider = buildColliderPolygonCollider; +#endif } //Destroy collider @@ -131,8 +142,20 @@ public override void OnInspectorGUI() if (GUILayout.Button("Create Collider")) { RecordDeepUndo(); + +#if !UNITY_4_2_OR_LOWER + GameObject obj; + if (buildColliderPolygonCollider) { + obj = new GameObject("Collider", typeof(PolygonCollider2D)); + polyMesh.polyCollider = obj.GetComponent(); + } else { + obj = new GameObject("Collider", typeof(MeshCollider)); + polyMesh.meshCollider = obj.GetComponent(); + } +#else var obj = new GameObject("Collider", typeof(MeshCollider)); polyMesh.meshCollider = obj.GetComponent(); +#endif obj.transform.parent = polyMesh.transform; obj.transform.localPosition = Vector3.zero; } @@ -140,7 +163,11 @@ public override void OnInspectorGUI() else if (GUILayout.Button("Destroy Collider")) { RecordDeepUndo(); - DestroyImmediate(polyMesh.meshCollider.gameObject); + if (polyMesh.meshCollider) { + DestroyImmediate(polyMesh.meshCollider.gameObject); + } else if (polyMesh.polyCollider) { + DestroyImmediate(polyMesh.polyCollider.gameObject); + } } } @@ -296,19 +323,21 @@ void HideWireframe(bool hide) void RecordUndo() { -#if UNITY_4_3 - Undo.RecordObject(target, "PolyMesh Changed"); -#else +#if UNITY_4_2_OR_LOWER Undo.RegisterUndo(target, "PolyMesh Changed"); +#else + Undo.RecordObject(target, "PolyMesh Changed"); #endif } void RecordDeepUndo() { -#if UNITY_4_3 +#if UNITY_4_2_OR_LOWER + Undo.RegisterSceneUndo("PolyMesh Changed"); +#elif UNITY_4_3 Undo.RegisterFullObjectHierarchyUndo(target); #else - Undo.RegisterSceneUndo("PolyMesh Changed"); + Undo.RegisterFullObjectHierarchyUndo(target, "PolyMesh Changed"); #endif } diff --git a/Assets/PolyMesh/Scripts/PolyMesh.cs b/Assets/PolyMesh/Scripts/PolyMesh.cs index c906b44..5d82aa3 100644 --- a/Assets/PolyMesh/Scripts/PolyMesh.cs +++ b/Assets/PolyMesh/Scripts/PolyMesh.cs @@ -1,4 +1,8 @@ -using UnityEngine; +#if UNITY_2_6 || UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2 +#define UNITY_4_2_OR_LOWER +#endif + +using UnityEngine; using System.Collections; using System.Collections.Generic; @@ -9,10 +13,16 @@ public class PolyMesh : MonoBehaviour public List curvePoints = new List(); public List isCurve = new List(); public MeshCollider meshCollider; +#if !UNITY_4_2_OR_LOWER + public PolygonCollider2D polyCollider; +#endif [Range(0.01f, 1)] public float curveDetail = 0.1f; public float colliderDepth = 1; public bool buildColliderEdges = true; public bool buildColliderFront; +#if !UNITY_4_2_OR_LOWER + public bool buildColliderPolygonCollider; +#endif public Vector2 uvPosition; public float uvScale = 1; public float uvRotation; @@ -91,6 +101,19 @@ public void BuildMesh() void UpdateCollider(List points, int[] tris) { +#if !UNITY_4_2_OR_LOWER + //Update the polygon collider if there is one + if (polyCollider != null) + { + Vector2[] points2d = new Vector2[points.Count]; + for (int i = 0; i < points.Count; i++) { + Vector3 point = points[i]; + points2d[i] = new Vector2(point.x, point.y); + } + + polyCollider.SetPath(0, points2d); + } +#endif //Update the mesh collider if there is one if (meshCollider != null) {