Beispiel #1
0
    public void LoadSequences()
    {
        if (this.sequenceObjectFiles == null)
        {
            this.sequenceObjectFiles = CsvLoader.LoadingFromFile(MagicOrchestraUtils.pathToGame2Sequence);
        }

        // Getting the sequence panel
        GameObject         sequencePanel      = gameObject.transform.GetChild(4).gameObject;
        SequenceObjectFile sequenceObjectFile = sequenceObjectFiles[difficultySlider.GetComponent <DifficultySlider>().GetCurrentDifficulty() - difficultySlider.GetComponent <DifficultySlider>().lowDifficulty];


        // Looping on sequences of the target difficulty
        for (int arrayIndex = 0; arrayIndex < sequenceObjectFile.sequences.Count; arrayIndex++)
        {
            // Getting a sequence
            List <int> currentArray = sequenceObjectFile.sequences[arrayIndex];

            if (sequencePanel.transform.childCount != sequenceObjectFile.sequences.Count)
            {
                // Creating a new toggle
                GameObject toggle = Instantiate(togglePrefab, sequencePanel.transform, false);
                toggle.transform.SetParent(sequencePanel.transform);
                toggle.transform.localScale = new Vector3(1.2f, 1.2f, 1.2f);
                toggle.GetComponent <SequenceToggle>().SetIndex(arrayIndex);

                // Setting toggle group
                toggleGroup.GetComponent <ToggleGroup>().RegisterToggle(toggle.GetComponent <Toggle>());
                toggle.GetComponent <Toggle>().group = toggleGroup.GetComponent <ToggleGroup>();

                // In order to leave only one toggle on
                if (arrayIndex != 0)
                {
                    toggle.GetComponent <Toggle>().isOn = false;
                }

                // Creating the visual sequence
                string stringToWrite = "";
                foreach (int number in currentArray)
                {
                    stringToWrite = stringToWrite + number.ToString() + " ";
                }
                toggle.transform.GetChild(1).gameObject.GetComponent <Text>().text = stringToWrite;
            }
            else
            {
                // Creating the visual sequence
                string stringToWrite = "";
                foreach (int number in currentArray)
                {
                    stringToWrite = stringToWrite + number.ToString() + " ";
                }
                sequencePanel.transform.GetChild(arrayIndex).gameObject.transform.GetChild(1).gameObject.GetComponent <Text>().text = stringToWrite;
            }
        }
    }
    public static List <SequenceObjectFile> LoadingFromFile(string pathFromStreamingAssets)
    {
        // Instanciting the sequences list
        List <SequenceObjectFile> sequenceObjectFiles = new List <SequenceObjectFile>();

        // Loading the sequence file
        string path    = Application.streamingAssetsPath + pathFromStreamingAssets;
        string cvsFile = File.ReadAllText(path);

        String[] lines = cvsFile.Split(System.Environment.NewLine[0]);
        // Looping on lines of the file
        // KEEP ATTENTION:
        // - line starts from 1 in order to skip the header line
        // - line arrives before lines.Lenght - 1 because the last line is empty
        for (int line = 1; line < lines.Length - 1; line++)
        {
            // Parsing the file removing the separetors
            String[] lineData = lines[line].Split(","[0]);

            // Creating the Unity Object representing the sequences
            SequenceObjectFile objectFile = new SequenceObjectFile();
            objectFile.sequences = new List <List <int> >();

            // Converting the difficulty
            objectFile.difficulty = int.Parse(lineData[0]);

            // Retriving all the sequences of the target difficulty
            for (int seqID = 1; seqID < lineData.Length; seqID++)
            {
                // Creating the list of a given sequence
                List <int> seqList = new List <int>();

                // Looping on chars to add numbers in the list
                foreach (char singleChar in lineData[seqID])
                {
                    if (singleChar != " "[0])
                    {
                        seqList.Add(int.Parse(singleChar.ToString()));
                    }
                }

                // Adding the sequence to the sequences' list
                objectFile.sequences.Add(seqList);
            }

            // Adding the Object in the Class list
            sequenceObjectFiles.Add(objectFile);
        }

        return(sequenceObjectFiles);
    }