/// <summary> /// Gets the custom event of an animation on a certain frame. /// </summary> /// <returns> /// The event of the specified animation on the selected frame. Null if not found. /// </returns> public SpriteAnimatorEvent GetCustomEvent(string animation, int frame) { if (animations.Count == 0) { return(null); } SpriteAnimation anim = GetAnimation(animation); if (anim == null || anim.FramesCount <= frame) { return(null); } SpriteAnimatorEventInfo eventInfo = new SpriteAnimatorEventInfo(animation, frame); if (customEvents.ContainsKey(eventInfo)) { return(customEvents[eventInfo]); } else { return(null); } }
private void CheckEvents() { if (customEvents != null) { SpriteAnimatorEventInfo frameInfo = new SpriteAnimatorEventInfo(currentAnimation, frameIndex); if (customEvents.ContainsKey(frameInfo)) { customEvents[frameInfo].Invoke(this); } } }
private void CheckEvents() { if (currentAnimation.Events != null) { SpriteAnimatorEventInfo frameInfo = new SpriteAnimatorEventInfo(currentAnimation, frameIndex); var triggeredEvent = currentAnimation.Events.Find((x) => x.eventFrame == frameIndex); if (triggeredEvent != null) { triggeredEvent.Invoke(this); } } }
private void Update() { // We do nothing if FPS <= 0 if (framesPerSecond <= 0) { return; } if (playing) { if (!ignoreTimeScale) { animationTimer += Time.deltaTime; } else { animationTimer += Time.unscaledDeltaTime; } if (1f / framesPerSecond < animationTimer) { // Next Frame! frameDurationCounter++; ChangeFrame(currentAnimation.GetFrame(animationIndex)); SpriteAnimatorEventInfo frameInfo = new SpriteAnimatorEventInfo(currentAnimation.Name, animationIndex); if (customEvents != null && customEvents.ContainsKey(frameInfo)) { customEvents[frameInfo].Invoke(this); } animationTimer = 0; if (frameDurationCounter >= currentAnimation.FramesDuration[animationIndex]) { // Change frame only if have passed the desired frames animationIndex = (backwards) ? animationIndex - 1 : animationIndex + 1; frameDurationCounter = 0; } if (animationIndex >= framesInAnimation) { // Last frame, reset index and stop if is one shot animationIndex = (backwards) ? framesInAnimation - 1 : 0; onFinish.Invoke(); if (oneShot) { Stop(); } } } } }
/// <summary> /// Adds a custom event to specified animation on a certain frame. /// </summary> /// <returns> /// The event created. Null if the animation is null or doesn't have enough frames. /// </returns> public SpriteAnimatorEvent AddCustomEvent(SpriteAnimation animation, int frame) { if (animation == null || animation.FramesCount <= frame) { return(null); } SpriteAnimatorEventInfo eventInfo = new SpriteAnimatorEventInfo(animation, frame); if (customEvents == null) { customEvents = new Dictionary <SpriteAnimatorEventInfo, SpriteAnimatorEvent>(); } if (!customEvents.ContainsKey(eventInfo)) { customEvents.Add(eventInfo, new SpriteAnimatorEvent()); } return(customEvents[eventInfo]); }
private void Update() { // We do nothing if FPS <= 0 if (currentAnimation == null || currentAnimation.FPS <= 0 || !playing) { return; } if (!ignoreTimeScale) { animationTimer += Time.deltaTime; } else { animationTimer += Time.unscaledDeltaTime; } if (!waitingLoop && 1f / currentAnimation.FPS < animationTimer) { // Check frames duration frameDurationCounter++; animationTimer = 0; // Double check animation frame index if (frameIndex > framesInAnimation - 1 || frameIndex < 0) { Restart(); } if (frameDurationCounter >= currentAnimation.FramesDuration[frameIndex]) { // Change frame only if have passed the desired frames frameIndex = (backwards) ? frameIndex - 1 : frameIndex + 1; frameDurationCounter = 0; // Check last or first frame if (frameIndex > framesInAnimation - 1 || frameIndex < 0) { // Last frame, reset index and stop if is one shot onFinish.Invoke(); if (oneShot) { Stop(); return; } else { if (!waitingLoop) { waitingLoop = true; loopTimer = 0; // Check delay between loops if (delayBetweenLoops) { SetActiveRenderer(!disableRendererOnFinish); if (maxDelayBetweenLoops > 0) { loopTimer = Random.Range(minDelayBetweenLoops, maxDelayBetweenLoops); } } } } } else { // Change sprite ChangeFrame(currentAnimation.GetFrame(frameIndex)); } // Check events SpriteAnimatorEventInfo frameInfo = new SpriteAnimatorEventInfo(currentAnimation.Name, frameIndex); if (customEvents != null && customEvents.ContainsKey(frameInfo)) { customEvents[frameInfo].Invoke(this); } } } if (waitingLoop) { // Continue looping if enought time have passed loopTimer -= Time.deltaTime; if (loopTimer <= 0) { waitingLoop = false; if (randomAnimation) { // Pick a random animation PlayRandom(oneShot, backwards); return; } else { // Continue playing the same animation SetActiveRenderer(true); frameIndex = (backwards) ? framesInAnimation - 1 : 0; ChangeFrame(currentAnimation.GetFrame(frameIndex)); } } } }