Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PolygonCollider2d in 4.3 or higher. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions Assets/PolyMesh/Scripts/Editor/PolyMeshEditor.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -131,16 +142,32 @@ 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<PolygonCollider2D>();
} else {
obj = new GameObject("Collider", typeof(MeshCollider));
polyMesh.meshCollider = obj.GetComponent<MeshCollider>();
}
#else
var obj = new GameObject("Collider", typeof(MeshCollider));
polyMesh.meshCollider = obj.GetComponent<MeshCollider>();
#endif
obj.transform.parent = polyMesh.transform;
obj.transform.localPosition = Vector3.zero;
}
}
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);
}
}
}

Expand Down Expand Up @@ -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

}
Expand Down
25 changes: 24 additions & 1 deletion Assets/PolyMesh/Scripts/PolyMesh.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -9,10 +13,16 @@ public class PolyMesh : MonoBehaviour
public List<Vector3> curvePoints = new List<Vector3>();
public List<bool> isCurve = new List<bool>();
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;
Expand Down Expand Up @@ -91,6 +101,19 @@ public void BuildMesh()

void UpdateCollider(List<Vector3> 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)
{
Expand Down