![](/img/trans.png)
[英]Unity Shader Graph - How to Change Parameters — How to change a parameter of a mesh on all objects at the same time?
[英]Unity : How do I change a Shader Graph's property (float) over time?
如下所示,我創建了一個着色器效果並添加了一個名為“Visibility”的 Vector1 屬性,如果設置為 1,則不會發生任何事情,但隨着它的進展為 0,着色器效果開始發揮作用並變得可見。
我的問題是找到一種方法來隨着時間的推移將Visibility屬性從 1 減少到 0,在 2 秒內它應該 go 從 1 到 0。我知道如何使用 SetFloat 通過代碼更改“Visibility”屬性的值,像這樣:
if (door.GetComponent<MeshRenderer>().material = changeDisolve)
{
changeDisolve.SetFloat("Visibility", 0.0f);
}
但我希望該值通過“持續時間”進展到 0,所以我嘗試使用 Mathf.Lerp,如下所示 - 這也不起作用:
if (door.GetComponent<MeshRenderer>().material = changeDisolve)
{
float changeVisibility = Mathf.Lerp(1f, 0f, 3f);
changeDisolve.SetFloat("Visibility", changeVisibility);
}
有誰知道我做錯了什么或如何 go 關於這個? 提前致謝。
如果您想更改每 X 秒調用的代碼,您應該使用InvokeRepeating
。 您也可以在使用InvokeRepeating
OnTriggerEnter()
進入 BoxCollider 或使用OnTriggerExit()
等離開 BoxCollider 時開始 InvokeRepeating ,但不能使用Update
。
您可以使用CancelInvoke();
停止所有InvokeRepeating
調用。 在這種情況下,在您的shaderValue
達到 0 或更少之后。
// Starting at the start of the game after Awake()
// the shader will be changed every 0.2 seconds
public class ExampleScript : MonoBehaviour
{
private shaderValue = 1f;
private float change = 0.1f;
void Start()
{
InvokeRepeating("ChangeShader", 0.1f, 0.2f);
}
void Changeshader()
{
if (shaderValue <= 0f) {
CancelInvoke();
}
else if (door.GetComponent<MeshRenderer>().material = changeDisolve) {
shaderValue -= change;
changeDisolve.SetFloat("Visibility", shaderValue);
}
}
}
您還可以使用IEnumerator
,這使得可以調用WaitForSeconds
,然后等待該數量,並在指定的延遲完成后繼續使用代碼的 rest。
您也可以使用OnTriggerEnter()
或OnTriggerExit()
而不是Start()
。
// Starting in Update
// the shader will be changed every 0.2 seconds
public class ExampleScript : MonoBehaviour
{
private shaderValue = 1f;
private float change = 0.1f;
private float delay = 0.2f;
void Start()
{
StartCouroutine(ChangeShader());
}
IEnumerator Changeshader()
{
while (shaderValue > 0f){
yield return new WaitForSeconds(delay);
if (door.GetComponent<MeshRenderer>().material = changeDisolve) {
shaderValue -= change;
changeDisolve.SetFloat("Visibility", shaderValue);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.