Hey all,
It’s been a while (too long) since I’ve worked on game stuff, and I’m diving straight into something I should have attempted a long time ago: I want to make a custom sprite animation system because dealing with Mechanim has just been such a pain. I know I’m not the first person to come to that decision, and I’ve looked at other people’s posts on Unity Answers/forums to get an idea of what needs to be done. I still have question though.
My idea was to create an AnimationScript that looks like this (so far):
using UnityEngine;
using System.Collections;
public class AnimationScript : MonoBehaviour
{
public Sprite[] frames;
public bool loop = false;
public float speed = 0.13f;
private SpriteRenderer rend;
private int currentFrame;
public bool playing = false;
public void Awake()
{
rend = GetComponent<SpriteRenderer>();
Play();
}
public void Play()
{
StartCoroutine(PlayAnim());
}
private IEnumerator PlayAnim()
{
playing = true;
currentFrame = 0;
rend.sprite = frames[currentFrame];
currentFrame++;
while (currentFrame < frames.Length)
{
yield return new WaitForSeconds(speed);
rend.sprite = frames[currentFrame++];
Debug.Log("frame: " + currentFrame);
}
if (loop)
Play();
else
playing = false;
}
}
So the idea was that any animated sprite would have an AnimationScript member (or, more likely, an array of them for multiple animations). Then, the sprites of each animation would be set in the inspector. Doing it this way doesn’t quite work though because the AnimationScript member shows up as a script object in need of an object reference. How is this done then? Like, a public Vector2 member lets you set the x and y values in the inspector, so what do I need to do to make my AnimationScript behave that way? That’s the current problem with the execution of my plan.
I’m sure there are problems with the plan itself that I’m not seeing right now, but one potential issue I do see is that my method doesn’t actually save animations outside of the object they’re created for. This means that if I accidentally killed a prefab, all of the animations made for it would be gone too, and I’d have to re-assign all of the sprite frames.
Any suggestions? Thanks in advance!