- Home /
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;
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;
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?
You don't need to have another object. All you need to have is the material and reference it from the script.
Correct, however how do I do it without a material already created?
Can't I:
Create new shader with code
Assign image from other material to that shader
Apply new shader/texture to object
?
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);
Thank you, I got it working, and posted my code. I will upvote your answer once I get enough rep. $$anonymous$$uch respect!
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.
Your answer
Follow this Question
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