Hi everyone,
I have been trying to create animation during run-time. I got UI element and making animation in editor doesn’t work since resolution can change. If I add a keyframe to lower UI element by -275, it doesn’t go down to exactly desired location when resolution changed. I want to drag that UI element to desired location (between two UI elements). Don’t know if there is a way to add a relative key frame. That’s why decided to make it by code and I got no problem when I use typeof(Transform) and “localPosition.y” while creating curve like this;
Animation anim = scoreText.GetComponent<Animation>();
AnimationCurve curve = new AnimationCurve();
AnimationClip clip = new AnimationClip();
clip.legacy = true;
curve = AnimationCurve.Linear(0f, 0f, 1f, -275);
clip.SetCurve("", typeof(Transform), "localPosition.y", curve);
anim.AddClip(clip, "ScoreAnim");
anim.Play("ScoreAnim");
but this causes problem since this is a UI component and has an anchor point. So, I been trying to create a curve by using typeof(RectTransform) and “anchoredPosition.y” and this is where I get errors. Code is below; (Just changed 8th line.)
Animation anim = scoreText.GetComponent<Animation>();
AnimationCurve curve = new AnimationCurve();
AnimationClip clip = new AnimationClip();
clip.legacy = true;
curve = AnimationCurve.Linear(0f, 0f, 1f, -275);
clip.SetCurve("", typeof(RectTransform), "anchoredPosition.y", curve);
anim.AddClip(clip, "ScoreAnim");
anim.Play("ScoreAnim");
When I use it like this, I get an error at the new animation clip as “Score: Anchored Position.y (Missing!)”
I have searched the forums a lot tried some stuff like using “m_anchoredPosition.y” (don’t know why someone suggested to ad “m_” ) and tried other methods like creating keyFrames as unity manual tells;
Keyframe[] keys;
keys = new Keyframe[2];
keys[0] = new Keyframe(0.0f, 0.0f);
keys[1] = new Keyframe(1.0f, -275);
curve = new AnimationCurve(keys);
clip.SetCurve ("", typeof(RectTransform), "anchoredPosition.x", curve);
But all of them gave same error. So wondering if this is a bug or i am doing something wrong. I m open to other solutions too if I am not doing mistakes at code. (I will probably solve this by using localPosition and some math but it doen’t seem elegant )