TrueSync
TSPolygonCollider2D.cs
1 using System.Collections.Generic;
2 using UnityEngine;
3 
4 namespace TrueSync {
8  [AddComponentMenu("TrueSync/Physics/PolygonCollider2D", 0)]
10 
11  [SerializeField]
12  private TSVector2[] _points;
13 
14  public TSVector2[] points {
15  get {
16  if (_body != null) {
17  Physics2D.PolygonShape polygonShape = (Physics2D.PolygonShape)_body.FixtureList[0].Shape;
18  return polygonShape.Vertices.ToArray();
19  }
20 
21  return _points;
22  }
23 
24  set {
25  _points = value;
26 
27  if (_body != null) {
28  Physics2D.PolygonShape polygonShape = (Physics2D.PolygonShape)_body.FixtureList[0].Shape;
29  polygonShape.Vertices = new Physics2D.Vertices(value);
30  }
31  }
32  }
33 
37  public override TrueSync.Physics2D.Shape CreateShape() {
38  if (_points == null || _points.Length == 0) {
39  return null;
40  }
41 
42  TSVector2 lossy2D = new TSVector2(lossyScale.x, lossyScale.y);
43 
44  TrueSync.Physics2D.Vertices v = new Physics2D.Vertices();
45  for (int index = 0, length = _points.Length; index < length; index++) {
46  v.Add(TSVector2.Scale(_points[index], lossy2D));
47  }
48 
49  return new TrueSync.Physics2D.PolygonShape(v, 1);
50  }
51 
52  protected override void DrawGizmos() {
53  TSVector2[] allPoints = _points;
54 
55  if (allPoints == null || allPoints.Length == 0) {
56  return;
57  }
58 
59  for (int index = 0, length = allPoints.Length - 1; index < length; index++) {
60  Gizmos.DrawLine(allPoints[index].ToVector(), allPoints[index+1].ToVector());
61  }
62 
63  Gizmos.DrawLine(allPoints[allPoints.Length - 1].ToVector(), allPoints[0].ToVector());
64  }
65 
66  protected override Vector3 GetGizmosSize() {
67  return lossyScale.ToVector();
68  }
69 
70  }
71 
72 }
FP y
The Y component of the vector.
Definition: TSVector.cs:39
override TrueSync.Physics2D.Shape CreateShape()
Create the internal shape used to represent a TSBoxCollider.
Collider with a polygon 2D shape.
override void DrawGizmos()
Draws the specific gizmos of concrete collider (for example "Gizmos.DrawWireCube" for a TSBoxCollider...
TSVector lossyScale
Holds an first value of the GameObject&#39;s lossy scale.
FP x
The X component of the vector.
Definition: TSVector.cs:37
override Vector3 GetGizmosSize()
Returns the gizmos size.
Abstract collider for 2D shapes.
Definition: TSCollider2D.cs:11