標簽: Unity3D、Material、Shader、Toggle 、Reflections 代碼動態(tài)控制勾選或取消
通過本文你可以學到怎么用代碼控制材質(zhì)球
Material
(實則是Shader
)上的Toggle狀態(tài)
描述一下思路:
找到這個材質(zhì)球?qū)?code>Shader→打開Shader
源代碼→根據(jù)關(guān)鍵字找到想修改的屬性→使用Material.SetFloat這個API修改即可。
實際操作流程(假如我想動態(tài)修改Reflections 的狀態(tài),怎么做呢?)
Step1.找到Shader
:
如何找到Shander,請按圖索驥
Step2. 進入Shader
源碼:
如何進入``Shader``源碼
Step3. 獲取屬性值
通過搜索獲取屬性值
Step4. 代碼動態(tài)控制(為便于觀看效果,無腦代碼走起?。?br> 代碼塊:
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);//為了便于觀察,初始化為未勾選狀態(tài)
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
int index = ifActive ? 1 : 0;
Debug.Log(msg[index]);
tt.SetFloat("_GlossyReflections", index);
ifActive = !ifActive;
}
}
}
Debug輸出以及效果顯示:
點擊鼠標,Toggle開關(guān)切換
補充,有些則必須使用 EnableKeyword
和DisableKeyword
方法處理,譬如 自發(fā)光 。
private void SetEmission(Material mat, bool emissionOn)
{
if (emissionOn)
{
mat.EnableKeyword("_EMISSION");
}
else
{
mat.DisableKeyword("_EMISSION");
}
}
標簽:Unity3D、腳本編程、Sharder初級編程、Material.SetFloat、_GlossyReflections
快捷訪問:
- Unity3D 如何設(shè)置CubeMap
- UGUI Button OnClick事件統(tǒng)一管理
- UGUI Slider onValueChanged事件統(tǒng)一管理
- UGUI Toggle onValueChanged事件統(tǒng)一管理
- Unity3D監(jiān)測按鍵輸入(快捷鍵)的有效方法
- Unity3D Editor模式下的System.IO數(shù)據(jù)訪問
- Unity3D 掛載的腳本取消勾選居然還會運行??!
- Unity 代碼動態(tài)勾選或取消材質(zhì)球上的選項 ←您在這里
- Unity SerializeField序列化字段導致Bug的跳坑筆記