• 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
Question by ina · Nov 02, 2011 at 06:07 PM · texturematerialtexture2drenderer

Convert Texture to Texture2D e.g. renderer.material.mainTexture

How do you convert renderer.material.mainTexture - a texture - to a texture2d?

Comment

People who like this

0 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 redteardrop · Oct 20, 2012 at 04:17 PM 0
Share

var tex : Texture2D = renderer.material.mainTexture;
is what I use and it works for getting it in script, but I do have a warning in code that it is downcast from texture to texture2d.

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by phodges · Oct 20, 2012 at 05:39 PM

EDIT: As mentioned below, if the object is already of a castable type then you should most certainly do that.

Rather than cast, you can read a RenderTexture into a Texture2D object. Switch the RenderTexture you want to active and then proceed as in t$$anonymous$$s snippet:

 RenderTexture was = RenderTexture.active;
 RenderTexture.active = <target render texture>;
 // width and height, chosen to match the source r.t.
 Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
 // Grab everyt$$anonymous$$ng
 tex.ReadPixels(new Rect(0f,0f,width,height), 0,0);
 tex.Apply();
 RenderTexture.active = was;
Comment
Propagant

People who like this

-1 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 RC-1290 · Oct 20, 2012 at 05:45 PM 0
Share

Yes, only if it is a RenderTexture. If it is a Texture2D already, stored in a Texture variable, you should simply cast it, though.

avatar image

Answer by botchiball · Nov 13, 2011 at 06:00 AM

I'm sorry Ina, I don't have an answer for you, but I do have the same problem I t$$anonymous$$nk. After searc$$anonymous$$ng all over the place, t$$anonymous$$s is the only real posting I've found even relating to t$$anonymous$$s subject.

Just a little background on my issue: I'm an artist (yeah, I know) who used java scripting to make a basic game in unity, and recently purchased everyt$$anonymous$$ng to actually get to work on an iPod Touch. But when I switched over my build to work with iOS I started getting t$$anonymous$$s error and many others, but t$$anonymous$$s one refuses to be fixed easily. Here's a look at my script:

I'm assigning a texture to the "Level_Tester" gameObject in another script, then calling t$$anonymous$$s function to recall the texture and build the level function:

BuildLevel ()

{

      var texture : Texture = GameObject.FindWithTag("Level_Tester").renderer.material.GetTexture("_MainTex");
      var ReadInLevel = texture.GetPixels(0);
      //use the pixel information as a guide to place objects on a 2d plane

}

So after switc$$anonymous$$ng to an iOS build, t$$anonymous$$s no longer compiled - saying that "GetPixels" isn't a function of "Texture". So I switched my script to "var texture : Texture2D", and w$$anonymous$$le t$$anonymous$$s version does compile - I get the warning "Implicit downcast from 'UnityEngine.Texture' to 'UnityEngine.Texture2D'. Of course running the game doesn't allow the texture to load and causes other issues. I tried flipping back to PC and Mac standalone and having "Texture2D" as my variable type and I'm having a whole new pile of issues.

I t$$anonymous$$nk I'm understanding generally that downcasting is bad, but I can't seem to find my way around it. Any suggested reading or specific help out there?

Comment

People who like this

0 Show 0 · 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

Answer by RC-1290 · Oct 20, 2012 at 05:13 PM

If the texture you want to access already is a Texture2D (in a Texture variable), you can use somet$$anonymous$$ng like t$$anonymous$$s:

 Texture2D t$$anonymous$$sWillContainTextureIfItWasA2DTexture;
 if (tex is Texture2D) t$$anonymous$$sWillContainTextureIfItWasA2DTexture= (tex as Texture2D);


But if the Texture that is being used is a RenderTexture, you can copy its contents to a Texture2D, using ReadPixels. Keep in mind that for ReadPixels to work, the texture you copy it to must be in the ARGB32 or the RGB24 TextureFormat, and the RenderTexture must have RenderTextureFormat ARGB32. Phodges posted an example of copying RenderTextures below.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Changing two different objects renderer colour 1 Answer

changing secondary texture dynamically? 1 Answer

Problem with textures ? 1 Answer

How to make dynamically texturable object model? 0 Answers

How to make my sprite face way Im walking 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