I select a sprite to render on a quad. I use the following code:
public Sprite m_sprite;
void Awake()
{
m_renderer = GetComponent< MeshRenderer >();
m_mesh = GetComponent< MeshFilter >().mesh;
m_material = m_renderer.material;
Rect spriteRect = m_sprite.textureRect;
spriteRect.x /= m_sprite.texture.width;
spriteRect.width /= m_sprite.texture.width;
spriteRect.y /= m_sprite.texture.height;
spriteRect.height /= m_sprite.texture.height;
// Log.Info("spriteRect {0} num UVs {1}", spriteRect, m_mesh.uv.Length);
// m_mesh.uv[0] = new Vector2(spriteRect.x + 0.0f, spriteRect.y + spriteRect.height);
// m_mesh.uv[1] = new Vector2(spriteRect.x + spriteRect.width, spriteRect.y + spriteRect.height);
// m_mesh.uv[2] = new Vector2(spriteRect.x + 0.0f, spriteRect.y + 0.0f);
// m_mesh.uv[3] = new Vector2(spriteRect.x + spriteRect.width, spriteRect.y + 0.0f);
m_mesh.uv[0] = new Vector2(0.0f, 1.0f);
m_mesh.uv[1] = new Vector2(1.0f, 1.0f);
m_mesh.uv[2] = new Vector2(0.0f, 0.0f);
m_mesh.uv[3] = new Vector2(1.0f, 0.0f);
m_material.mainTexture = m_sprite.texture;
}
MeshRenderer m_renderer;
Mesh m_mesh;
Material m_material;
Update 1: as stevethorne pointed out I need the UVs but the texture is scramble even with the default UVs in the code above. Why is that does the shader or material need certain UVs?