思路
找到这个材质球对应的Shader→打开Shader源代码→根据关键字找到想修改的属性→使用Material.SetFloat
这个API修改即可。
操作流程
(假如我想动态修改Reflections 的状态,怎么做呢?)
- 找到Shader:
- 进入Shader源码:
- 获取属性值
- 代码动态控制(为便于观看效果,无脑代码走起!)
using UnityEngine;
public class TestForShader : MonoBehaviour
{
public Material tt;
private bool ifActive = true;
string[] msg = new string[] { "取消Toggle勾选!", "勾选Toggle!" };
private void Start()
{
tt.SetFloat("_GlossyReflections", 0f);//为了便于观察,初始化为未勾选状态
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
int index = ifActive ? 1 : 0;
Debug.Log(msg[index]);
tt.SetFloat("_GlossyReflections", index);
ifActive = !ifActive;
}
}
}
Debug输出以及效果显示:
补充,有些则必须使用EnableKeyword
和DisableKeyword
方法处理,譬如“自发光”。
private void SetEmission(Material mat, bool emissionOn)
{
if (emissionOn)
{
mat.EnableKeyword("_EMISSION");
}
else
{
mat.DisableKeyword("_EMISSION");
}
}