Unity3d代码大小与UI大小不同

时间:2018-08-03 09:05:18

标签: unity3d

I got a square in Unity with a size of 500x500(slightly transparent).

此外,我想对此区域进行快照。因此,我创建了以下用于拍照的代码(该区域的尺寸为500x500)。

public class CameraShot : MonoBehaviour{

private int width = 500; 
private int height = 500;
private string saveFolder = "/picture";
private string saveType = ".png";

public void MakePicture()
{
    var texture = SetClipping();
    EncodeAndSafe(texture);        
}

private Texture2D SetClipping()
{
    int x = (Screen.width - width) / 2;
    int y = (Screen.height - height) / 2;

    Debug.Log("[AR_Recognition] CameraShot: " + Screen.dpi);

    var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
    Rect rect = new Rect(x, y, width, height);

    tex.ReadPixels(rect, 0, 0);
    tex.Apply();
    return tex;
}

private void EncodeAndSafe(Texture2D texture)
{
    int count = 1;
    var bytes = texture.EncodeToPNG();
    Destroy(texture);

    string savePath = Application.persistentDataPath + saveFolder;

    while (true) {
        if (System.IO.File.Exists(savePath))
        {
            count++;
        }
        else
        {
            File.WriteAllBytes(savePath+count+saveType, bytes);
            Debug.Log("[AR_Recognition] CameraShot: " + savePath + saveType);
            break;
        } 
    }       
}

现在我遇到了以下问题:

The picture taken is not matching the 500x500 square but I cannot figure out why.

Ty

编辑:The Canvas information

0 个答案:

没有答案