예제 #1
0
    // Deletes the text and recreates it with the new text / color.
    void RefreshText()
    {
        // Delete old text. Refresh all parse variables.
        foreach (GameObject i in letterObjects)
        {
            Destroy(i.gameObject);
        }
        letterObjects.Clear();
        parsingColor      = false;
        parsingWave       = false;
        parsingShake      = false;
        parsingRandoColor = false;
        parsingRbowWave   = false;
        rgb          = new int[] { -1, -1, -1 };
        colorCounter = 0;
        int charactersParsed   = 0;
        int charactersToIgnore = 0;             // Used for word wrapping. Ignores the parsing characters when calculating word length.

        // Parse ease-of-use tags
        while (txt.text.Contains("[color]"))
        {
            StringBuilder tempText    = new StringBuilder(txt.text);
            int           idxStart    = txt.text.IndexOf("[color]");
            int           numToRemove = 6;
            tempText.Remove(idxStart, numToRemove);
            tempText[idxStart] = '┤';
            txt.text           = tempText.ToString();
        }
        while (txt.text.Contains("[wave]"))
        {
            StringBuilder tempText    = new StringBuilder(txt.text);
            int           idxStart    = txt.text.IndexOf("[wave]");
            int           numToRemove = 5;
            tempText.Remove(idxStart, numToRemove);
            tempText[idxStart] = '╡';
            txt.text           = tempText.ToString();
        }
        while (txt.text.Contains("[shake]"))
        {
            StringBuilder tempText    = new StringBuilder(txt.text);
            int           idxStart    = txt.text.IndexOf("[shake]");
            int           numToRemove = 6;
            tempText.Remove(idxStart, numToRemove);
            tempText[idxStart] = '╢';
            txt.text           = tempText.ToString();
        }
        while (txt.text.Contains("[rando]"))
        {
            StringBuilder tempText    = new StringBuilder(txt.text);
            int           idxStart    = txt.text.IndexOf("[rando]");
            int           numToRemove = 6;
            tempText.Remove(idxStart, numToRemove);
            tempText[idxStart] = '╖';
            txt.text           = tempText.ToString();
        }
        while (txt.text.Contains("[rbowwave]"))
        {
            StringBuilder tempText    = new StringBuilder(txt.text);
            int           idxStart    = txt.text.IndexOf("[rbowwave]");
            int           numToRemove = 9;
            tempText.Remove(idxStart, numToRemove);
            tempText[idxStart] = '╕';
            txt.text           = tempText.ToString();
        }

        // Create new text with following process: Parse all commands, Check for unknown symbols, Instantiate an object,
        // parent it to the text box, reset scale (glitch) get the ref to the Image,
        // color it, add it to the letterObjects list for later reference.

        foreach (char i in txt.text)
        {
            // PARSE COMMANDS.

            // Color.
            // Begin parsing.
            if (i == '┤' && !parsingColor)
            {
                //Debug.Log("Begin parsing");
                parsingColor        = true;
                charactersToIgnore += 1;
                continue;
            }
            // Parse color.
            if (parsingColor && colorCounter < 3)
            {
                //Debug.Log("Parsing color");
                rgb[colorCounter] = (int)i - 48;
                colorCounter++;
                charactersToIgnore += 1;
                continue;
            }
            // End parsing.
            if (i == '┤' && parsingColor)
            {
                colorCounter = 0;
                //Debug.Log("Stop parsing");
                parsingColor        = false;
                charactersToIgnore += 1;
                continue;
            }

            // Wave.
            // Begin parsing.
            if (i == '╡' && !parsingWave)
            {
                parsingWave         = true;
                charactersToIgnore += 1;
                continue;
            }
            // End parsing.
            if (i == '╡' && parsingWave)
            {
                parsingWave         = false;
                charactersToIgnore += 1;
                continue;
            }

            // Shake.
            // Begin parsing.
            if (i == '╢' && !parsingShake)
            {
                parsingShake        = true;
                charactersToIgnore += 1;
                continue;
            }
            // End parsing.
            if (i == '╢' && parsingShake)
            {
                parsingShake        = false;
                charactersToIgnore += 1;
                continue;
            }

            // RandoColor
            // Begin parsing.
            if (i == '╖' && !parsingRandoColor)
            {
                parsingRandoColor   = true;
                charactersToIgnore += 1;
                continue;
            }
            // End parsing.
            if (i == '╖' && parsingRandoColor)
            {
                parsingRandoColor   = false;
                charactersToIgnore += 1;
                continue;
            }

            // RainbowWave
            // Begin parsing.
            if (i == '╕' && !parsingRbowWave)
            {
                parsingRbowWave     = true;
                charactersToIgnore += 1;
                continue;
            }
            // End parsing.
            if (i == '╕' && parsingRbowWave)
            {
                parsingRbowWave     = false;
                charactersToIgnore += 1;
                continue;
            }


            // CREATING TEXT.

            // Check for word length issues.
            if (justParsedSpace && i != ' ' && (scrollController == null || !firstRun))
            {
                //Debug.Log(txt.text);
                //Debug.Log("STARTING SPACING BLOCK");
                //Debug.Log("Current Char: " + i + " Is char number: " + charactersParsed);
                // Grab max word length based on rect size.
                // Can also be considered the maximum characters per line.
                float charSize = layout.spacing.x + layout.cellSize.x;
                float width    = txt.rectTransform.rect.width;
                int   max      = (int)(width / charSize);
                //Debug.Log("Max per line: " + max);


                // Grab number of characters remaining in the current line.
                int charsRemaining = 999;
                if (max != 0)
                {
                    charsRemaining = max - ((letterObjects.Count) % max);
                }
                // The line directly below works in most occasions, but sometimes max is 0 for some reason...
                //int charsRemaining = max - ((letterObjects.Count) % max);



                //int charsRemaining = max - ((charactersParsed) % max);
                //int charsRemaining = max - ((charactersParsed + numSpaces) % max);
                //Debug.Log("Chars remaining in line: " + charsRemaining);

                // Grab current word length.
                // Check to make sure the word doesn't start with an invisible character.
                int j = charactersParsed + charactersToIgnore;
                //Debug.Log("Text at " + j + " is: " + txt.text[j] + " or char num: " + (int)(txt.text[j]));

                if (txt.text[j] == '╡' || txt.text[j] == '╢' || txt.text[j] == '╖' || txt.text[j] == '╕')
                {
                    charactersToIgnore += 1;
                    Debug.Log("Pushing parse# up 1");
                }
                if (txt.text[j] == '┤')
                {
                    charactersToIgnore += 4;
                }
                j = charactersParsed + charactersToIgnore;

                // Iterate j up to the end of a word to find its length.
                while (j < txt.text.Length && txt.text[j] != ' ' && (j - charactersParsed) < max)
                {
                    j++;
                }
                int wordLength = j - charactersParsed - charactersToIgnore;

                //Debug.Log("Word Length: " + wordLength + "   Word: " + txt.text.Substring(charactersParsed + charactersToIgnore, wordLength));


                // Force new line by adding spaces if word is too long.
                if (wordLength < max && wordLength > charsRemaining)
                {
                    //Debug.Log("Adding spaces.");
                    for (int k = 0; k < charsRemaining; k++)
                    {
                        GameObject spaceInstance = Instantiate(letterPrefab) as GameObject;                                              // Instantiate letter prefab
                        spaceInstance.name = "SPACE";
                        spaceInstance.transform.SetParent(txt.transform, false);                                                         // Set parent
                        spaceInstance.transform.localScale = new Vector3(1f, 1f, 1f);                                                    // Reset scale due to a strange glitch...
                        Image spaceInstText = spaceInstance.GetComponentInChildren <Image>();                                            // Get the image componenet
                        spaceInstText.color  = txt.color;                                                                                // Set the color by standard means
                        spaceInstText.sprite = sprites[61];                                                                              // Set the sprite
                        spaceInstText.transform.localScale = new Vector3(sizeMultiplierX * fontMult, sizeMultiplierY * fontMult, 1f);    // Set the scale
                        letterObjects.Add(spaceInstance);
                    }
                }
                // Do nothing if word length > max length.
            }

            // Time to actually make the letter we're currently working with.
            // Grab text index.
            int index;
            if (indices.TryGetValue(i.ToString().ToLower()[0], out index) == false)
            {
                Debug.Log("Character not found: " + i);
                continue;                       // Ignore bad character and move on.
            }


            GameObject instance = Instantiate(letterPrefab) as GameObject;                                                      // Instantiate letter prefab
            instance.transform.SetParent(txt.transform, false);                                                                 // Set parent
            instance.transform.localScale = new Vector3(1f, 1f, 1f);                                                            // Reset scale due to a strange glitch...
            Image instText = instance.GetComponentInChildren <Image>();                                                         // Get the image componenet
            instText.color  = txt.color;                                                                                        // Set the color by standard means
            instText.sprite = sprites[index];                                                                                   // Set the sprite
            instText.transform.localScale = new Vector3(sizeMultiplierX * fontMult, sizeMultiplierY * fontMult, 1f);            // Set the scale
            letterObjects.Add(instance);                                                                                        // Add to list



            // POST-PARSE COMMANDS.

            // Apply different color.
            if (rgb[0] != -1 && parsingColor && allowColor)
            {
                //Debug.Log("Setting special color");
                instText.color = new Color((float)(rgb[0] / 9f), (float)(rgb[1] / 9f), (float)(rgb[2] / 9f), 1f);
            }

            // Add wave component.
            if (parsingWave && allowWave)
            {
                TextWave waveScript = instText.gameObject.AddComponent <TextWave>();
                waveScript.SetVars(waveTimeOffset * charactersParsed, wavePower, waveSpeed, (8f * sizeMultiplierY * fontMult));
            }

            // Add shake component.
            if (parsingShake && allowShake)
            {
                TextShake shakeScript = instText.gameObject.AddComponent <TextShake>();
                shakeScript.SetVars(shakePower, 8f * sizeMultiplierY * fontMult);
            }

            // Add randocolor component.
            if (parsingRandoColor && allowRandoColor)
            {
                TextRandColor randColorScript = instText.gameObject.AddComponent <TextRandColor>();
                randColorScript.SetVars(randoColorRGBWeights);
            }

            // Add rbowWave component.
            if (parsingRbowWave && allowRbowWave)
            {
                TextRainbowWave waveScript = instText.gameObject.AddComponent <TextRainbowWave>();
                waveScript.SetVars(rbowWaveTimeOffset * charactersParsed, rbowWaveSpeed);
            }

            // Having just parsed space is a fairly good indicator for where the start of a word may be.
            // This is required so words don't wrap across lines.
            if (i == ' ')
            {
                justParsedSpace = true;
            }
            else
            {
                justParsedSpace = false;
            }

            charactersParsed++;         // Iterate this so effects that rely on the number of characters can function.
        }
        //Debug.Log(letterObjects.Count);

        // SCROLLING TEXT EXTENSION.
        // There is a very strange glitch that happens on the first run of this section of the script based on the way Unity's text object updates its variables.
        // Therefore, this part does not run the first time it is called.
        if (scrollController != null && !firstRun)
        {
            //Debug.Log("Refreshing Scroll.");
            scrollController.PrepNextLine(letterObjects);
        }
        else if (firstRun)
        {
            firstRun = false;
        }
    }
