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

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 this 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 everything
 tex.ReadPixels(new Rect(0f,0f,width,height), 0,0);
 tex.Apply();
 RenderTexture.active = was;
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 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
0

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 think. After searching all over the place, this is the only real posting I've found even relating to this 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 everything to actually get to work on an iPod Touch. But when I switched over my build to work with iOS I started getting this error and many others, but this 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 this 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 switching to an iOS build, this no longer compiled - saying that "GetPixels" isn't a function of "Texture". So I switched my script to "var texture : Texture2D", and while this 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 think 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
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
avatar image
0

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 something like this:

 Texture2D thisWillContainTextureIfItWasA2DTexture;
 if (tex is Texture2D) thisWillContainTextureIfItWasA2DTexture= (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
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

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

How to switch the texture being used by the material of the mesh renderer 1 Answer

change child texture problem 2 Answers

How could I make the texture on one object follow the rotation of another object? 0 Answers

Set image as material 0 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