unity3d - 如何通过脚本为材质 alpha 属性设置动画?
问题描述
我正在通过脚本创建 Animator OverrideController,该脚本基于分配给 animator 组件的当前控制器。我已经为位置创建了动画曲线,它可以工作。当我尝试为透明度设置动画(材质颜色 alpha)时,它不起作用。我是否未能正确访问此属性?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationScript : MonoBehaviour {
void Start()
{
AnimationClip clip = new AnimationClip();
AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, transform.position.x, 1.0f, transform.position.x);
clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
curve = AnimationCurve.Linear(0.0f, 1.0f, 1.0f, 0.0f);
clip.SetCurve("", typeof(Material), "_Color.a", curve);
Animator anim = GetComponent<Animator>();
AnimatorOverrideController animatorOverrideController = new AnimatorOverrideController(anim.runtimeAnimatorController);
// "loop" is the name of clip not name of state
animatorOverrideController["loop"] = clip;
anim.runtimeAnimatorController = animatorOverrideController;
}
}
然而,遗留动画组件的类似代码可以工作:
void Start()
{
Animation anim = GetComponent<Animation>();
AnimationCurve curve;
// create a new AnimationClip
AnimationClip clip = new AnimationClip();
clip.legacy = true;
// update the clip to a change color
curve = AnimationCurve.Linear(0.0f, 1.0f, 2.0f, 0.0f);
clip.SetCurve("", typeof(Material), "_Color.a", curve);
// now animate the GameObject
anim.AddClip(clip, clip.name);
anim.Play(clip.name);
}
我错过了什么,为什么第一个脚本不起作用?
解决方案
推荐阅读
- mysql - 从mysql中的csv字符串生成字符串的索引/位置
- python - Python Discord 机器人嵌入
- html - 我可以在 Spring Boot 环境中使用 ckEditor 使用 Thymeleaf 吗?
- swift - AF 没有成员“responseDecodable”与路由器
- mpandroidchart - 尝试在空对象引用 BarChart 上调用虚拟方法 'com.github.mikephil.charting.charts.BarChart.setMarker'
- c# - 使用 Cudafy 获取总 GPU 内存。返回最大 ulong 值
- python - 如何在 Python 中循环并增加 Excel 工作簿公式中的行?
- text - 允许从选定文件创建项目的 Macos 文本编辑器
- javascript - 如何从外部访问 js 包文件中的变量?
- python - 在 jupyter notebook 上显示 selenium webdriver 打开的 url