Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- public class AutoPlay : MonoBehaviour
- {
- [SerializeField]
- private AudioSource source { get { return GetComponent<AudioSource>(); } }
- [SerializeField]
- private EventSystem eventSystem;
- private readonly int[] temp = { 0, 0, 1, 1, 2, 2, 1, 3, 3,
- 4, 4, 5, 5, 0, 1, 1,
- 3, 3, 4, 4, 5, 1, 1,
- 3, 3, 4, 4, 5, 0, 0,
- 1, 1, 2, 2, 1, 3, 3,
- 4, 4, 5, 5, 0 };
- private List<float> _time;
- private int index;
- private Coroutine coroutine;
- private void Awake()
- {
- _time = new List<float>();
- index = 0;
- var timeLine = Resources.Load("Characters/Levels/level1").ToString().Split('\n');
- for (int i = 0; i < timeLine.Length; i++)
- {
- _time.Add((float)System.Convert.ToDouble(timeLine[i]));
- }
- }
- private void Start()
- {
- gameObject.AddComponent<AudioSource>();
- }
- private void OnMouseDown()
- {
- Debug.Log("Auto play begin");
- StartMelody();
- GetComponent<SpriteRenderer>().color = Color.black;
- }
- private void OnMouseUp()
- {
- GetComponent<SpriteRenderer>().color = Color.white;
- }
- private void StartMelody()
- {
- if (coroutine == null)
- {
- coroutine = StartCoroutine(Playing());
- }
- else
- {
- StopCoroutine(coroutine);
- coroutine = null;
- }
- }
- IEnumerator Playing()
- {
- while (index < 42)
- {
- source.PlayOneShot(MusicClipContainer.instance.Clip[temp[index]]);
- ExecuteEvents.Execute(ButtonsContainer.instance.Buttons[temp[index]].gameObject, new BaseEventData(eventSystem), ExecuteEvents.submitHandler);
- yield return new WaitForSeconds(_time[index]);
- index++;
- }
- StopCoroutine(coroutine);
- Debug.Log("Auto play end");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement