Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Color-transition animation parting from any color

Discussion in 'Animation' started by Yandrak3, Sep 25, 2019.

  1. Yandrak3

    Yandrak3

    Joined:
    Sep 14, 2019
    Posts:
    2
    I would like to have an animation that transitions a given GameObject (in this case a UI Button) from its original color (whatever it is) into red.

    My use case is the following: I have a canvas full of UI Buttons with a lot of different colors, but at a given point, I want them all to transition smoothly into color red. When creating an animation, I need to define the "source" color, but here comes the issue, because each button will have a completely different "source" color. Thus, if I create the animation with the first keyframe as White (1,1,1), and the second keyframe as Red (1,0,0), as soon as I start the animation, a Blue button will inmediately go to White, and then transition to Red.

    Therefore, my question is, is there any way to create a single animation that will transition from any color into another one? I'm not sure if this may be achievable by code (modifying the keyframes to match the source color), although I think if it was the case, it would be a rather hacky solution.

    Additionally, after some time (let's say 1s), I would need the buttons to go back to their original color.
     
  2. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    You can do that by code, using Color.Lerp() in a coroutine. Needs to write code, but that should do exactly what you want to achieve
     
    Yandrak3 likes this.
  3. Yandrak3

    Yandrak3

    Joined:
    Sep 14, 2019
    Posts:
    2
    I was thinking more of using this within the Animator component, but after looking around, it looks like Color.Lerp() could indeed do the trick. Thanks, I'll give it a try!