- Editado
count animation attached to a Spine GameObject
Hello,
i'm trying to access a Spine GameObject trough my script and count how many animations it has? How can i do it?
It is very important for me as i'm currently trying to create a dropdown list in order to let the user select the animation to play through Unity's inspector.
thank you
After hours, i've finally achieve what i'm doing!! Here's the solution in order to select the animation to play for the attached gameObject from a script!
1- Create an AnimationSelectorAttribute.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
//
---
CLEVEREEN
---
//THIS CLASS PURPOSE IS TO CREATE A DROPDOWN LIST IN THE INSPECTOR FOR THE useDisplay.cs. IT MAKES EASIER
//TO CHOOSE AN ANIMATION NAME
//
public class AnimationSelectorAttribute : PropertyAttribute
{
public bool UseDefaultAnimationFieldDrawer = false;
}
2- Create AnimationSelectorPropertyDrawer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Spine;
using Spine.Unity;
/*
---
MADE BY CLEVEREEN
THIS SCRIPT PURPOSE IS TO FIND THE ANIMATIONS ATTACHED OF THE SPINE GAME OBJECT AND CREATE A LIST IN
THE INSPECTOR.
---
*/
[CustomPropertyDrawer(typeof(AnimationSelectorAttribute))]
public class AnimationSelectorPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.String)
{
EditorGUI.BeginProperty(position, label, property);
///generate the Spine Animation List
List<string> spineAnimationList = new List<string>();
spineAnimationList.Add("<None>");
useDisplay useDisplay = Selection.activeGameObject.GetComponent<useDisplay>();//get the component
GameObject objectToActivate_i = useDisplay.objectToActivate; //get the gameobject
SkeletonAnimation spineObjSkelAnim = objectToActivate_i.GetComponent<SkeletonAnimation>(); //get the skeletonMecanim component
int index = -1;
//find every animation in the attached gameObject
foreach(Spine.Animation spineAnimation in spineObjSkelAnim.skeleton.Data.Animations)
{
spineAnimationList.Add(spineAnimation.Name);//store the animation name
}
for(int i = 1; i < spineAnimationList.Count; i++)
{
index = i;
break;
}
////Draw the popup box with the current selected index
index = EditorGUI.Popup(position, label.text, index, spineAnimationList.ToArray());
EditorGUI.EndProperty();
}
else
{
EditorGUI.PropertyField(position, property, label);
}
}
}
3-Now you can use the dropdown list to your script in order to select the animation attached to the gameobject in the inspector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;//allow to use textmeshPro
using Spine.Unity;
using UnityStandardAssets.CrossPlatformInput;//To use the UI button of standard asset
public class useDisplay : MonoBehaviour
{
[AnimationSelector] public string animationToPlay = "";
}
I am sorry to say that you have just reinvented the wheel in this case.
[Note that I also posted the same answer at the other thread here:
[Unity / C#] List available animations on SkeletonAnimator?]
You can use the SpineAnimation
attribute now, so the much more elegant way is as follows:
using UnityEngine;
using Spine.Unity;
public class AnimNameInspector : MonoBehaviour {
[SpineAnimation]
public string listOfAllAnimations;
public SkeletonAnimation otherSkeletonData;
[SpineAnimation(dataField: "otherSkeletonData")]
public string otherSkeletonsAnimations; // dropdown list will be populated with otherSkeletonData's animations.
}
Note that the SpineAnimation
attribute has some more useful entries apart from the dataField:
spine-runtimes/SpineAttributes.cs at 3.7