Struggling to use an Animator to fade in my canvas
I am trying to have a simple splash screen fade in on my title screen before the main menu shows up. I know I can tie into the main Unity splash screen, but I want more control than that. I figured this would be a good candidate for the Animator and Animation components.
How I imagine the workflow: - Scene starts with a black screen (the buttons and title all at an alpha of 0 and interactions disabled) - Start the main menu song and fade in/fade out the splash screen image over about 5 seconds using an animation, then fade in the buttons and title. - C# scripts queries the Animator to find out when it is complete, then reactivates the buttons so the user can click on the menu items
I can think of a couple simple ways to get what I want to work:
The animator component could have a bool that lets the caller know if it is currently playing an animation or not.
The AnimatorStateInfo returned by GetCurrentAnimatorStateInfo could have a name property on it that I could print out and directly check.
I am going to keep banging away on this, but what I am dong is dirt simple and I am really feeling like this is the animator class is designed for a completely different use case.
Answer by Shadoninja · Apr 23, 2018 at 05:21 PM
Ok I found a workaround, but I still feel like this is a hack. What I have done is made two separate animations. One does all the fading in and out, and one is a one-frame animation that holds the final state of everything. Then in the Animator for the canvas, I have the main animation transition to the "Fade-end" animation at the end. My C# script looks like this:
private const string FadeInCompletedStateName = "Fade-end";
private Animator animator;
private void Start() {
animator = GetComponent<Animator>();
}
private void Update()
{
var animatorState = animator.GetCurrentAnimatorStateInfo(0);
if (animatorState.IsName(FadeInCompletedStateName))
{
// Reactivate buttons here
}
}
There has to be a better way.