Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Neil Smith · Oct 16, 2013 at 11:20 AM · texturetexture2dimagescaleresize

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)]);

 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image awsapps · Feb 17 at 04:53 PM 0
Share

2022 dead link

avatar image Eno-Khaon awsapps · Feb 17 at 08:16 PM 0
Share

Updated the link to an archive.org variant.

avatar image
8

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Jul 13, 2018 at 11:54 PM 0
Share

Awesome script!

avatar image Burkard · Sep 30, 2019 at 02:17 AM 0
Share

Worked perfectly (even after morethan 3 years)! Thanks!!

avatar image epernigo · Mar 06, 2020 at 10:31 AM 0
Share

Awesome! Thank you @petersvp! (On _gpu_scale function i think you need to destroy allocated renderTexture after user to avoid leak)

avatar image
-1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How do I iterate over a large Texture2d quickly? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges