Hello,
I am trying to generate a Texture in runtime in Unity and set it to the mesh.
But it does not work.
The texture dont show up (?).
Can anybody help me?
My code:
GameObject ekranprawy = GameObject.Find("ekranprawy");
// print(" znalaa " + ekranprawy.ToString());
MeshFilter viewedModelFilter = (MeshFilter)ekranprawy.gameObject.GetComponent();
// print(" znalaa2 " + viewedModelFilter.ToString());
print("mesh ma " + viewedModelFilter.sharedMesh.vertexCount);
MeshRenderer rr = (MeshRenderer)ekranprawy.GetComponent();
// Material yourMaterial = (Material)Resources.Load("sciany.mtl", typeof(Material));
print("render ma " + rr.materials.Length + " matow");
Texture2D tex = new Texture2D(256, 256);
for (int y = 0; y < 256; ++y)
{
for (int x = 0; x < 256; ++x)
tex.SetPixel(x, y, new Color());
}
for (int y = 0; y < 256; ++y)
{
for (int x = 0; x < 256; ++x)
tex.SetPixel(x,y,
new Color(Mathf.Sin(x * y), 0, 0));
}
// yourMaterial.mainTexture = tex;
// rr.sharedMaterial = yourMaterial;
Material mta = new Material(Shader.Find("Diffuse"));
rr.sharedMaterial = mta;
rr.sharedMaterial.mainTexture = tex;
rr.material = mta;
↧