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 Xenoch · Mar 25, 2014 at 04:35 AM · texturetexture2dgetpixels

How to get ARGB values from Texture object?

I have a simple 2-triangle mesh (a quad) which I'm applying a texture to. Basically a sprite. I'm still using Unity 3.5.7.

Attached to this mesh object is a script. In the script, I want to access

 this.renderer.material.mainTexture

The Type of the object sitting at this property is "UnityEngine.Texture".

I want to get all of the pixels in this Texture as a flattened (or 2d) array of ARGB values. I do not need single-pixel access, but rather access with a method like "GetPixels32()". However, the Texture class doesn't have such a method, the Texture2d class does.

An overriding requirement for this project is that I must minimize package size (mobile app), so I am very hopeful I don't have to enable "read/write" on the texture.

My current solution, which is unacceptable, is that I have one texture tied to the material that shows up in the GUI/Editor, and a second (possibly different) texture assigned to a Texture2d Property on the script. That second texture has "read/write" enabled. This is functionally accurate, but is horrible design (2 textures for the one purpose) and requires extra memory (for read/write enabled)

Does someone with deep Unity knowledge have a solution that would (a) allow me to leave "read/write enabled" OFF AND allow me to get all pixels in a Texture (not a Texture2d) as a flattened array of ARGB values?

Thank you so much for any help you may be able to offer!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Mar 25, 2014 at 04:43 AM

Just cast renderer.material.mainTexture to Texture2D. You do need read/write enabled...if you want to read a texture, there's obviously no alternative. But there's no reason to use two textures.

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 Xenoch · Mar 25, 2014 at 02:27 PM 0
Share

Can't do that- The object is of type Texture, as is the property "mainTexture" on material. Texture2d derives FRO$$anonymous$$ Texture. You cannot cast "up" like that in C#, and only dangerously so in C++.

If the object at renderer.material.mainTexture were of type Texture2d, then I could cast that Texture property as you suggest. But it isn't. The object AND the property are both of of type UnityEngine.Texture. There's no language-level casting that can be done.

$$anonymous$$y hope is that someone knows a method around this. $$anonymous$$y next step is to use Reflection at run-time to figure out what is available on Texture as "private" fields/methods. I would prefer to avoid that, but I gotta do what I gotta do.

Thanks, though!

avatar image Eric5h5 · Mar 25, 2014 at 02:55 PM 1
Share

Yes you can. You're massively overcomplicating this.

 Texture2D myTex = renderer.material.mainTexture as Texture2D;
 var pix = myTex.GetPixels32();

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

22 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

Related Questions

Setting pixels on a texture? 2 Answers

Draw one texture on to another? Get Set Pixels. 0 Answers

Merging Textures At Runtime - Not Correct? 1 Answer

GetPixels of RenderTexture 3 Answers

GUI texture to change texture when hovered over or clicked? 2 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