Skip to content

Instantly share code, notes, and snippets.

@tivec
Created September 24, 2015 09:37
Show Gist options
  • Save tivec/9216ed5c30d60c4c84d8 to your computer and use it in GitHub Desktop.
Save tivec/9216ed5c30d60c4c84d8 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using System;
[Tiled2Unity.CustomTiledImporter]
public class SplitPolygonColliders : Tiled2Unity.ICustomTiledImporter {
public void HandleCustomProperties(GameObject gameObject, IDictionary<string, string> customProperties)
{
// do nothing
}
public void CustomizePrefab(GameObject prefab)
{
var polygon2Ds = prefab.GetComponentsInChildren<PolygonCollider2D>();
if (polygon2Ds == null)
return;
int wallMask = LayerMask.NameToLayer("Walls");
var wallPolygons = from polygon in polygon2Ds where polygon.gameObject.layer == wallMask select polygon;
foreach(var poly in wallPolygons)
{
for (int p = 0; p < poly.pathCount; p++)
{
// make a new gameobject for the collider
GameObject newCollider = new GameObject("Collider_" + p);
newCollider.transform.parent = poly.transform;
// Make the PolygonCollider2D
PolygonCollider2D newPolygonCollider2D = newCollider.AddComponent<PolygonCollider2D>() as PolygonCollider2D;
newPolygonCollider2D.SetPath(0, poly.GetPath(p));
newPolygonCollider2D.transform.parent = poly.gameObject.transform;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment