Пример #1
0
        private void Update2DColliders()
        {
            for (int i = 0; i < colliders2D.Count; i++)
            {
                Collider2D source = colliders2D[i];
                if (source == null || !source.enabled || !source.gameObject.activeInHierarchy)
                {
                    continue;
                }

                Rigidbody2D rb = source.GetComponentInParent <Rigidbody2D>();
                ObiCollider oc = source.GetComponent <ObiCollider>();

                // Get the adequate rigidBodyIndex. If several colliders share a rigidbody, they'll get the same rigidBodyIndex.
                int rigidBodyIndex = -1;
                if (rb != null)
                {
                    if (!rigidbodyIDs.TryGetValue(rb.GetInstanceID(), out rigidBodyIndex))
                    {
                        ObiRigidbody or = rb.GetComponent <ObiRigidbody>();

                        rigidBodyIndex = Oni.GetRigidbodyCount(oniColliderGroup);
                        Oni.SetRigidbodies(oniColliderGroup, new Oni.Rigidbody[] {
                            new Oni.Rigidbody(rb, (or != null) ? or.kinematicForParticles : false)
                        }, 1, rigidBodyIndex);
                        rigidbodyIDs[rb.GetInstanceID()] = rigidBodyIndex;
                    }
                }

                float thickness = (oc != null)?oc.thickness:0;

                if (source is CircleCollider2D)
                {
                    Oni.SetColliders(oniColliderGroup, new Oni.Collider[] {
                        new Oni.Collider(source, Oni.ShapeType.Sphere, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Sphere), rigidBodyIndex, (oc != null)?oc.materialIndex:0)
                    }, 1, Oni.GetColliderCount(oniColliderGroup));
                    Oni.SetSphereShapes(oniColliderGroup, new Oni.SphereShape[] {
                        new Oni.SphereShape(source as CircleCollider2D)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Sphere));
                }
                else if (source is BoxCollider2D)
                {
                    Oni.SetColliders(oniColliderGroup, new Oni.Collider[] {
                        new Oni.Collider(source, Oni.ShapeType.Box, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Box), rigidBodyIndex, (oc != null)?oc.materialIndex:0)
                    }, 1, Oni.GetColliderCount(oniColliderGroup));
                    Oni.SetBoxShapes(oniColliderGroup, new Oni.BoxShape[] {
                        new Oni.BoxShape(source as BoxCollider2D)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Box));
                }
                else if (source is EdgeCollider2D)
                {
                    EdgeCollider2D           mc = source as EdgeCollider2D;
                    EdgeColliderShapeAndData shapeAndData;

                    if (!edgeColliderData.TryGetValue(source as EdgeCollider2D, out shapeAndData))
                    {
                        // Get current amount of triangle mesh shapes:
                        int shapeIndex = Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.EdgeMesh);

                        // Generate mesh collider triangle data and shape:
                        Oni.EdgeMeshData  data  = new Oni.EdgeMeshData(mc);
                        Oni.EdgeMeshShape shape = new Oni.EdgeMeshShape(mc,
                                                                        thickness,
                                                                        data.AddrOfVertexData(),
                                                                        data.AddrOfEdgeData());

                        // Pack both in a small wrapper:
                        shapeAndData = new EdgeColliderShapeAndData(data, shape, shapeIndex);

                        // Tell Oni we want to define a new triangle mesh:
                        Oni.SetEdgeMeshShapes(oniColliderGroup,
                                              new Oni.EdgeMeshShape[] { shapeAndData.meshShape },
                                              1, shapeIndex);

                        Oni.UpdateEdgeMeshShapes(oniColliderGroup, 1, shapeIndex);

                        // Store mesh collider data:
                        edgeColliderData[mc] = shapeAndData;
                    }

                    Oni.SetColliders(oniColliderGroup, new Oni.Collider[] {
                        new Oni.Collider(source, Oni.ShapeType.EdgeMesh, thickness, shapeAndData.shapeIndex, rigidBodyIndex, (oc != null)?oc.materialIndex:0)
                    }, 1, Oni.GetColliderCount(oniColliderGroup));
                }
                else
                {
                    Debug.LogWarning("2D Collider type " + source.GetType() + " not supported by Obi. Ignoring it.");
                }
            }
        }
Пример #2
0
 public EdgeColliderShapeAndData(Oni.EdgeMeshData meshData, Oni.EdgeMeshShape meshShape, int shapeIndex)
 {
     this.meshData   = meshData;
     this.meshShape  = meshShape;
     this.shapeIndex = shapeIndex;
 }