Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
3
Question by parth_vadera · Feb 19, 2018 at 01:08 PM · texturetexture2dsaveconvertpainted

convert Texture to Texture2D

Hello guys, I want to save a custom painted texture of car in device storage (android/IOS) but my painted texture is of Texture type, now I need to convert it to Texture2D to have a byte format to save it into device. Searched many demos and examples but none works. Thanks!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Vincent1236 · Feb 19, 2018 at 02:01 PM

After some digging I found another link which explains your situation, see if this helps you. Copying it here for future sake (Credit To:BBIT-SOLUTIONS):

   Texture mainTexture = renderer.material.mainTexture;
              Texture2D texture2D = new Texture2D(mainTexture.width, mainTexture.height, TextureFormat.RGBA32, false);
  
              RenderTexture currentRT = RenderTexture.active;
  
              RenderTexture renderTexture = new RenderTexture(mainTexture.width, mainTexture.height, 32);
              Graphics.Blit(mainTexture, renderTexture);
  
              RenderTexture.active = renderTexture;
              texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
              texture2D.Apply();
  
              Color[] pixels = texture2D.GetPixels();
  
              RenderTexture.active = currentRT;


Comment
Add comment · Show 1 · 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 parth_vadera · Feb 19, 2018 at 02:13 PM 0
Share

Hmmm. I'm totally unaware of Graphics.Blit(). I should try it! Thank you again!

avatar image
1

Answer by Vince_TenebrisLab · Feb 19, 2018 at 01:18 PM

You can simply cast your Texture to a Texture2D. Like So:

 Texture2D tex2D = (Texture2D)PaintedTexture;

Comment
Add comment · Show 8 · 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 parth_vadera · Feb 19, 2018 at 01:24 PM 0
Share

Gives an error "InvalidCastException: Cannot cast from source type to destination type." Even PaintedTexture as Texture2D doesn't work.

avatar image Vince_TenebrisLab parth_vadera · Feb 19, 2018 at 01:29 PM -1
Share

You can definitely convert a Texture to Texture2D as shown above. Are you sure PaintedTexture is of type Texture? Some code from you might help

avatar image Bunny83 Vince_TenebrisLab · Jun 24, 2019 at 01:42 AM 3
Share

Actually, no. You can't always cast a Texture to a Texture2D. You can only do that if the texture is actually a Texture2D. Texture is the base class for all texture types. Currently there are only two texture types: Texture2D and RenderTexture. If the texture you want to cast to Texture2d is a RenderTexture it will fail.


Texture2D has a representation on the GPU and might also have one on the CPU side. RenderTextures are pure GPU textures. If you want to "convert" a RenderTexture to a Texture2D you actually have to copy the content like Vincent1236 showed in his answer.

Show more comments
avatar image karsnen · Apr 18, 2019 at 01:51 AM 0
Share

In 2018.3.11, it does work on the Editor but however it gives the following error. InvalidCastException: Specified cast is not valid.

avatar image
0

Answer by Max-Bot · Jan 07, 2019 at 04:15 PM

Unity 2018+

 public static class TextureExtentions
     {
         public static Texture2D ToTexture2D(this Texture texture)
         {
             return Texture2D.CreateExternalTexture(
                 texture.width,
                 texture.height,
                 TextureFormat.RGB24,
                 false, false,
                 texture.GetNativeTexturePtr());
         }
     }

Usage:

 Texture2D texture2D = your_texture.ToTexture2D();

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 distastee · Mar 05, 2019 at 07:45 AM 1
Share

Worth noting - Unity will die if you use this code on anything runtime generated. Only use this if you imported the asset into Unity Editor.

avatar image Mammuoils1 · Feb 17, 2020 at 09:44 AM 0
Share

Unit 2019 will crash if use this solution.

avatar image
0

Answer by RendergonPolygons · Nov 05, 2021 at 10:32 AM

Unity 2020 crash

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

114 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 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 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 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 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

Create .png file from script, then import as asset 1 Answer

Texture to Texture2D 2 Answers

GUI texture to change texture when hovered over or clicked? 2 Answers

Converting a NPOT Texture2D at runtime with SetPixel problem 1 Answer

Can Unity utilize textures created by a plugin? 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