Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace Spine.Unity.Examples
  6. {
  7.     public class SpineEventListener : MonoBehaviour
  8.     {
  9.         public List<spineEventListeners> spineEventListeners = new List<spineEventListeners>();
  10.     }
  11.  
  12.     [System.Serializable]
  13.     public class spineEventListeners
  14.     {
  15.         public SkeletonAnimation skeletonAnimation;
  16.         [SpineEvent(dataField: "skeletonAnimation", fallbackToTextField: true)]
  17.         public string eventName;
  18.  
  19.         public AudioClip[] audioClip;
  20.         public float basePitch = 1f;
  21.         public float randomPitchOffset = 0.1f;
  22.  
  23.        public Spine.EventData eventData;
  24.  
  25.         public void Start()
  26.         {
  27.            
  28.                 if (skeletonAnimation == null) return;
  29.                 skeletonAnimation.Initialize(false);
  30.                 if (!skeletonAnimation.valid) return;
  31.  
  32.                 eventData = skeletonAnimation.Skeleton.Data.FindEvent(eventName);
  33.                 skeletonAnimation.AnimationState.Event += HandleAnimationStateEvent;
  34.          
  35.         }
  36.  
  37.         public void Play()
  38.         {
  39.             Debug.Log("PLAY");
  40.  
  41.             float pitch = basePitch + Random.Range(-randomPitchOffset, randomPitchOffset);
  42.  
  43.             if (audioClip.Length > 0)
  44.             {
  45.                 int i = Random.Range(0, audioClip.Length);
  46.                 GameManager.PlaySound(audioClip[i]);
  47.             }
  48.             else
  49.             {
  50.                 GameManager.PlaySound(audioClip[0]);
  51.  
  52.             }
  53.  
  54.         }
  55.         public void HandleAnimationStateEvent(TrackEntry trackEntry, Event e)
  56.         {
  57.          
  58.                 // if (!enabled) return;
  59.                // Debug.Log("Event fired! " + e.Data.Name);
  60.                 //bool eventMatch = string.Equals(e.Data.Name, eventName, System.StringComparison.Ordinal); // Testing recommendation: String compare.
  61.                 bool eventMatch = (eventData == e.Data); // Performance recommendation: Match cached reference instead of string.
  62.                 if (eventMatch)
  63.                 {
  64.                 Debug.Log("Event fired! " + e.Data.Name);
  65.                 Play();
  66.                 }
  67.            
  68.  
  69.         }
  70.  
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement