我在 Unity 中有一个带有动画资产的场景。动画资源是一种自定义预制效果,其中嵌套了多个动画对象。资源循环运行,所有对象都使用 shadergraph 分配一个 PBR 着色器。一些资产最终会消失,而另一些资产则会消失。我可以通过禁用对象来控制对象何时在时间线上消失。但其他人需要逐渐消失。我怀疑我可以通过在动画剪辑中的特定时间点更改 PBR 着色图材质的 alpha 来淡出这些对象。谁能建议有关如何淡出对象的过程或教程链接,从动画剪辑中的特定时间点开始,并设置对象完全不可见时所需的持续时间?
1 回答
1
为了实现你想要的,你需要在AnimationEvent
你的动画中添加一个。
您可以使用在动画窗口属性中找到的矩形符号来做到这一点。
您现在可以使用它AnimationEvent
来调用脚本中的函数,该函数将使对象淡出。
还要确保将您希望将对象淡化为 afloat
并将当前淡化GameObject
为a 的时间传递Object
到函数中。
动画事件函数:
public void FadeOutEvent(float waitTime, object go) {
// Get the GameObject fromt the recieved Object.
GameObject parent = go as GameObject;
// Get all ChildGameObjects and try to get their PBR Shader
List<RPBShader> shaders = parent
.GetComponentsInChildren(typeof(RPBShader)).ToList();
// Call our FadeOut Coroutine for each Component found.
foreach (var shader in shaders) {
// If the shader is null skip that element of the List.
if (shader == null) {
continue;
}
StartCoroutine(FadeOut(waitTime, shader));
}
}
RPBShader
您想要获取的组件的类型。
要随着时间的推移淡出对象,我们需要使用IEnumerator
.
淡出协程:
private IEnumerator FadeOut(float waitTime, RPBShader shader) {
// Decrease 1 at a time,
// with a delay equal to the time,
// until the Animation finished / 100.
float delay = waitTime / 100;
while (shader.alpha > 0) {
shader.alpha--;
yield return new WaitForSeconds(delay);
}
}
shader.alpha
将是您想要减少的当前对象 PBR 着色器 Alpha 值。
于 2020-12-09T08:10:50.380 回答