解决AnimationClip.SetCurve RectTransform Color参数 出现Missing!的情况

搬迁原来博客海澜CSDN

在项目开发中有需求动态创建Animationclip设置其中的AnimationCure曲线,但是其中按照官方给出的示例方式设置一些参数的时候会出现Missing丢失的情况

经过查找一些资料发现这根他的命名方式可能有关系,如下 这种命名方式第一个字母会自动大写

这种也会保持大写

如下但是这种命名方式会自动把m_剔除掉,然后首字母自动大写,所以我猜测在unity内部对于组建属性的命名有些可能是采用匈牙利命名法,这种命名方式再早期C++比较常见
所以参数改成匈牙利命名法

完美解决~~

附带unity官方API示例
using UnityEngine;  
using System.Collections;  
  
[RequireComponent(typeof(Animation))]  
public class ExampleClass : MonoBehaviour {  
    public Animation anim;  
    void Start() {  
        anim = GetComponent<Animation>();  
        AnimationCurve curve = AnimationCurve.Linear(0.0F, 1.0F, 2.0F, 0.0F);  
        AnimationClip clip = new AnimationClip();  
        clip.legacy = true;  
        clip.SetCurve("", typeof(Transform), "localPosition.x", curve);  
        anim.AddClip(clip, "test");  
        anim.Play("test");  
    }  
}  
// This script example shows how SetCurve() can be used  
using UnityEngine;  
using System.Collections;  
  
public class ExampleClass : MonoBehaviour  
{  
    // Animate the position and color of the GameObject  
    public void Start()  
    {  
        Animation anim = GetComponent<Animation>();  
        AnimationCurve curve;  
  
        // create a new AnimationClip  
        AnimationClip clip = new AnimationClip();  
        clip.legacy = true;  
  
        // create a curve to move the GameObject and assign to the clip  
        Keyframe[] keys;  
        keys = new Keyframe[3];  
        keys[0] = new Keyframe(0.0f, 0.0f);  
        keys[1] = new Keyframe(1.0f, 1.5f);  
        keys[2] = new Keyframe(2.0f, 0.0f);  
        curve = new AnimationCurve(keys);  
        clip.SetCurve("", typeof(Transform), "localPosition.x", curve);  
  
        // update the clip to a change the red color  
        curve = AnimationCurve.Linear(0.0f, 1.0f, 2.0f, 0.0f);  
        clip.SetCurve("", typeof(Material), "_Color.r", curve);  
  
        // now animate the GameObject  
        anim.AddClip(clip, clip.name);  
        anim.Play(clip.name);  
    }  
}  

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 132,673评论 18 139
  • CSS命名规则 头:header内容:content/containe尾:footer导航:nav侧栏:sideb...
    纹小艾阅读 677评论 0 9
  • 1、引言 数据库设计过程中表、字段等的命名规范也算是设计规范的一部分,不过设计规范更多的是为了确保数据库设计的合理...
    SnowflakeCloud阅读 39,945评论 0 48
  • 快速排序基本思想 以 6 1 2 7 9 3 4 5 10 8 按从小到大排序 为例进行说明: 以6为基准,小于6...
    面试小集阅读 198评论 0 0
  • 《冒牌天神1》讲的是主人公埃文·巴特斯特失去了自己的工作,准备自杀时说了一句话,说上帝他不公平,之后上帝就...
    郭轩宇阅读 942评论 1 0