Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Spine.Unity.Examples
- {
- public class SpineEventListener : MonoBehaviour
- {
- public List<spineEventListeners> spineEventListeners = new List<spineEventListeners>();
- }
- [System.Serializable]
- public class spineEventListeners
- {
- public SkeletonAnimation skeletonAnimation;
- [SpineEvent(dataField: "skeletonAnimation", fallbackToTextField: true)]
- public string eventName;
- public AudioClip[] audioClip;
- public float basePitch = 1f;
- public float randomPitchOffset = 0.1f;
- public Spine.EventData eventData;
- public void Start()
- {
- if (skeletonAnimation == null) return;
- skeletonAnimation.Initialize(false);
- if (!skeletonAnimation.valid) return;
- eventData = skeletonAnimation.Skeleton.Data.FindEvent(eventName);
- skeletonAnimation.AnimationState.Event += HandleAnimationStateEvent;
- }
- public void Play()
- {
- Debug.Log("PLAY");
- float pitch = basePitch + Random.Range(-randomPitchOffset, randomPitchOffset);
- if (audioClip.Length > 0)
- {
- int i = Random.Range(0, audioClip.Length);
- GameManager.PlaySound(audioClip[i]);
- }
- else
- {
- GameManager.PlaySound(audioClip[0]);
- }
- }
- public void HandleAnimationStateEvent(TrackEntry trackEntry, Event e)
- {
- // if (!enabled) return;
- // Debug.Log("Event fired! " + 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)
- {
- Debug.Log("Event fired! " + e.Data.Name);
- Play();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement