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
0
Question by oliver-jones · Jul 01, 2015 at 04:36 PM · texture2dcrop

Crop Texture2D - Get/Set Pixels

Hello,

I'm working on a Crop system for my Texture2Ds, and I can't seem to get it to work, not sure what I'm doing wrong here:

 //original image (before crop)
 var oImg : Texture2D = fromQuad.material.mainTexture;
 var pixels : Color[] = oImg.GetPixels(0, 0, oImg.width, oImg.height, 0); 

   
  //CROP IMAGE TO FIT SQUARE RATIO (cropSize)
 var cropSize = 500;
 var img : Texture2D = new Texture2D(cropSize, cropSize, TextureFormat.RGB24, false);
     
 img.SetPixels(0, 0, cropSize, cropSize, pixels, 0);
 img.Apply();
     
 toQuad.material.mainTexture = img;


Any ideas? Thanks

Comment
Add comment · Show 1
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 Fattie · Mar 01, 2016 at 01:08 AM 0
Share

FYI cropping code is RIGHT HERE:

http://docs.unity3d.com/ScriptReference/Texture2D.GetPixels.html

hooray!

2 Replies

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

Answer by Thomas-Mountainborn · Jul 01, 2015 at 05:32 PM

SetPixels() requires that the array is the correct size for the image (also keeping mip maps in mind). You are first getting the array of the original image in its original size, then you declare a new texture with a different resolution and you try to cram the same pixels in the new space. Instead, use oImg.GetPixels(0,0,cropSize, cropSize,0).

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 oliver-jones · Jul 01, 2015 at 05:37 PM 0
Share

I was thinking about use that technique, but the only issue now is that it forces me to only crop the lower left portion of the image. Eventually, I want to use the centre of the image.

avatar image Thomas-Mountainborn · Jul 01, 2015 at 05:49 PM 0
Share

Then just offset with half of the remaining width and height: oImg.GetPixels((oImg.width - cropSize) / 2, (oImg.height - cropSize) / 2, cropSize, cropSize, 0);

Of course, this is only correct when oImg is larger than cropSize. For decent code you'd have to check that this is the case.

Also, I just remembered that you can use Graphics.DrawTexture() to do this operation in a single statement.

avatar image
2

Answer by centsol · Apr 04, 2017 at 04:59 AM

Simple way of image Cropping in unity 5.5

 public Texture2D mTexture;
 void Start () {
     Color[] c = mTexture.GetPixels (0, 0, 200, 200);
     Texture2D m2Texture = new Texture2D (200, 200);

     m2Texture.SetPixels (c);
     m2Texture.Apply ();

     gameObject.GetComponent<MeshRenderer> ().material.mainTexture = m2Texture;



 }
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

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

24 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

Related Questions

Resize Texture2D comes out grey 2 Answers

Unity 2D, Fit a texture/sprite to a specific shape/another sprite/polygon 0 Answers

Easy way to crop a Texture2D? 0 Answers

Crop Texture2D from center 2 Answers

Scaling / Resizing an Image (Texture2D) 6 Answers


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