Unity 动态修改材质贴图和发光
2022-08-09 本文已影响0人
114105lijia
UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(fileUrl);
yield return uwr.SendWebRequest();
if(uwr.result == UnityWebRequest.Result.Success)
{
if (type == "1")
{
//直接修改材质
Texture texture = DownloadHandlerTexture.GetContent(uwr);
mesh.material.SetTexture("_MainTex", texture);
}
else if (type == "4")
{
Texture texture = DownloadHandlerTexture.GetContent(uwr);
int count = mesh.materials.Length;
if (count > 0)
{
Material target = mesh.materials[count - 1];
//修改材质贴图
target.mainTexture = texture;
//修改自发光贴图
target.SetTexture("_EmissionMap", texture);
}
}
}
else
{
Debug.LogError(uwr.error);
}
uwr.Dispose();
其他属性修改:
//启用自发光效果的代码是
material.EnableKeyword("_EMISSION");
//关闭自发光效果的代码是
material.DisableKeyword("_EMISSION");
//修改发光颜色
material.DisableKeyword.SetColor("_EmissionColor", new Color(0,1,1));
//修改材质颜色
material.color=Color.red;
//修改材质颜色
material.SetColor("_TintColor",color);