Instantiate a prefab and access sub-mesh material
Hélo,
Unity noob here, I may have started with the wrong project :)
I'm trying to create a small lighting simulation app. My projector is made of several meshes. I made a "Projector" prefab that I can instanciate with an Empty Object containing the following script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class projector_instancer : MonoBehaviour {
public GameObject proj_prefab;
private GameObject proj_array;
// Use this for initialization
void Start ()
{
for (float x = 0; x < 10; x++)
{
for (float y = 0; y < 10; y++)
{
// Create the clones
proj_array = Instantiate (proj_prefab, new Vector3 (x * (float)1.5, y * (float)1.5, 0), Quaternion.Euler(90,0,0));
// Rename the clones
proj_array.name = "Projectorl";
}
}
}
}
"proj_prefab" is supplied via Inspector, and contains the meshes making my projector.
Ok, now the tricky part. I would like each of the instantiated prefab to receive two textures, each one on one of the mesh they are made of (these projectors are actully LED matrices on a moving head). Later they also have to be stretched/translated so that each projector displays the part of the texture it's supposed to display.
Thing is : I have no idea how I should provide those textures to my prefab. I guess I'm lacking some Unity knowledge...
should I create a Material public var in my Instancer script, and assign/offset/scale the texture in the same loop that instantiates ?
should I assign my material to my prefab and do the offset/stretch thingy in another script ?
I'm totaly lost here, any help would be greeeeeaaatly appreciated :)
Thanks !
the only variance you seem to be seeking is a different part of the same texture for all instances depending on their position.
this means the prefab can have the material assigned already.
only code you need then is: in the loop, get the meshrenderer of the current instance and change tiling and offset according to coordinates.
a more performance friendly and a little more advanced solution would be using $$anonymous$$aterialPropertyBlocks once you have that.