Use a textured material with Graphics.DrawMesh(...)


I create a blank Unity 3d project, and add a material in Assets/Resources. I keep it by default (it uses the default shader).
I then associate a C# script to the camera object, and draw a simple rectangle -in the XY plane- using that material in the scene.

	void Update () {
        Mesh mesh = new Mesh();
        mesh.vertices = new Vector3[]
            new Vector3(0,0,0),
            new Vector3(0,1,0),
            new Vector3(1,1,0),
            new Vector3(1,0,0)
        mesh.triangles = new int[]


        Graphics.DrawMesh(mesh, new Vector3(), Quaternion.identity, (Material)(Resources.Load("material")), 0, null, 0, null, false, false, false);

When I run the game, I get a white rectangle. If I change the albedo of my material, the color of the rectangle changes… Great…

Back to albedo white, and I hit the small round near it to add a texture (simple .png image).
I can see my texture applied on a sphere in the material previewer…

When I run the game, I get an uniform color on the rectangle, which seems to be the color of the first pixel of my texture…

I tried playing with tiling and offset on the material, but without any effect at all.

I must be missing something simple, but I’ve searched hours without any succes… Your help is welcome, thanks!


Well. I just discovered what I was missing:

        Vector2[] uvs = new Vector2[mesh.vertices.Length];
        for (int i = 0; i < uvs.Length; i++)
            uvs <em>= new Vector2(mesh.vertices_.x, mesh.vertices*.y);*_</em>

mesh.uv = uvs;
Sorry about the newbie question… :wink: