New to Unity, and to coding.
Question: Is it possible to set up a list or an array of animations to play on a game object, that can easily be switched between as game states change? And if so, how?
Current quest:
Make a list or an array of Animators (or AnimatorControllers?) in a scriptable object, which can then be called on via a state machine of my own making via code.
(I know I could do this using Unity’s animation system, but honestly I hate it, it’s quite limiting for what I want to do from my current knowledge base.)
Additional Quest information:
I’m trying to set up a character customization, that allows for changed sprites with changed sprite animations, dependent on what parts are worn/used. This being pixel art, 2D animation style.
the Player Character has 40+ changeable parts (for huge customizability), and I want each to have a lot of variant possible choices to wear, which would require a sprite change (which I know how to do) and an animation change that reflects the sprite change (which I technically know how to do).
What I have so far:
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor.Animations;
using UnityEngine;
using TMPro;
using System;
public class AnimChangerTest : MonoBehaviour
{
//Get variable from Sprite_Manager
//public SpriteRenderer colourPickerSprite;
//public AnimatorController myColourPicker;
public Sprite_Manager ColourPickerDatabase;
public TMPro.TMP_Text AnimCount;
public RuntimeAnimatorController myAnim;
public int selectedAnim = 0;
private void Start()
{
//get component from animator as variable
UpdateSelectedAnim(selectedAnim);
}
//Function to change selected index by +1, and if it goes over the length, reset to 0
public void NextSelectedAnim()
{
selectedAnim++;
if (selectedAnim >= ColourPickerDatabase.ColourPickerCount)
{
selectedAnim = 0;
}
print("UsedTool");
UpdateSelectedAnim(selectedAnim);
}
//Function to change selected index by -1, and if it goes below 0, reset to the length
public void PreviousSelectedAnim()
{
selectedAnim--;
if (selectedAnim < 0)
{
selectedAnim = ColourPickerDatabase.ColourPickerCount - 1;
}
print("UsedTool");
UpdateSelectedAnim(selectedAnim);
}
//the selection update function
private void UpdateSelectedAnim(int selectedAnim)
{
AnimatorControllerManager myAnimPicker = ColourPickerDatabase.GetColourPicker(selectedAnim);
myAnim = myAnimPicker.ColourPicker0;
AnimCount.SetText(selectedAnim.ToString());
}
}
The above is mostly a copy of what I got for changing sprites on spriterenderer game objects, which when a button was pressed, could change even mid-animation.
However, changing animation is not working. Not even with the RuntimeAnimatorController which I… thought would run the animation tied to it.
I have a script for a list of animators (well, currently AnimatorControllers because I thought they would play the animation where Animators did not). This list is then added to the Sprite_Manager (which is a scriptable object) where I can add all the animations/animation controllers I want.
And to pick between them, the script I showed above has a way to shift up and down between 0 and the array.length-1. (Though, for some reason, my button will subtract from 1 to 0, but it won’t go through the rest of the array like it should, and instead says:
I can’t tell what I’m doing wrong with it. As the setup I have for changing out sprites in the sprite editor works just fine and is set up the same way, but with sprite renderers instead of animators/controllers. But this animator changer isn’t working in the same way. And I’m not sure how to get it to play the animations from the array set even if it did work properly.)
Additional info via pics/code snippets:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Animations;
using System;
[CreateAssetMenu(menuName = "Scriptable object/Sprite_Manager")]
public class Sprite_Manager: ScriptableObject
{
//All Character Sprite List as Variables
//Colours -----------------------------------
//Utility
//public AnimatorController ColourPicker0;
public AnimatorControllerManager[] ColourPicker;
public int ColourPickerCount
{
get
{
return ColourPicker.Length;
}
}
public AnimatorControllerManager GetColourPicker(int index)
{
return ColourPicker[index];
}
I’m sure that, being new to all this, I’m doing things exceedingly inefficiently. But I’m trying to learn as best I can. This is the first time I haven’t been able to eventually find what I need from researching to progress my development. And I know I could use the Unity animation system in a way that’d… work… and it’d be faster than what I’m trying to do at the moment. But I guess I’m being kinda stubborn, thinking what I want to do will end up being easier to manage long term so I’m trying my darndest to see if it’s possible.
Bonus Question:
Is it possible to use one sprite sheet to dictate all the animations for a given entity?
To elaborate… I’m also trying to do this with as few animation save files as possible, so I can “hopefully” open just one file per Part (say Bracelet), and do all the edits to the bracelet, and it automatically update the sprites, and the animation loops, without having to open a dozen different files for one single item change.
(Hopefully these questions are suitable. I ‘might’ be exhausted and not thinking straight.)