简体   繁体   English

Unity:如何随时间更改 Shader Graph 的属性(浮动)?

[英]Unity : How do I change a Shader Graph's property (float) over time?

As seen below, I created a shader effect and added a Vector1 property called "Visibility", where if set to 1 nothing happens but as it progresses to 0 the shader effect comes into play and becomes visible.如下所示,我创建了一个着色器效果并添加了一个名为“Visibility”的 Vector1 属性,如果设置为 1,则不会发生任何事情,但随着它的进展为 0,着色器效果开始发挥作用并变得可见。

enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

My issue is finding a way to reduce the Visibility property from 1 to 0 over time, in the span of 2 seconds it should go from 1 to 0. I know how to change the value of the "Visibility" property through code using SetFloat, like this:我的问题是找到一种方法来随着时间的推移将Visibility属性从 1 减少到 0,在 2 秒内它应该 go 从 1 到 0。我知道如何使用 SetFloat 通过代码更改“Visibility”属性的值,像这样:

 if (door.GetComponent<MeshRenderer>().material = changeDisolve)
     {
         changeDisolve.SetFloat("Visibility", 0.0f);
     }

But i want the value to progress to 0 via a 'duration', so i tried using Mathf.Lerp as seen below-which did not work either:但我希望该值通过“持续时间”进展到 0,所以我尝试使用 Mathf.Lerp,如下所示 - 这也不起作用:

 if (door.GetComponent<MeshRenderer>().material = changeDisolve)
     {
         float changeVisibility = Mathf.Lerp(1f, 0f, 3f);
         changeDisolve.SetFloat("Visibility", changeVisibility);
     }

Does anyone know what i am doing wrong or how to go about this?有谁知道我做错了什么或如何 go 关于这个? Thanks in advance.提前致谢。

If you want to change the code to be called each X Seconds you should use InvokeRepeating .如果您想更改每 X 秒调用的代码,您应该使用InvokeRepeating You could also start InvokeRepeating the moment you enter a BoxCollider with OnTriggerEnter() or the moment you leave a BoxCollider with OnTriggerExit() etc, but you can't use Update .您也可以在使用InvokeRepeating OnTriggerEnter()进入 BoxCollider 或使用OnTriggerExit()等离开 BoxCollider 时开始 InvokeRepeating ,但不能使用Update

You can use CancelInvoke();您可以使用CancelInvoke(); to stop all InvokeRepeating calls.停止所有InvokeRepeating调用。 In this case after your shaderValue has reached 0 or less.在这种情况下,在您的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);
         }
    }
}

You could also use an IEnumerator , which makes it possible to call WaitForSeconds which will then wait that amount and after the specified delay has been finished continue with the rest of the code.您还可以使用IEnumerator ,这使得可以调用WaitForSeconds ,然后等待该数量,并在指定的延迟完成后继续使用代码的 rest。

You could also use OnTriggerEnter() or OnTriggerExit() instead of Start() .您也可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM