在模型更改或者需要大量上材质球时,为了节省时间我们使用动态加载的方式,将代码挂在场景中的某一物体上。将材质球和贴图放在Resources文件夹下的TexPrefix文件夹中,在Inspector中设定贴图的格式化名字。Albedo, Metallic, Normal, Emission;
运行场景。你就会发现贴图自动贴好了。
public Material[] Mats;
public string Folder = "Material_Textures";
public string TexPrefix;
public string Albedo, Metallic, Normal, Emission;
private void Start()
{
for (int i = 0; i < Mats.Length; i++)
{
Mats[i].color = Color.white;
Mats[i].SetTexture("_MainTex", Resources.Load(Folder + "/" + TexPrefix + Mats[i].name + Albedo) as Texture);
Mats[i].SetTexture("_MetallicGlossMap", Resources.Load(Folder + "/" + TexPrefix + Mats[i].name + Metallic) as Texture);
Mats[i].SetTexture("_BumpMap", Resources.Load(Folder + "/" + TexPrefix + Mats[i].name + Normal) as Texture);
Mats[i].SetTexture("_Emission", Resources.Load(Folder + "/" + TexPrefix + Mats[i].name + Emission) as Texture);
}
}