天天看點

解決AnimationClip.SetCurve RectTransform Color參數 出現Missing!的情況

搬遷原來部落格 海瀾CSDN

在項目開發中有需求動态建立Animationclip設定其中的AnimationCure曲線,但是其中按照官方給出的示例方式設定一些參數的時候會出現Missing丢失的情況
解決AnimationClip.SetCurve RectTransform Color參數 出現Missing!的情況
解決AnimationClip.SetCurve RectTransform Color參數 出現Missing!的情況

經過查找一些資料發現這根他的命名方式可能有關系,如下 這種命名方式第一個字母會自動大寫

解決AnimationClip.SetCurve RectTransform Color參數 出現Missing!的情況
解決AnimationClip.SetCurve RectTransform Color參數 出現Missing!的情況

這種也會保持大寫

解決AnimationClip.SetCurve RectTransform Color參數 出現Missing!的情況
解決AnimationClip.SetCurve RectTransform Color參數 出現Missing!的情況
如下但是這種命名方式會自動把m_剔除掉,然後首字母自動大寫,是以我猜測在unity内部對于組建屬性的命名有些可能是采用 匈牙利命名法 ,這種命名方式再早期C++比較常見
解決AnimationClip.SetCurve RectTransform Color參數 出現Missing!的情況
解決AnimationClip.SetCurve RectTransform Color參數 出現Missing!的情況
是以參數改成匈牙利命名法
解決AnimationClip.SetCurve RectTransform Color參數 出現Missing!的情況
解決AnimationClip.SetCurve RectTransform Color參數 出現Missing!的情況

完美解決~~

附帶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);  
    }  
}  
           

繼續閱讀