Skip to content
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
5 changes: 4 additions & 1 deletion Assets/Editor/PhysicsEditor.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

[CustomEditor(typeof(Polygon))]
public class PhysicsEditor : Editor {


SerializedProperty polygonCollider;
SerializedProperty plistPath;
SerializedProperty pixelsPerUnit;
SerializedProperty totalPolygonsinFile;
SerializedProperty selectedIndex;

public void OnEnable () {
polygonCollider = serializedObject.FindProperty("_polygonCollider");
plistPath = serializedObject.FindProperty("PlistPath");
pixelsPerUnit = serializedObject.FindProperty("PixelsPerUnit");
totalPolygonsinFile = serializedObject.FindProperty("_totalPolygonsinFile");
Expand All @@ -29,6 +31,7 @@ public override void OnInspectorGUI ()

GUI.changed = false;

EditorGUILayout.PropertyField(polygonCollider, new GUIContent("Polygon Collider"));
EditorGUILayout.PropertyField(plistPath, new GUIContent("Plist Path"));

/*If file path is changed*/
Expand Down
53 changes: 31 additions & 22 deletions Assets/Scripts/Polygon.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class Polygon : MonoBehaviour {

public int selectedIndex = 0;

private PolygonCollider2D _polygonCollider;
public PolygonCollider2D _polygonCollider = null;

[SerializeField]
public TextAsset PlistPath = null;
Expand All @@ -53,7 +53,8 @@ void Awake() {
// Use this for initialization
void Start () {

_polygonCollider = this.GetComponent<PolygonCollider2D>();
if (_polygonCollider == null)
_polygonCollider = this.GetComponent<PolygonCollider2D>();

//this.ClearBodiesList();

Expand Down Expand Up @@ -91,30 +92,35 @@ public void ParsePolygonsFromFile () {
_totalPolygonsinFile[i].bodyname = keyNames[i] as String;

Hashtable bodyDic = bodies[keyNames[i]] as Hashtable;
/*Using single fixture because unity support single fixture*/
ArrayList fixtures = bodyDic["fixtures"] as ArrayList;

Hashtable fixture1 = fixtures[0] as Hashtable;

ArrayList polygonsArray = fixture1["polygons"] as ArrayList;

PolygonPath[] totalPaths = new PolygonPath[polygonsArray.Count];

for(int j = 0; j<totalPaths.Length; j++) {
ArrayList pointArray = polygonsArray[j] as ArrayList;
PolygonPath tempPath = new PolygonPath();
Vector2[] pointsVector = new Vector2[pointArray.Count];
for(int k = 0; k<pointsVector.Length; k++) {
string pointInString = pointArray[k] as String;
pointsVector[k] = this.ConvertToVector2FromString(pointInString);

var totalPaths = new List<PolygonPath>();

for (var f = 0; f < fixtures.Count; f++) {
var fixture = fixtures[f] as Hashtable;
if (fixture == null)
continue;

var polygonsArray = fixture["polygons"] as ArrayList;
if (polygonsArray == null)
continue;

for(int j = 0; j < polygonsArray.Count; j++) {
ArrayList pointArray = polygonsArray[j] as ArrayList;
PolygonPath tempPath = new PolygonPath();
Vector2[] pointsVector = new Vector2[pointArray.Count];
for(int k = 0; k<pointsVector.Length; k++) {
string pointInString = pointArray[k] as String;
pointsVector[k] = this.ConvertToVector2FromString(pointInString);
}

tempPath.points = pointsVector;

totalPaths.Add(tempPath);
}

tempPath.points = pointsVector;

totalPaths[j] = tempPath;
}

_totalPolygonsinFile[i].paths = totalPaths;
_totalPolygonsinFile[i].paths = totalPaths.ToArray();

}

Expand Down Expand Up @@ -143,6 +149,9 @@ private Vector2 ConvertToVector2FromString(string strVector) {
public void setPolygonOfIndex(int index) {
_polygonObject = _totalPolygonsinFile[index];

if (_polygonCollider == null)
return;

_polygonCollider.pathCount = _polygonObject.TotalPaths;

for(int i = 0; i<_polygonCollider.pathCount; i++) {
Expand Down