Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
5
Question by MichaelTaylor3d · Nov 14, 2012 at 03:09 PM · texture2dscaleresizecrop

Resize Texture2D comes out grey

I'm trying to resize a downloaded image but all resized images come out solid grey. Can someone please explain why?

     public void AddPhotosToList(WWW www)
        {        
         Texture2D uploadedPhoto = www.texture;
         uploadedPhoto.Resize(256,256);
         uploadedPhoto.Apply();
         memberPhotos.Add(uploadedPhoto);
         SavePhotosToCache(uploadedPhoto);    
        }

Also, I'd rather crop the texture within the boundary of 256,256. But I cant find any functions in the documentation. If anyone has any insights on that as well.

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

2 Replies

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

Answer by Eric5h5 · Nov 14, 2012 at 04:27 PM

As the docs say about Resize: "After resizing, texture pixels will be undefined." It just resizes the texture, it does not scale the contents. You can use this instead.

Comment
Add comment · Show 7 · 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 MichaelTaylor3d · Nov 14, 2012 at 05:22 PM 0
Share

Awesome Thanks!

avatar image Cascho01 · Jul 09, 2013 at 04:14 PM 0
Share

Does not work in Webplayer -right?

avatar image Eric5h5 · Jul 09, 2013 at 06:41 PM 0
Share

It does work in the webplayer. Or at least the version I posted did; looks like someone added to it using things that don't work in the webplayer subset. I've restored it so it works in the webplayer again, and while I was at it I changed the Color stuff to Color32 so it's faster. (This is for the JS version; there's also a C# version which someone else did, so that would have to be updated as well if you want it to work in the webplayer.)

avatar image Major0 · Aug 25, 2015 at 12:17 PM 0
Share

Thanks a lot Eric! I have been trying to solve this issue for 2 months now! Will those two functions work on other Platforms ($$anonymous$$AC for example) or do I have to make platform-specific code regions to handle different platform-cases?

avatar image jaxas · May 28, 2019 at 08:30 PM 1
Share

Eric, you're savior! Works like a charm after 7 years!

Show more comments
avatar image
1

Answer by Fattie · Mar 01, 2016 at 01:45 AM

Regarding Eric5h5's famous and awesome TextureScale, here's some example code that shows a use of it, and indeed how to crop.

For google: in Unity3D take a photo from your iPhone or Android webcam device camera, take a photo on to a WebCamTexture and then scale and crop the image and save to disk.

It's all a piece of cake with TextureScale !!

 public WebCamTexture wct;
 
 public void UserClickedTakePhoto()
     {
     // take the photo. scale down to 256
     // and crop to a central-square
     
     // !!NOTE!! for simplicity this demo code
     // assumes the tetxture is wider than high!
     
     //consider... yield return new WaitForEndOfFrame(); 
     Debug.Log("is " +wct.width +" h" +wct.height );
     
     int oldW = wct.width;    // assume it's wider than high
     int oldH = wct.height;
     
     Texture2D photo = new Texture2D(oldW, oldH,
           TextureFormat.ARGB32, false);
     
     photo.SetPixels( 0,0,oldW,oldH, wct.GetPixels() );
     photo.Apply();
     
     Debug.Log("took photo !");
     Debug.Log("is " +photo.width +" h" +photo.height );
     
     int newH = 256;
     int newW = Mathf.FloorToInt(
            ((float)newH/(float)oldH) * oldW );
     TextureScale.Bilinear(photo, newW,newH);
     
     Debug.Log("scaled !");
     Debug.Log("is " +photo.width +" h" +photo.height );
     
     // finally crop to central square 256.256
     int startAcross = (newW - 256)/2;
     Debug.Log("starting across at: " +startAcross);
     
     Color[] pix = photo.GetPixels(startAcross,0, 256,256);
     photo = new Texture2D(256,256, TextureFormat.ARGB32, false);
     photo.SetPixels(pix);
     photo.Apply();
     
     Debug.Log("'SetPixels' cropped !");
     Debug.Log("is " +photo.width +" h" +photo.height );
     demoImage.texture = photo;
     
     // consider
     // System.IO.File.WriteAllBytes(
     //   Application.persistentDataPath+"p.png",
     //   photo.EncodeToPNG());
     // or
     // byte[] bytes = photo.EncodeToPNG();
     // File.WriteAllBytes(
     //   Application.persistentDataPath+"p.png", bytes);
     }

Year in and year out, TextureScale is still the best and only way to scale your PNG and Texture2D in Unity3D! Hope it saves someone some typing.

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 rmib200 · Dec 26, 2021 at 03:07 AM 0
Share

I can't find the TextureScale component. And unity wiki link of the original response is dead. Any idea on how to resolve this now?

avatar image RikuTheFuffs rmib200 · Jan 02 at 10:06 AM 0
Share

Maybe this can help: https://gist.github.com/gszauer/7799899

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

18 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

Related Questions

Scaling / Resizing an Image (Texture2D) 6 Answers

Resizing GUI textures 0 Answers

How to shorten/extend an object? 2 Answers

Resize object in runtime 1 Answer

Texture2D scale - iTween 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