Unity uses the Animation component to make simple animations

There are often dynamic prompts in the game, such as the prompt that the current money is insufficient, usually pops up from the center of the screen, and then slowly floats upwards.
I encountered such a requirement in the game project today. I have done it several times before, but I always forget it. With the update of the unity version, there are always some inexplicable pitfalls in the new version. I will write an article to record it today.
The first component used is Animation, which can make animations.
insert image description here
First create a folder and add an Animation.

Then create a prompt box text and add the Animation component.
insert image description here
Then drag the Animation you just created onto it.

Note: Clicking the Animation component under the project panel cannot add animation.
insert image description here
You need to click on specific objects, such as text, and then add animation.
Here we just need to change the position, add an animation
insert image description here
here we choose the position.

insert image description here
The top column represents the number of frames. Under normal circumstances, 0~60 frames is enough for a cycle.
Click the red recording button in the upper left corner to start recording animation.
At this time, we don't care about the position at frame 0, move to the position at frame 60, and increase the Y coordinate of the text.
Then click the play button, and you will find that the text is moving.
insert image description here
At this point, an animation effect is ready, turn off the recording, click to run normally, and you will see the animation effect on purpose. But there is a pit in the higher version of unity (I use 2019), you need to click on the animation component, then turn on the debug mode, and check Legacy to run normally.
insert image description here
insert image description here
At this time, there is a problem, how to control the hiding of this text, it can’t be kept here all the time, the most stupid way is to write a delay to call Invoke, calculate the animation time, and hide the object.
The advanced method is to use AnimationEvent
insert image description here
to right-click on the top, add event, or click the small icon on the left.
insert image description here
After clicking, there will be an AnimationEvent on the right. Click here to select the script writing method (Text component) bound to the script. The event method needs to be written on this script. At present, I only know this method, and there are other methods in the future. I'm still adding.
insert image description here
insert image description here
complete code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class View: MonoBehaviour
{
    
    
    public AnimationTest animationTest;

    private void Update()
    {
    
    
        if (Input.GetKeyDown(KeyCode.A))
        {
    
    
            animationTest.gameObject.SetActive(true);
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimationTest : MonoBehaviour
{
    
    
    public void EndEvent()
    {
    
    
        this.gameObject.SetActive(false);
    }
}

Supplement: The animation mode I choose here is to play when activated, so as long as the display of the object is controlled, if other modes may call the component to play the animation.

Demo

Guess you like

Origin blog.csdn.net/qq_40629631/article/details/121951439