I have events (actually, event singular) set up in my animation. I have coded in all the the listener functions and it works well 95% of the time. It is that other 5% that is the problem. Occasionally, it seems that the event is heard. Any guesses as to what would cause this?
Here is the code I'm using:
[SpineEvent(dataField: "skeletonAnimation", fallbackToTextField: true)]
public string eventName = "TurnComplete";
public Spine.Unity.SkeletonAnimation skeletonAnimation;
public JellyDirectionSetter directionSetter;
public Spine.Unity.AnimationReferenceAsset stillRight;
public Spine.Unity.AnimationReferenceAsset stillLeft;
public Spine.Unity.AnimationReferenceAsset danceRight;
public Spine.Unity.AnimationReferenceAsset danceLeft;
public Spine.Unity.AnimationReferenceAsset leftToRight;
public Spine.Unity.AnimationReferenceAsset rightToLeft;
public bool animPlaying = false;
public JellyMoveDirection moveDirection = JellyMoveDirection.right;
Spine.EventData eventData;
public LayerMask clickMask;
// Start is called before the first frame update
void Start()
{
eventData = skeletonAnimation.Skeleton.Data.FindEvent(eventName);
skeletonAnimation.AnimationState.Event += HandleAnimationStateEvent;
directionSetter.moveDirection = moveDirection;
if(moveDirection == JellyMoveDirection.right)
{
skeletonAnimation.AnimationState.SetAnimation(0, stillRight, false);
}
else
{
skeletonAnimation.AnimationState.SetAnimation(0, stillLeft, false);
}
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D[] hit2D = Physics2D.GetRayIntersectionAll(ray, Mathf.Infinity, clickMask.value);
if (hit2D.Length > 0)
{
foreach (RaycastHit2D hit in hit2D)
{
Debug.Log(hit.collider.gameObject.name);
if(hit.collider.gameObject == gameObject)
{
Clicked();
return;
}
}
}
}
}
private void HandleAnimationStateEvent(Spine.TrackEntry trackEntry, Spine.Event e)
{
Debug.Log(e.Data.Name);
//bool eventMatch = string.Equals(e.Data.Name, eventName, System.StringComparison.Ordinal); // Testing recommendation: String compare.
bool eventMatch = (eventData == e.Data); // Performance recommendation: Match cached reference instead of string.
if (eventMatch)
{
animPlaying = false;
}
}
There's more, but this is the relevant part. The problem is that, if that event isn't heard, the object in question remains unclickable. Any advice is appreciated.
It's worth noting that this is in play mode in the editor. I don't know if that makes any difference at all.