从Gallery中拍摄照片和裁剪所选圆圈部分中的照片在Unity 3d中?

时间:2015-07-26 02:15:26

标签: unity3d

我正在开展一个项目。我想从Android设备的画廊拍照。 1.如何从Android设备库中拍照? 2.如何在unity3d中裁剪选定的圆圈部分?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码裁剪图像。我只传递Texture2D并根据纹理自动设置中心和半径;但如果你愿意,你可以手动传递它们。

Texture2D RoundCrop (Texture2D sourceTexture) {
        int width = sourceTexture.width;
        int height = sourceTexture.height;
        float radius = (width < height) ? (width/2f) : (height/2f);
        float centerX = width/2f;
        float centerY = height/2f;
        Vector2 centerVector = new Vector2(centerX, centerY);

        // pixels are laid out left to right, bottom to top (i.e. row after row)
        Color[] colorArray = sourceTexture.GetPixels(0, 0, width, height);
        Color[] croppedColorArray = new Color[width*height]; 

        for (int row = 0; row < height; row++) {
            for (int column = 0; column < width; column++) {
                int colorIndex = (row * width) + column;
                float pointDistance = Vector2.Distance(new Vector2(column, row), centerVector);
                if (pointDistance < radius) {
                    croppedColorArray[colorIndex] = colorArray[colorIndex];
                }
                else {
                    croppedColorArray[colorIndex] = Color.clear;
                }
            }
        }

        Texture2D croppedTexture = new Texture2D(width, height);
        croppedTexture.SetPixels(croppedColorArray);
        croppedTexture.Apply();
        return croppedTexture; 
    }