- Home /
Applying a texture to a plane
Hi, I'm trying to apply a texture to a plane. I imported the psd file as an asset but I don't know what to do with it. I've searched the tutorials but I can't figure out how to apply a texture to a 2d plane. It would be nice if I could know how to do it at runtime AND how I can apply it before running the program.
Have you tried experimenting? It's really pretty easy. As in, drag and drop easy. To the point at which, if you can't manage that, I'm not sure if you'd be able to use anything I could tell you about doing it in scripts!
Answer by anszwa · Oct 05, 2011 at 08:31 AM
Hi, in the editor mode is it very easy, you only need to create the plane, and put the texture on it with drag and drop from the project folder into the scene view plane. Unity will automatically creates a Material folder for you with the material on the object. The other way is to create the material by yourselve (Assets-Create-Material) and put the texture on it in the inspector, drag and drop the material in the scene view like before or assign it in the hirarchy to the plane. An advice, dont work with photoshop files, they are very large. Convert the psd files to an other format like png or jpeg before importing it to unity.
If you want to change texture while runtime, the easiest way is to put a script on the plane with following code (JaveScript):
public var myTexture : Texture;
renderer.material.SetTexture("_MyTexture", myTexture);
If you want to change the material only under a certain conditions and not at startup, for example key input, you have to put the sceond line in the update function and ask for it in an if condition.
Good advice. :) But remember that this only works if the shader he selects for the material has a property called "$$anonymous$$yTexture". $$anonymous$$ost of them don't actually, for example the default diffuse shader's texture is called "$$anonymous$$ainTex" in its shader code.
To begin with, it's probably better to use the builtin property, $$anonymous$$aterial.mainTexture, it does the same as calling SetTexture with "_$$anonymous$$ainTex" as the property you're setting, see here:
http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$aterial-mainTexture.html
To be fair, doing it that way is slower, but for the purposes is much easier to use.
Oh, ok thank you, you are right! Dont think of this when i answered the question^^
If I drag and drop the texture onto the scene, it doesn't show up at runtime. I tried putting the script code you mentioned into Javascript (I tried maintexture and setTexture) and neither of them make the texture appear on the plane at runtime.
Actually, it works fine when I drag and drop but I can't get it to appear using code. When I use Javascript, the default texture "none" is applied. Do I have to somehow set the main texture in the settings somewhere?
Answer by Rustam-Ganeyev · Oct 05, 2011 at 10:39 AM
Don't know anything about psd, but i apply texture on maya's plane the following way:
public GameObject go; //your plane
void ApplyTexture(Texture texture) {
renderer.material.mainTexture = texture;
}
How to apply texture before running: put your prefab into scene. You'll see it's mesh renderer in inspector view. Then create new material with your texture and put your new created material to that mesh renderer.
Answer by anszwa · Oct 05, 2011 at 08:07 PM
Ok i dont know why it is still not working, but here are some reasons I can imagine what could be the problem. Dont know how much experience you have with unity, but that are realy basics.
Make sure that your camera is seeing the plane in the game view. There for at best use the 2-3 layout you can select in the upper right corner. Then also deselect the "maximize on play" button to see the scene view also while running the game in editor. If there is the texture on the plane, you have the wrong camera position or the side with the back face culling of the plane at the front.
Drag and drop the plane not only somewhere in the scene, drop it it directly on the plane. You should see the texture on the plane even while holding the mouse and be over the object.
If you use the script, put it the same way on the plane, and then you have to drag an drop the texture over the empty variable in the hirarchy where is standing "none" at default. You could also go to the little icon left of "none" and select it from the list. Somehow or other at the and there should be the name of the texture instead of "none".
Answer by codebaby2 · Apr 29, 2021 at 03:03 PM
Would now be:
public GameObject go; //your plane
void ApplyTexture(Texture2D texture) {
go.GetComponent().material.mainTexture = texture;
}
(based on @Rustam-Ganeyev answer)
Always cool to go back and see how things evolved within 10 years :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612235300im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
360 degree plane rotation & texture change 1 Answer
Parenting Bullet Holes? 1 Answer
Texture fade in 2 Answers
Texture3D Ignore depth mipmapping 0 Answers
Show mouse click feedback on textured 2d plane button 2 Answers