[Solved] Animation SetCurve anchoredPosition problem

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 :smile: )

I believe "m_AnchoredPosition.y" should work.

Here's how I got that string:
According to the Unity Docs here:
https://docs.unity3d.com/ScriptReference/AnimationClip.SetCurve.html

At the very bottom is shows we can find a way to get the correct Property Names.
Go to Edit->Project Settings->Editor
Change Asset Serialization from mixed to Force Text.
Now Easiest thing to do is Create a Button on your Scene and call it MyButton. Set its RectTransform to top left (Do NOT hold shift or alt). This will make sure its anchored positions (the x,y) are changed from zero, so get saved into the scene file).

If you go into the Assets folder of your scene file, you should see SceneName.Unity If you edit this file with a text editor you can search for MyButton and find the RectTransform. You should see the names of all the Properties listed there. m_AnchoredPosition is one (note the captial A).

2 Likes

thanks for teaching how to find out them by myself. I felt stupid right now. I tried “m_anchoredPosition.y” many times and it didn’t work, because I should have used capital “A” for anchored -_- Anyway, here is the correct code;

            AnimationCurve curve = new AnimationCurve();
            AnimationClip clip = new AnimationClip();
            clip.legacy = true;
          
            curve = AnimationCurve.Linear(0f, 0f, 1f, -275);

            clip.SetCurve("", typeof(RectTransform), "m_AnchoredPosition.y", curve);

            anim.AddClip(clip, "ScoreAnimation");
            anim.Play("ScoreAnimation");

For others doing same mistakes as I do, capitalization is important at this matter and write anchoredPosition.y exactly as “m_AnchoredPosition.y”

I wouldn't feel stupid. The docs show localPosition.x as a property name that is valid, yet the method I showed above on regular transforms shows m_LocalPosition.x. Clearly they added an extra property name to match the variable name in docs. I bet " m_LocalPosition.x" would work as well. This system was probably all implemented before they added RecTransforms, and they never bothered to go back and add the shortcuts for them. So the obvious "anchoredPosition.y" never ended up working.