So I'm making a little tilemap system which consists in a mesh renderer + script that allows me to define which part of the map texture (seen in the material at the bottom-right corner) it will render. Everything works but I'm constantly getting this in the log: "Instantiating mesh due to calling MeshFilter.mesh during edit mode. This will leak meshes. Please use MeshFilter.sharedMesh instead.". If I use sharedMesh, whenever I set the tilePosition for one, it changes for every tile too. Is there a safe way to deal with "instance UVs"? The system is meant to be quite simple, 1 quad per tile and 1 material w/ 1 big texture, I just go and tell which part of the texture I want to render in a specific tile.
Code is as simple as:
private void OnValidate()
{
var uv = new Vector2[]
{
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(1, 1),
};
var tex = GetComponent<MeshRenderer>().sharedMaterial.GetTexture("_MainTex");
if (tex == null)
{
Debug.Log("no tex");
return;
}
var texSize = new Vector2(tex.width, tex.height);
var uvScale = tileSize / texSize;
var uvOffset = tilePosition * tileSize / texSize;
GetComponent<MeshFilter>().mesh.uv = uv
.Select(v => (v + uvOffset / uvScale) * uvScale)
.ToArray();
}