In Unity to achieve animation forward and reverse playback code
- 2021-01-06 00:43:02
- OfStack
using UnityEngine;
using System.Collections;
public class AnimationAntiSowing : MonoBehaviour {
public static AnimationAntiSowing _initialise;
void Awake()
{
_initialise = this;
}
/// <summary>
/// The animation plays forward and backward
/// </summary>
/// <param name="gameObject"> Object that needs to be animated </param>
/// <param name="isZhengOrFan"> The playback state of the animation </param>
/// <param name="animation"> The animation state </param>
/// <returns></returns>
public bool isPositiveAndNegativePlay(GameObject gameObject,bool isZhengOrFan,AnimationState animation)
{
// Determines the playback state of the incoming animation
//---- The state passed in is true , indicating normal play
//---- The state passed in is false , indicating that the animation can be played back
if(!isZhengOrFan)
{
// Play an animation when no animation is playing
if(!gameObject.animation.isPlaying)
{
gameObject.animation.Play();
//----- Changes the playback state of the animation -----
// The current time of the animation is set to initialize
animation.time = 0f;
// The animation is played at normal speed
animation.speed = 1.0f;
// Change the incoming bool The value of the variable
isZhengOrFan = true;
}
}
else
{
// Play an animation when no animation is playing
if (!gameObject.animation.isPlaying)
{
gameObject.animation.Play();
//----- Changes the playback state of the animation -----
// The current time of the animation is set to the length of the animation
animation.time = animation.length;
// Go backwards, that is, the animation is played backwards, from the end to the start
animation.speed = -1.0f;
// Change the incoming bool The value of the variable
isZhengOrFan = false;
}
}
return isZhengOrFan;
}
}
That's all for this article, I hope you enjoy it.