Search code examples
c#unity-game-engineitween

Unity UI Elements Move Animation Using iTween


I was using iTween for giving animation to each main menu elements. But animation not get played as per desire thinking, something different is running so I become confused about this for a solution.

I want to play game title, move animation from 1200 units to 0 unit using iTween move animation. But something like this, I was getting a result - it get stopped on -540 units: enter image description here

This kind of code, I have written for UI Image move animation:

iTween.MoveTo (ballTitleObj, iTween.Hash ("x", 0f, "speed", 100f, "delay", 1f));

I know iTween work with Transform component and UI element with work RectTransform but what I require to do for a solution?


Solution

  • First, get the RectTransform of the title:

    this.titleRectTransform = ballTitleObj.GetComponent<RectTransform>();
    

    Then, call iTween.ValueTo, because you don't want to change the transform, you need to change the RectTransform.

    iTween.ValueTo(ballTitleObj, iTween.Hash(
        "from", this.titleRectTransform.anchoredPosition,
        "to", new Vector2(0, this.titleRectTransform.anchoredPosition.y),
        "speed", 100f,
        "delay", 1f,
        "onupdatetarget", this.gameObject, 
        "onupdate", "MoveBallTitle"));
    }
    

    And in the same class that you are calling iTween.ValueTo in, you need to define a MoveBallTitle method that updates the anchoredPosition:

    public void MoveBallTitle(Vector2 position){
        this.titleRectTransform.anchoredPosition = position;
    }
    

    This answer is based off this answer on the Unity Q&A site