예제 #2
0
    // Deletes the text and recreates it with the new text / color.
    void RefreshText()
    {
        // Delete old text. Refresh all parse variables.
        foreach (GameObject i in letterObjects)
        {
            Destroy(i.gameObject);
        }
        letterObjects.Clear();
        parsingColor      = false;
        parsingWave       = false;
        parsingShake      = false;
        parsingRandoColor = false;
        parsingRbowWave   = false;
        rgb          = new int[] { -1, -1, -1 };
        colorCounter = 0;
        int charactersParsed = 0;

        // Parse ease-of-use tags
        while (txt.text.Contains("[color]"))
        {
            StringBuilder tempText    = new StringBuilder(txt.text);
            int           idxStart    = txt.text.IndexOf("[color]");
            int           numToRemove = 6;
            tempText.Remove(idxStart, numToRemove);
            tempText[idxStart] = '┤';
            txt.text           = tempText.ToString();
        }
        while (txt.text.Contains("[wave]"))
        {
            StringBuilder tempText    = new StringBuilder(txt.text);
            int           idxStart    = txt.text.IndexOf("[wave]");
            int           numToRemove = 5;
            tempText.Remove(idxStart, numToRemove);
            tempText[idxStart] = '╡';
            txt.text           = tempText.ToString();
        }
        while (txt.text.Contains("[shake]"))
        {
            StringBuilder tempText    = new StringBuilder(txt.text);
            int           idxStart    = txt.text.IndexOf("[shake]");
            int           numToRemove = 6;
            tempText.Remove(idxStart, numToRemove);
            tempText[idxStart] = '╢';
            txt.text           = tempText.ToString();
        }
        while (txt.text.Contains("[rando]"))
        {
            StringBuilder tempText    = new StringBuilder(txt.text);
            int           idxStart    = txt.text.IndexOf("[rando]");
            int           numToRemove = 6;
            tempText.Remove(idxStart, numToRemove);
            tempText[idxStart] = '╖';
            txt.text           = tempText.ToString();
        }
        while (txt.text.Contains("[rbowwave]"))
        {
            StringBuilder tempText    = new StringBuilder(txt.text);
            int           idxStart    = txt.text.IndexOf("[rbowwave]");
            int           numToRemove = 9;
            tempText.Remove(idxStart, numToRemove);
            tempText[idxStart] = '╕';
            txt.text           = tempText.ToString();
        }

        // Create new text with following process: Parse all commands, Check for unknown symbols, Instantiate an object,
        // parent it to the text box, reset scale (glitch) get the ref to the Image,
        // color it, add it to the letterObjects list for later reference.

        foreach (char i in txt.text)
        {
            // PARSE COMMANDS.

            // Color.
            // Begin parsing.
            if (i == '┤' && !parsingColor)
            {
                //Debug.Log("Begin parsing");
                parsingColor = true;
                continue;
            }
            // Parse color.
            if (parsingColor && colorCounter < 3)
            {
                //Debug.Log("Parsing color");
                rgb[colorCounter] = (int)i - 48;
                colorCounter++;
                continue;
            }
            // End parsing.
            if (i == '┤' && parsingColor)
            {
                colorCounter = 0;
                //Debug.Log("Stop parsing");
                parsingColor = false;
                continue;
            }

            // Wave.
            // Begin parsing.
            if (i == '╡' && !parsingWave)
            {
                parsingWave = true;
                continue;
            }
            // End parsing.
            if (i == '╡' && parsingWave)
            {
                parsingWave = false;
                continue;
            }

            // Shake.
            // Begin parsing.
            if (i == '╢' && !parsingShake)
            {
                parsingShake = true;
                continue;
            }
            // End parsing.
            if (i == '╢' && parsingShake)
            {
                parsingShake = false;
                continue;
            }

            // RandoColor
            // Begin parsing.
            if (i == '╖' && !parsingRandoColor)
            {
                parsingRandoColor = true;
                continue;
            }
            // End parsing.
            if (i == '╖' && parsingRandoColor)
            {
                parsingRandoColor = false;
                continue;
            }

            // RainbowWave
            // Begin parsing.
            if (i == '╕' && !parsingRbowWave)
            {
                parsingRbowWave = true;
                continue;
            }
            // End parsing.
            if (i == '╕' && parsingRbowWave)
            {
                parsingRbowWave = false;
                continue;
            }


            // CREATING TEXT.

            // Grab text index.
            int index;
            if (indices.TryGetValue(i.ToString().ToLower()[0], out index) == false)
            {
                Debug.Log("Character not found: " + i);
                continue;
            }


            GameObject instance = Instantiate(letterPrefab) as GameObject;                                      // Instantiate letter prefab
            instance.transform.SetParent(txt.transform, false);                                                 // Set parent
            instance.transform.localScale = new Vector3(1f, 1f, 1f);                                            // Reset scale due to a strange glitch...
            Image instText = instance.GetComponentInChildren <Image>();                                         // Get the image componenet
            instText.color  = txt.color;                                                                        // Set the color by standard means
            instText.sprite = sprites[index];                                                                   // Set the sprite
            instText.transform.localScale = new Vector3(sizeMultiplierX, sizeMultiplierY, 1f);                  // Set the scale
            letterObjects.Add(instance);                                                                        // Add to list



            // POST-PARSE COMMANDS.

            // Apply different color.
            if (rgb[0] != -1 && parsingColor && allowColor)
            {
                //Debug.Log("Setting special color");
                instText.color = new Color((float)(rgb[0] / 9f), (float)(rgb[1] / 9f), (float)(rgb[2] / 9f), 1f);
            }

            // Add wave component.
            if (parsingWave && allowWave)
            {
                TextWave waveScript = instText.gameObject.AddComponent <TextWave>();
                waveScript.SetVars(waveTimeOffset * charactersParsed, wavePower, waveSpeed, (8f * sizeMultiplierY));
            }

            // Add shake component.
            if (parsingShake && allowShake)
            {
                TextShake shakeScript = instText.gameObject.AddComponent <TextShake>();
                shakeScript.SetVars(shakePower, 8f * sizeMultiplierY);
            }

            // Add randocolor component.
            if (parsingRandoColor && allowRandoColor)
            {
                TextRandColor randColorScript = instText.gameObject.AddComponent <TextRandColor>();
                randColorScript.SetVars(randoColorRGBWeights);
            }

            // Add rbowWave component.
            if (parsingRbowWave && allowRbowWave)
            {
                TextRainbowWave waveScript = instText.gameObject.AddComponent <TextRainbowWave>();
                waveScript.SetVars(rbowWaveTimeOffset * charactersParsed, rbowWaveSpeed);
            }



            charactersParsed++;         // Iterate this so effects that rely on the number of characters can function.
        }
        //Debug.Log(letterObjects.Count);

        // There is a very strange glitch that happens on the first run of this section of the script based on the way Unity's text object updates its variables.
        // Therefore, this part does not run the first time it is called.
        if (scrollController != null && !firstRun)
        {
            //Debug.Log("Refreshing Scroll.");
            scrollController.PrepNextLine(letterObjects);
        }
        else if (firstRun)
        {
            firstRun = false;
        }
    }