Exemplo n.º 1
0
    public static PositionAndScale RelativePosOnSpriteToUnityCoordinates(SpriteRenderer containingSpriteRenderer, Sprite includedSprite, float relativeX1, float relativeY1, float relativeX2, float relativeY2)
    {
        PositionAndScale result = new PositionAndScale();

        Vector3 spriteBottomLeftCorner = containingSpriteRenderer.sprite.bounds.min;
        Vector3 spriteTopRightCorner   = containingSpriteRenderer.sprite.bounds.max;

        float relativeCenterX = (relativeX1 + relativeX2) / 2.0f;
        float relativeCenterY = (relativeY1 + relativeY2) / 2.0f;


        result.position.x = (spriteBottomLeftCorner.x + ((spriteTopRightCorner.x - spriteBottomLeftCorner.x) * relativeCenterX)) * containingSpriteRenderer.gameObject.transform.localScale.x;
        result.position.y = (spriteTopRightCorner.y + ((spriteBottomLeftCorner.y - spriteTopRightCorner.y) * relativeCenterY)) * containingSpriteRenderer.gameObject.transform.localScale.y;

        float containingSpriteWidth = (spriteTopRightCorner.x - spriteBottomLeftCorner.x) * containingSpriteRenderer.gameObject.transform.localScale.x;
        float containingSpriteHeigh = (spriteTopRightCorner.y - spriteBottomLeftCorner.y) * containingSpriteRenderer.gameObject.transform.localScale.y;

        float relativeWidth = (relativeX2 - relativeX1) * containingSpriteWidth;
        float relativeHeigh = (relativeY2 - relativeY1) * containingSpriteHeigh;

        result.scale.x = relativeWidth / includedSprite.bounds.size.x;
        result.scale.y = relativeHeigh / includedSprite.bounds.size.y;


        //	print (containingSpriteWidth + " ///// " + containingSpriteHeigh);

        return(result);
    }
Exemplo n.º 2
0
    public void SetSpriteRelativelyToAnotherOne(Sprite sprite, SpriteRenderer parentSpriteRenderer, float relativex1, float relativey1, float relativex2, float relativey2, int orderInLayer = 0)
    {
        if (parentSpriteRenderer == null)
        {
            return;
        }

        PositionAndScale transformInScene = PositionOnSprite.RelativePosOnSpriteToUnityCoordinates(parentSpriteRenderer,
                                                                                                   sprite,
                                                                                                   relativex1,
                                                                                                   relativey1,
                                                                                                   relativex2,
                                                                                                   relativey2);

        transform.localScale    = transformInScene.scale;
        transform.localPosition = transformInScene.position;

        SetSprite(sprite, orderInLayer);
    }