- Home /
Apply material to imported mesh
I'm trying to put a material onto a mesh that I've imported from a .obj, but instead of the texture showing up, it shows a solid color that approximates the average hue of the texture.
I can instantiate a cube from the GameObject menu, then drag the material onto it in the Hierarchy view and it shows up as expected, but if I drag my mesh from the project view onto the hierarchy view, then drag the material onto it, I only get a solid color.
In case this is a strange way of doing things, I should probably also point out that I want to change the textures during the course of the game, so I don't want to apply them in the 3d app.
Answer by Eric5h5 · Mar 30, 2010 at 07:45 AM
You need to UV map your objects. Without UV mapping, the hardware has no way of knowing how to apply the texture, so you just get a solid color. The built-in cube is UV mapped.
Of course! $$anonymous$$icking myself now, I should've thought of that. Thanks.
Facepalm I knew it should be something simple. Thanks also straydogstrut for your example of using material objects.
I know it's an old post, but could someone explain me what the hell do you do to UV map your object? I've been searching for hours and none of the documentation I checked explained what to do.
Answer by straydogstrut · Mar 30, 2010 at 06:56 AM
Hi Jaimse,
I don't use .obj much, but I threw one I had lying around into Unity and applied a texture no problem. The normal workflow is to create a material first, unless you already have one from your 3d app, then set its texture and apply the material to the mesh. Don't confuse materials and textures. Materials define how your object looks through properties such as color, texture and which shader you're using. However, Unity usually creates a material for you if you don't have one when you apply a texture straight to an object, so I don't see how you would have problems. Do you see a material in the Project View?
As for changing textures at runtime, look up renderer.material.mainTexture
. The following assigns a texture to the object this script is attached to when it is initialised (myTexture being the texture you've chosen in the Editor):
var myTexture: Texture2D;
function Start(){
renderer.material.mainTexture = myTexture;
}
See the Docs: http://unity3d.com/support/documentation/ScriptReference/Material-mainTexture.html
While the following assigns a texture through code that is stored in the resources folder (example from the Docs):
// Assigns a texture named "Assets/Resources/glass" to a Plane.
function Start () {
var go = new GameObject.CreatePrimitive(PrimitiveType.Plane);
go.renderer.material.mainTexture = Resources.Load("glass");
}
http://unity3d.com/support/documentation/ScriptReference/Resources.Load.html
Hope that helps, sorry I don't have an answer for why your texture isn't showing up.
Answer by krstrobe · Nov 30, 2015 at 06:46 PM
Sometimes when you haven't UV Mapped, the tiling being at 1 is too big. Take it way crazy down, like .01 or .001 and you may be able to see it.