要实现你想要的,你需要在你的动画中添加一个AnimationEvent。
您可以使用在动画窗口属性中找到的Rectangle Symbol 来做到这一点。
您现在可以使用 AnimationEvent 在脚本中调用一个函数,该函数将使对象淡出。
还要确保将您希望将对象淡化为float 和当前GameObject 作为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 值。