Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.UI;
- public class GameManager : MonoBehaviour //Class names use always PascalCase.
- {
- //Public variables are used in very specific cases, if you need to show in the inspector, use the SerializeField property.
- //Keep this in mind also for methods, only mark them as public, if it is really needed.
- //Also initializing your variables, even to null and/or 0, you will get rid of all the warnings in the unity's console.
- [SerializeField] private List<string> myList = new List<string>();
- [SerializeField] private AudioSource[] soundNotes = null;
- [SerializeField] private GameObject[] noteDot = null;
- [SerializeField] private InputField inputNotes = null;
- //[SerializeField] private string LastCharacterofGetList = null; //Not in use.
- [SerializeField] private float notesDelay = 0f;
- [SerializeField] private int counterx = 0;
- //private List<string> GetList = new List<string>(); //Not in use anymore.
- private void Awake()
- {
- //Use this if you want to keep your methods private, the other way is to make InputfieldToUpperCase a public method
- //and in the InputField object add an "On Value Changed" event, reference this script and call the method.
- //How you want to do it is up to you.
- //This will add a listener to the InputField and whenever you type in it, it will call InputfieldToUpperCase, so you don't have to call it every frame.
- inputNotes.onValueChanged.AddListener(delegate { InputfieldToUpperCase(); });
- }
- void Update()
- {
- //make inputnotes To UpperCase
- //InputfieldToUpperCase();
- if (Input.GetKeyDown(KeyCode.V)) //This is my personal opinion but I think it is much better to use KeyCode instead of a string.
- {
- }
- }
- public void DoSomethingWithNotes()
- {
- //AddInListTheInputNotesEachSeparateLetter(inputNotes.text); //This method doesn't make sense to me (unless you are using it elsewhere).
- //You are adding each character to a list and later you're converting it back to a string.
- //Just use the inputNotes.text inside the ReturnNotes method.
- AddsTheNotesTomyList();
- StartCoroutine(SetActiveWithString(myList));
- }
- //this method adds in a list each letter
- // public void AddInListTheInputNotesEachSeparateLetter(string inputCharacters) //Vague names make things confusing. Changed from "x" to "inputCharacters".
- // {
- // for (int i = 0; i < inputCharacters.Length; i++)
- // {
- // GetList.Add(inputCharacters[i].ToString());
- // }
- // }
- void AddsTheNotesTomyList()
- {
- //Here makes sense to have a local variable for the ReturnNotes, so you just call it once.
- string notes = ReturnNotes();
- //adds the outputName(refers to the each letter in inputnotes field) to the List one by one
- for (int i = 0; i < notes.Length; i++)
- {
- myList.Add(notes[i].ToString());
- }
- for (int i = 0; i < myList.Count; i++)
- {
- //where the list contains #
- if (myList[i].Contains("#"))
- {
- //go to the previous element and merry that together like if
- //element 1 is A and element 2 is #
- //Then it will become A#
- myList[i - 1] = myList[i - 1] + myList[i];
- //This line here just removes the # from the second element(given the example above) after is added to the list.
- myList.RemoveAt(i);
- }
- }
- }
- //Returns the notes without Space
- public string ReturnNotes()
- {
- //string outputName = "";
- string withoutSpace = "";
- //first we use the outputname to add the whole list into one string
- // for (int i = 0; i < GetList.Count; i++)
- // {
- // outputName += GetList[i];
- // }
- //removes the space from the outputName so something like A B C it converts it to ABC
- foreach (var letter in inputNotes.text) //You can use inputNotes.text directly here, having AddInListTheInputNotesEachSeparateLetter method is confusing.
- { //Since we don't make use of the GetList anymore, the outputName and the for loop are not needed.
- if (letter == ' ')
- {
- continue;
- }
- withoutSpace += letter;
- }
- return withoutSpace;
- }
- //this method just makes the Inputfield to UpperCase
- void InputfieldToUpperCase()
- {
- //I really think this check is not needed, you can keep it but I think the messege is clearer and more simple without it.
- inputNotes.text = inputNotes.text.ToUpper();
- // string text = inputNotes.text;
- // if (text != inputNotes.text.ToUpper())
- // {
- // }
- }
- public void Clear()
- {
- //GetList.Clear(); //Not in use anymore.
- myList.Clear();
- counterx = 0;
- }
- IEnumerator SetActiveWithString(List<string> texts)
- {
- for (int i = 0; i < texts.Count; i++)
- {
- switch (texts[i])
- {
- case "C":
- {
- if (noteDot[0].activeSelf)
- {
- noteDot[0].SetActive(false);
- }
- counterx++;
- #if UNITY_EDITOR //https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
- if (counterx > 1)
- {
- Debug.Log("counterx is " + counterx);
- }
- #endif
- yield return PlayNote(soundNotes[0], noteDot[0]);
- break;
- }
- case "D":
- yield return PlayNote(soundNotes[1], noteDot[1]);
- break;
- case "E":
- yield return PlayNote(soundNotes[2], noteDot[2]);
- break;
- case "F":
- yield return PlayNote(soundNotes[3], noteDot[3]);
- break;
- case "G":
- yield return PlayNote(soundNotes[4], noteDot[4]);
- break;
- case "G#":
- yield return PlayNote(soundNotes[5], noteDot[10]);
- break;
- case "A":
- yield return PlayNote(soundNotes[6], noteDot[5]);
- break;
- case "B":
- yield return PlayNote(soundNotes[7], noteDot[6]);
- break;
- case "A#":
- yield return PlayNote(soundNotes[8], noteDot[9]);
- break;
- case "C#":
- yield return PlayNote(soundNotes[9], noteDot[7]);
- break;
- case "F#":
- yield return PlayNote(soundNotes[10], noteDot[8]);
- break;
- }
- }
- yield return null;
- }
- private IEnumerator PlayNote(AudioSource soundNotes, GameObject noteDot)
- {
- soundNotes.Play();
- noteDot.SetActive(true);
- yield return new WaitForSeconds(notesDelay);
- noteDot.SetActive(false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement