AnimationClip.SetCurve 设置曲线

public void SetCurve(string relativePath, Type type, string propertyName, AnimationCurve curve);

参数说明
relativePath应用给该曲线的游戏物体的路径。relativePath被格式化类似路径,如“root/spine/leftArm”。如果relativePath为空,表示动画剪辑附加的游戏物体。
type被动画的组件的类类型
propertyName被动画的属性的名字或路径
curve动画曲线

描述 :

指定动画曲线来动画特定的属性。

如果曲线为null,曲线将被移除,如果曲线属性已经存在,曲线将被替换。

通常的名称是: “localPosition.x”, “localPosition.y”, “localPosition.z”, “localRotation.x”, “localRotation.y”, “localRotation.z”, “localRotation.w” “localScale.x”, “localScale.y”, “localScale.z”.

  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent(typeof(Animation))]
  4. public class ExampleClass : MonoBehaviour {
  5. public Animation anim;
  6. void Start() {
  7. anim = GetComponent<Animation>();
  8. AnimationCurve curve = AnimationCurve.Linear(0, 1, 2, 3);
  9. AnimationClip clip = new AnimationClip();
  10. clip.legacy = true;
  11. clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
  12. anim.AddClip(clip, "test");
  13. anim.Play("test");
  14. }
  15. }

Material材质属性可以使用shader输出的属性名称制作动画。通常使用的名称是: “_MainTex”, “_BumpMap”, “_Color”, “_SpecColor”, “_Emission”。如何动画化不同材质属性类型:

Float属性: “PropertyName” Vector4 属性: “PropertyName.x”, “PropertyName.y”, “PropertyName.z”, “PropertyName.w” Color 属性: “PropertyName.r”, “PropertyName.g”, “PropertyName.b”, “PropertyName.a” UV 旋转属性:“PropertyName.rotation” UV 偏移和缩放: “PropertyName.offset.x”, “PropertyName.offset.y”, “PropertyName.scale.x”, “PropertyName.scale.y” 对于在同一renderer的多个索引材质,你能想这样添加前缀:“[1]._MainTex.offset.y”

  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent(typeof(Animation))]
  4. public class ExampleClass : MonoBehaviour {
  5. public Animation anim;
  6. void Start() {
  7. anim = GetComponent<Animation>();
  8. AnimationClip clip = new AnimationClip();
  9. clip.legacy = true;
  10. clip.SetCurve("", typeof(Material), "_Color.a", new AnimationCurve(new Keyframe(0, 0, 0, 0), new Keyframe(1, 1, 0, 0)));
  11. clip.SetCurve("", typeof(Material), "_MainTex.offset.x", AnimationCurve.Linear(0, 1, 2, 3));
  12. anim.AddClip(clip, clip.name);
  13. anim.Play(clip.name);
  14. }
  15. }

属性名可以通过设置资源序列化,可以在编辑器中设置强制文本模式进行查找。该文本文件由编辑器写入,将包含属性名。例如,yaml文件写入的场景对象将包含相机设置。看下面的yaml文件。

  1. m_BackGroundColor: {r: .192156866, g: .301960796, b: .474509805, a: .0196078438}
  2. m_NormalizedViewPortRect:
  3. serializedVersion: 2
  4. x: 0
  5. y: 0
  6. width: 1
  7. height: 1
  8. near clip plane: .300000012
  9. far clip plane: 1000
  10. field of view: 60
  11. orthographic: 0
  12. orthographic size: 5
  13. m_Depth: -1

这个显示FOV参数的名为field of view。如果你想要创建一个动画剪辑来动画相机的field of view,你应该传递field of view作为参数。

?