けいごのなんとか

Unityユーザーとしてのブログ。ギリギリ路線走ってます。

AnimationClip.SetCurve

この情報はUnity Documentation Tabsでも閲覧することが出来ます

Unity Documentation Tabsで閲覧する場合のURLはこちら(EN)またはこちら(JP)です


propertyNameを知るには

SpriteRendererのColorを操作したい時は下記のコードです。

Colorではなくm_Colorであることに注意してください。

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{
        public AnimationClip clip;

        void Start ()
        {
                AnimationCurve curve = new AnimationCurve (new Keyframe[]{ new Keyframe (0, 1), new Keyframe (1, 0) });
                clip.SetCurve ("", typeof(SpriteRenderer), "m_Color.a", curve);

        }
}

このpropertyNameはSerializedProperty.propertyPathと同じものです。
ですので簡単なビューワーを作成しました。

using UnityEngine;
using UnityEditor;

public class PropertyNamesWindow : EditorWindow
{
        [MenuItem ("Window/PropertyNamesWindow")]
        static void Show ()
        {
                GetWindow<PropertyNamesWindow> ();
        }

        Object obj;
        SerializedObject serializedObject;
        Vector2 scroll;

        void OnGUI ()
        {
                EditorGUI.BeginChangeCheck ();
                obj = EditorGUILayout.ObjectField (obj, typeof(Object), true);

                if (EditorGUI.EndChangeCheck () && obj) {
                        serializedObject =  new SerializedObject (obj);
                } else if (!obj) {
                        serializedObject = null;
                }

                if (serializedObject != null) {
                        SerializedProperty property =   serializedObject.GetIterator ();
                        scroll = EditorGUILayout.BeginScrollView (scroll);

                        while (property.Next (true)) {
                                EditorGUILayout.SelectableLabel (property.propertyPath);
                        }
                        EditorGUILayout.EndScrollView ();
                }
        }
}