Example #1
0
    public List <Polygon2> GetPhysicsShapePolygons()
    {
        if (spritePhysicsShapePolygons == null)
        {
            spritePhysicsShapePolygons = new List <Polygon2>();

            if (originalSprite == null)
            {
                return(spritePhysicsShapePolygons);
            }

                                #if UNITY_2017_4_OR_NEWER
            if (spritePhysicsShape == null)
            {
                spritePhysicsShape = SpriteExtension.PhysicsShapeManager.RequesCustomShape(originalSprite);
            }

            if (spritePhysicsShape != null)
            {
                spritePhysicsShapePolygons = spritePhysicsShape.Get();
            }
                                #endif
        }

        return(spritePhysicsShapePolygons);
    }
        static public PhysicsShape RequesCustomShape(Sprite originalSprite)
        {
            if (originalSprite == null)
            {
                return(null);
            }

            PhysicsShape shape = null;

            bool exist = dictionary.TryGetValue(originalSprite, out shape);

            if (exist)
            {
                if (shape == null || shape.GetSprite().texture == null)
                {
                    shape = RequestCustomShapeAccess(originalSprite);
                }
                return(shape);
            }
            else
            {
                shape = RequestCustomShapeAccess(originalSprite);
                return(shape);
            }
        }
        static public PhysicsShape RequestCustomShapeAccess(Sprite originalSprite)
        {
            PhysicsShape shape = null;

            bool exist = dictionary.TryGetValue(originalSprite, out shape);

            if (exist)
            {
                if (shape == null || shape.GetSprite().texture == null)
                {
                    dictionary.Remove(originalSprite);

                    shape = AddShape(originalSprite);

                    dictionary.Add(originalSprite, shape);
                }
                return(shape);
            }
            else
            {
                shape = AddShape(originalSprite);

                dictionary.Add(originalSprite, shape);

                return(shape);
            }
        }
        static private PhysicsShape AddShape(Sprite sprite)
        {
            if (sprite == null || sprite.texture == null)
            {
                return(null);
            }

            PhysicsShape shape = new PhysicsShape();

            shape.SetSprite(sprite);

            return(shape);
        }
Example #5
0
    public LightingOcclussion(LightingOcclusionShape shape, float size)
    {
        spriteRenderer.sprite = shape.spritePhysicsShape.GetOriginalSprite();

        if (shape.spritePhysicsShape.GetSpriteRenderer() != null)
        {
            spriteRenderer.flipX = shape.spritePhysicsShape.GetSpriteRenderer().flipX;
            spriteRenderer.flipY = shape.spritePhysicsShape.GetSpriteRenderer().flipY;
        }
        else
        {
            spriteRenderer.flipX = false;
            spriteRenderer.flipY = false;
        }

        polygonPoints.Clear();
        outlinePoints.Clear();
        polygonPairs.Clear();

        List <Polygon2> polygons = null;

        switch (shape.shadowType)
        {
        case LightOcclusion2D.ShadowType.Collider:
            polygons = new List <Polygon2>();

            List <Polygon2> polygons3 = shape.GetPolygonsLocal();

            foreach (Polygon2 p in polygons3)
            {
                Polygon2 poly = p.Copy();

                polygons.Add(poly);
            }


            break;

        case LightOcclusion2D.ShadowType.SpritePhysicsShape:
            SpriteRenderer sRenderer = shape.spritePhysicsShape.GetSpriteRenderer();

            SpriteExtension.PhysicsShape customShape = SpriteExtension.PhysicsShapeManager.RequesCustomShape(sRenderer.sprite);

            List <Polygon2> polygons2 = customShape.Get();

            polygons = new List <Polygon2>();

            foreach (Polygon2 p in polygons2)
            {
                Polygon2 poly = p.Copy();
                polygons.Add(poly);
            }

            break;
        }

        if (polygons == null || polygons.Count < 1)
        {
            return;
        }

        foreach (Polygon2 polygon in polygons)
        {
            polygon.Normalize();

            polygonPoints.Add(Pair2D.GetList(polygon.points));
            outlinePoints.Add(Pair2D.GetList(PreparePolygon(polygon, size).points));
            polygonPairs.Add(DoublePair2.GetList(polygon.points));
        }
    }