Skip to content

Is there an option for returning the texture as square cropped from middle? #50

Answered by gokturk-tas
gokturk-tas asked this question in Q&A
Discussion options

You must be logged in to vote

I came up with a solution. I used one of @mops project script. It's link is;
https://github.com/mopsicus/unity-crop-scale-texture/blob/master/Assets/Scripts/CropScale.cs
I editted that a little bit and here is the result.

`private static Texture2D CropTexture(Texture2D texture)
{
int cropSize;
if (texture.height > texture.width)
cropSize = texture.width;
else
cropSize = texture.height;
Rect sizes = new Rect();
Texture2D result = new Texture2D(cropSize, cropSize);

      sizes.x = 0;
      sizes.y = 0;
      sizes.width = cropSize;
      sizes.height = cropSize;

      sizes.x = (texture.width - cropSize) / 2f;
      sizes.y = (texture.height - cropSize) / 2f;


      result.SetPixels(textu…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@yasirkula
Comment options

@gokturk-tas
Comment options

Answer selected by gokturk-tas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants