Animator custom animation playback

Animator is a very powerful animation control tool, and there are many basic online tutorials.
In most cases, especially for the editing of character actions, we usually use the Animator connection method to realize the playback of the action when editing the animation, like this: Insert picture description here
I hope to share another very practical way here. His layout may be In this way:
Insert picture description here
there are not too many lines and excesses between animation clips. In some cases, our animations are for a certain object. But the animation clips don’t need too much connection\continuity. At this time, I recommend using this method of direct playback and connection.
The benefits are as follows:
1. You can jump directly to the segment you want to play as you like.
2. Don't control too many states and branches.
3. With the combination of jump and connection, action modification and insertion are also very flexible.
The disadvantages are as follows:
1. The relationship between animations in the view is not obvious.
2. The combination of complex animation clips cannot be realized
. 3. The animation is excessively missing.

I personally think that this method is not a strongly recommended method, so it is just another way to choose. Here are a few code operations for your reference:
play animation:

protected void _PlayAnim(Animator anim, string animName)
{
    
    
	if (anim != null)
	{
    
    
    	anim.enabled = true;
    	anim.Play(animName);
    }
    else
    {
    
    
	    Debug.Log("StepHandleBase PlayAnim is error");
    }
}

Progress monitoring
variables:

private Dictionary<string, AnimPragressStruct> m_dicAnimMonitoring;
protected int m_step;
protected AnimatorStateInfo m_animatorInfo;

structure:

	/// <summary>
    /// 监控动画结构
    /// </summary>
    public class AnimPragressStruct
    {
    
    
        public Animator aminator;
        public string name;
        public string keyName;
        public int step;
        public float finish;
        public Action callback;
    }

Registration monitoring:

 	protected virtual void _RegisterAnimPragress(Animator anim, string name, int step, float finish, Action action)
    {
    
    
        if (anim != null && m_dicAnimMonitoring != null)
        {
    
    
            string keyName = anim.name + name;

            if (!m_dicAnimMonitoring.ContainsKey(keyName))
            {
    
    
                AnimPragressStruct s = new AnimPragressStruct();
                s.aminator = anim;
                s.name = name;
                s.keyName = keyName;
                s.step = step;
                s.finish = finish;
                s.callback = action;

                m_dicAnimMonitoring.Add(keyName, s);
            }
        }
        else
        {
    
    
            Debug.Log("StepHandleBase _RegasterAnimMonitoring is null");
        }
    }

Monitoring placed in Update :
It is mainly monitored through this sentence: aminator.GetCurrentAnimatorStateInfo(0);

 	private void __AnimPragressHandle()
    {
    
    
        if (m_dicAnimMonitoring != null && m_dicAnimMonitoring.Values.Count > 0)
        {
    
    
            Action a = null;
            foreach (AnimPragressStruct s in m_dicAnimMonitoring.Values)
            {
    
    
                if (m_step == s.step && s.aminator != null && s.aminator.enabled)
                {
    
    
                    m_animatorInfo = s.aminator.GetCurrentAnimatorStateInfo(0);
                    if ((m_animatorInfo.normalizedTime >= s.finish) && m_animatorInfo.IsName(s.name) && s.callback != null)
                    {
    
    
                        a = s.callback;
                        break;
                    }
                }
            }
            if (a != null) a();
        }
    }

The calling method is as follows:
1._PlayAnim(animE3, “GB143_Step3”);
Play animation clips in animE3 GB143_Step3
Insert picture description here
2._RegisterAnimMonitoring(animE3, “GB143_Step3”, 31, 1f, __Step32Over);
Registration monitoring progress: GB143_Step3 animation clips, when the step is 31 (Students who don't need to use it can be removed), the progress is 1 (0~1), the method __Step32Over is called when it is time
to share it! !

My mental journey: I
recently went to a game company. The summer vacation is also considered the peak season for games, so I have been busy working overtime recently. I found a lot of tips and didn't come to write a blog. I was very excited to post a picture before and waited until these days before I had time to blog it.

Summary:
In fact, I believe that, like everyone else, I used the connection method to control each animation at the beginning. One or more states can be used to control animation switching and transition animation between animations. However, it is not necessarily important to jump out of the game and the relationship between animations at other times. With the switch between 20 and 30 animations, the entire screen is all lines, and the animation structure that is brought at that time is not clear. It is all kinds of redundancy and difficult to modify, which will affect the whole body. When you want to insert an animation in the middle, you must be extra careful, so I tried to use this method to control.
There is also the monitoring of the completion of an animation. Using this method to monitor the animation can also better brake the progress of the animation. Cooperate with logic to flexibly know at which percentage to switch to another state or call other events.
All in all, sharing this method is not the optimal solution for control, and I hope it will serve as a reference for everyone.

Guess you like

Origin blog.csdn.net/ww1351646544/article/details/88754980