Skip to content

Instantly share code, notes, and snippets.

@themangomago
Created October 26, 2023 11:58
Show Gist options
  • Save themangomago/e9757b4fa458174016ce936ea060a6d9 to your computer and use it in GitHub Desktop.
Save themangomago/e9757b4fa458174016ce936ea060a6d9 to your computer and use it in GitHub Desktop.
Test implementation of the custom animator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NbAnimator : MonoBehaviour
{
[System.Serializable]
public class AnimationObject
{
public SpriteRenderer renderer;
public string name;
}
[SerializeField]
private AnimationObject[] animationObjects;
[SerializeField]
private NbAnimationData[] animations;
[SerializeField]
private NbAnimationController animationController;
private Sprite[][] sprites;
private IEnumerator coroutine;
private NbAnimationData currentAnimation;
// Start is called before the first frame update
void Start()
{
Initialize();
}
// Play animation
public bool Play(string animation_name, float offset = 0.0f)
{
if (currentAnimation)
{
if (currentAnimation.name == animation_name)
{
// Already playing this animation
//TODO: maybe we sync the offset here if given
return true;
}
}
// Loop over animations and find the one with animation.animationName corresponding to animation_name
for (int i = 0; i < animations.Length; i++)
{
if (animations[i].animationName == animation_name)
{
currentAnimation = animations[i];
if (coroutine != null)
{
// Stop previous animation if there is one
StopCoroutine(coroutine);
}
coroutine = AnimationIterator(offset);
StartCoroutine(coroutine);
return true;
}
}
return false;
}
public void LoadSprites(byte objectIndex, string texture_name)
{
string filePath = "Textures/" + texture_name;
if (System.IO.File.Exists("Assets/Resources/" + filePath + ".png"))
{
sprites[objectIndex] = Resources.LoadAll<Sprite>(filePath);
animationObjects[objectIndex].renderer.enabled = true;
Debug.Log("Loaded file: " + filePath);
return;
}
Debug.Log("File not found: " + filePath);
}
public void ClearSprites(byte objectIndex)
{
sprites[objectIndex] = null;
animationObjects[objectIndex].renderer.enabled = false;
}
private IEnumerator AnimationIterator(float offset = 0.0f)
{
// TODO: handle offset for network interpolation
float timeElapsed = 0f;
byte currentIndex = 0;
while (timeElapsed < currentAnimation.totalLength && currentAnimation.loopable)
{
if (currentIndex < currentAnimation.keyFrameTimes.Length)
{
if (currentAnimation.keyFrameTimes[currentIndex] <= timeElapsed)
{
AnimationSetFrame(currentAnimation.keyFrameSpriteIndexes[currentIndex]);
currentIndex++;
}
}
yield return new WaitForSeconds(currentAnimation.timeStepping);
timeElapsed += currentAnimation.timeStepping;
if (timeElapsed >= currentAnimation.totalLength)
{
timeElapsed = 0f;
currentIndex = 0;
if (animationController)
{
// Callback anaimation controller - if set
animationController.Callback(currentAnimation.animationName);
}
}
}
}
private void AnimationSetFrame(byte frame)
{
byte i = 0;
foreach (var animation in animationObjects)
{
if (sprites[i] != null)
{
if (frame < sprites[i].Length)
{
animation.renderer.sprite = sprites[i][frame];
}
}
i++;
}
}
private void Initialize()
{
sprites = new Sprite[animationObjects.Length][];
Debug.Log(sprites);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment