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 evilzombie · Mar 27, 2014 at 03:57 PM · shadertexturechange

How to: Create shader and copy texture?

Hello,

My gameObject has a VertexLit Shader, but I want to switch to a Mobile Unlit Shader, with code. I tried this:

 var MOBILEUNLIT : Shader = Shader.Find("Mobile/Unlit");
 var TEXTUREIMAGE:Texture=renderer.material.mainTexture;
 renderer.material.shader = MOBILEUNLIT;
 renderer.material.mainTexture=TEXTUREIMAGE;

As you can see I am attempting to preserve the image in the Base (RGB) texture.

However, the object turns pink? What am I doing wrong?

Thank you kindly for any help.

EDIT: got it to work like this thanks to Professor Snake:

 var TEXTUREIMAGE:Texture=SOME_GAME_OBJECT.renderer.material.mainTexture;
 var MOBILEUNLIT:Material = new Material (Shader.Find("Mobile/Unlit (Supports Lightmap)"));
 MOBILEUNLIT.mainTexture=TEXTUREIMAGE;
 SOME_GAME_OBJECT.renderer.material=MOBILEUNLIT;
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

2 Replies

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

Answer by Professor Snake · Mar 27, 2014 at 03:58 PM

You can instead have two materials and copy the properties between them.

 var vertexLitMaterial:Material=renderer.material;
 var unlitMaterial:Material;
 
 unlitMaterial.mainTexture=vertexLitMaterial.mainTexture;
 renderer.material=unlitMaterial;
Comment
Add comment · Show 6 · 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 evilzombie · Mar 27, 2014 at 04:09 PM 0
Share

Thank you, but I was hoping to avoid having another object with another material.

Is there no way to create a shader and attach the texture like I posted?

avatar image Professor Snake · Mar 27, 2014 at 04:10 PM 0
Share

You don't need to have another object. All you need to have is the material and reference it from the script.

avatar image evilzombie · Mar 27, 2014 at 04:23 PM 0
Share

Correct, however how do I do it without a material already created?

Can't I:

  1. Create new shader with code

  2. Assign image from other material to that shader

  3. Apply new shader/texture to object

?

avatar image Professor Snake · Mar 27, 2014 at 04:47 PM 0
Share

You can create the shader and then create a new material with that shader with the $$anonymous$$aterial(shader:Shader) constructor.

So basically:

 var myShader:Shader;
 var unlit$$anonymous$$aterial=new $$anonymous$$aterial(myShader);


avatar image evilzombie · Mar 27, 2014 at 04:53 PM 0
Share

Thank you, I got it working, and posted my code. I will upvote your answer once I get enough rep. $$anonymous$$uch respect!

Show more comments
avatar image
0

Answer by evilzombie · Mar 27, 2014 at 05:35 PM

Thank you Professor Snake, you pointed me into the right direction.

I got it to work like this:

 var TEXTUREIMAGE:Texture=SOME_GAME_OBJECT.renderer.material.mainTexture;
 var MOBILEUNLIT:Material = new Material (Shader.Find("Mobile/Unlit (Supports Lightmap)"));
 MOBILEUNLIT.mainTexture=TEXTUREIMAGE;
 SOME_GAME_OBJECT.renderer.material=MOBILEUNLIT;

I am using this to switch all materials to more Mobile friendly materials runtime, mainly because it seems IOS is really good at choking on everything.

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

21 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

Related Questions

How to render part of an object 2 Answers

How can i change detail texture? 1 Answer

Add a texture onto a material 3 Answers

Is it possible to create a shader that emits light into the world? 0 Answers

Cutout Material is not showing texture transparency? 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