HandleEventWithAudioExample.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using UnityEngine;
  32. namespace Spine.Unity.Examples {
  33. public class HandleEventWithAudioExample : MonoBehaviour {
  34. public SkeletonAnimation skeletonAnimation;
  35. [SpineEvent(dataField: "skeletonAnimation", fallbackToTextField: true)]
  36. public string eventName;
  37. [Space]
  38. public AudioSource audioSource;
  39. public AudioClip audioClip;
  40. public float basePitch = 1f;
  41. public float randomPitchOffset = 0.1f;
  42. [Space]
  43. public bool logDebugMessage = false;
  44. Spine.EventData eventData;
  45. void OnValidate () {
  46. if (skeletonAnimation == null) GetComponent<SkeletonAnimation>();
  47. if (audioSource == null) GetComponent<AudioSource>();
  48. }
  49. void Start () {
  50. if (audioSource == null) return;
  51. if (skeletonAnimation == null) return;
  52. skeletonAnimation.Initialize(false);
  53. if (!skeletonAnimation.valid) return;
  54. eventData = skeletonAnimation.Skeleton.Data.FindEvent(eventName);
  55. skeletonAnimation.AnimationState.Event += HandleAnimationStateEvent;
  56. }
  57. private void HandleAnimationStateEvent (TrackEntry trackEntry, Event e) {
  58. if (logDebugMessage) Debug.Log("Event fired! " + e.Data.Name);
  59. //bool eventMatch = string.Equals(e.Data.Name, eventName, System.StringComparison.Ordinal); // Testing recommendation: String compare.
  60. bool eventMatch = (eventData == e.Data); // Performance recommendation: Match cached reference instead of string.
  61. if (eventMatch) {
  62. Play();
  63. }
  64. }
  65. public void Play () {
  66. audioSource.pitch = basePitch + Random.Range(-randomPitchOffset, randomPitchOffset);
  67. audioSource.clip = audioClip;
  68. audioSource.Play();
  69. }
  70. }
  71. }