- Home /
Scaling / Resizing an Image (Texture2D)
I am trying to scale an image to fit within GUILayer box. The script comes up with an error on the line with NewImageWidth_1 = image_1.width * ImageRatio;
"BCE0051: Operator '*' cannot be used with a left hand side of type 'int' and a right hand side of type 'Object'."
private var BoxWidth = 350;
private var BoxHeight = 400;
var image_1 : Texture2D;
private var maxImageWidth = BoxWidth-50;
private var ImageRatio;
private var NewImageWidth_1 = image_1.width;
private var NewImageHeight_1 = image_1.height;
if(image_1.width > maxImageWidth){
ImageRatio = maxImageWidth / image_1.width;
NewImageWidth_1 = image_1.width * ImageRatio;
NewImageHeight_1 = image_1.height * ImageRatio;
}
GUILayout.Box(image_1,[GUILayout.Width(NewImageWidth_1),GUILayout.Height(NewImageHeight_1)]);
Answer by CHPedersen · Oct 16, 2013 at 11:25 AM
Instead of trying to implement this yourself, you might be interested in Eric's (Eric5h5) solution for this, which is freely available on the Wiki:
http://wiki.unity3d.com/index.php/TextureScale (archive.org)
It allows you to select which filtering type you'd like when scaling, and it's also multithreaded, so there's a good change it'll be faster, too.
Answer by petersvp · Jun 16, 2015 at 05:39 AM
This script does the scaling on the GPU, it's lighting fast and done by me. Here is the code: http://pastebin.com/qkkhWs2J - Because this one scales with RTT, it's only available to Unity 5 users and will possibly work in Unity 4 Pro.
Awesome! Thank you @petersvp! (On _gpu_scale function i think you need to destroy allocated renderTexture after user to avoid leak)
Answer by susannamoore · Mar 10, 2014 at 10:44 AM
Scaling image will stretch the existing contents to the new dimensions. Smooth scale image generally provides better quality results than Scaling, by blending neighbouring colors. And my project goes well with imaging method: public static int ApplyResize(REImage img, float ratio);
Because there is no 'best' or 'perfect' way, there are a lot of options that you may like to consider. Google to find out an image library suit your project.
Answer by bjennings76 · Feb 18, 2015 at 01:54 AM
If anyone is looking for a way to resize the 'canvas' of a Texture2D, here's some I ended up with after some trial and error:
public static Color32[] ResizeCanvas(Texture2D texture, int width, int height)
{
var newPixels = ResizeCanvas(texture.GetPixels32(), texture.width, texture.height, width, height);
texture.Resize(width, height);
texture.SetPixels32(newPixels);
texture.Apply();
return newPixels;
}
private static Color32[] ResizeCanvas(IList<Color32> pixels, int oldWidth, int oldHeight, int width, int height)
{
var newPixels = new Color32[(width * height)];
var wBorder = (width - oldWidth) / 2;
var hBorder = (height - oldHeight) / 2;
for (int r = 0; r < height; r++)
{
var oldR = r - hBorder;
if (oldR < 0) { continue; }
if (oldR >= oldHeight) { break; }
for (int c = 0; c < width; c++)
{
var oldC = c - wBorder;
if (oldC < 0) { continue; }
if (oldC >= oldWidth) { break; }
var oldI = oldR*oldWidth + oldC;
var i = r*width + c;
newPixels[i] = pixels[oldI];
}
}
return newPixels;
}
Answer by Antony-Blackett · Apr 24, 2015 at 11:55 PM
Here's a simple texture scaling solution I saw on another answer. I use it and it works well.
http://answers.unity3d.com/questions/150942/texture-scale.html
Your answer
Follow this Question
Related Questions
Resizing GUI textures 0 Answers
Blending two Texture2D 1 Answer
Cutting off a GUITexture's texture 0 Answers
Material not updating on Image 2 Answers
Set image as material 0 Answers