Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7.  
  8. public class AutoPlay : MonoBehaviour
  9. {
  10.     [SerializeField]
  11.     private AudioSource source { get { return GetComponent<AudioSource>(); } }
  12.  
  13.     [SerializeField]
  14.     private EventSystem eventSystem;
  15.  
  16.     private readonly int[] temp = { 0, 0, 1, 1, 2, 2, 1, 3, 3,
  17.                             4, 4, 5, 5, 0, 1, 1,
  18.                             3, 3, 4, 4, 5, 1, 1,
  19.                             3, 3, 4, 4, 5, 0, 0,
  20.                             1, 1, 2, 2, 1, 3, 3,
  21.                             4, 4, 5, 5, 0 };
  22.  
  23.  
  24.  
  25.     private List<float> _time;
  26.     private int index;
  27.     private Coroutine coroutine;
  28.  
  29.     private void Awake()
  30.     {
  31.         _time = new List<float>();
  32.         index = 0;
  33.         var timeLine = Resources.Load("Characters/Levels/level1").ToString().Split('\n');
  34.        
  35.         for (int i = 0; i < timeLine.Length; i++)
  36.         {
  37.             _time.Add((float)System.Convert.ToDouble(timeLine[i]));
  38.         }
  39.     }
  40.  
  41.     private void Start()
  42.     {
  43.         gameObject.AddComponent<AudioSource>();
  44.     }
  45.     private void OnMouseDown()
  46.     {
  47.         Debug.Log("Auto play begin");
  48.         StartMelody();
  49.  
  50.         GetComponent<SpriteRenderer>().color = Color.black;
  51.     }
  52.     private void OnMouseUp()
  53.     {
  54.         GetComponent<SpriteRenderer>().color = Color.white;
  55.     }
  56.  
  57.     private void StartMelody()
  58.     {
  59.         if (coroutine == null)
  60.         {
  61.             coroutine = StartCoroutine(Playing());
  62.         }
  63.         else
  64.         {
  65.             StopCoroutine(coroutine);
  66.             coroutine = null;
  67.         }
  68.     }
  69.  
  70.     IEnumerator Playing()
  71.     {
  72.         while (index < 42)
  73.         {
  74.             source.PlayOneShot(MusicClipContainer.instance.Clip[temp[index]]);
  75.             ExecuteEvents.Execute(ButtonsContainer.instance.Buttons[temp[index]].gameObject, new BaseEventData(eventSystem), ExecuteEvents.submitHandler);
  76.            
  77.             yield return new WaitForSeconds(_time[index]);
  78.             index++;
  79.         }
  80.         StopCoroutine(coroutine);
  81.         Debug.Log("Auto play end");
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement