Как создать свои Дополнительные теги в Тексте

Общие вопросы о Unity3D

Как создать свои Дополнительные теги в Тексте

Сообщение Orcan 17 сен 2017, 19:10

Привет Всем! Помогите пожалуйста Хочу создать в Тексте тег свой <wc> чтоб он останавливал напечатание символов , до нажатие на Пробел . Для новеллы. Примерный кода * (и конечно же чтобы эти <wc> не были видны в тексте)

Синтаксис:
Используется csharp
 
 public string SText= "Каждый веб-разработчик знает, что такое текст-«рыба».<wc> Текст этот, несмотря на название, не имеет никакого отношения к обитателям водоемов.<wc> Используется он веб-дизайнерами для вставки на интернет-страницы и демонстрации внешнего вида контента<wc>, просмотра шрифтов, абзацев<wc>, отступов и т.д. " ;
 public GameObject TextObj;
  public  void Start()
        {
         
       StartCoroutine ( SpawnWaves3());
         
        }
IEnumerator  SpawnWaves3()  // выводит поочередно символы
{ TextObj.GetComponent<Text>().text = SText;
 
 yield return new WaitForSeconds(1); //через сколько секунд заработает Цикл
     for(int i=0 ; i<  SText.Length+1  ; i++)   //счётчик
        {  if (SText.Substring(0,i) = "<")
            {
                break;
            }
           print(SText.Substring(0,i));
        TextObj.GetComponent<Text>().text =SText.Substring(0,i);
           //выводит колличество i символов за один цикл
        yield return new WaitForSeconds (1); //через сколько повториться  цикл
        }}
void Update()
{
if (Input.GetKeyDown (KeyCode.Space))
{    //продолжение текста после <wc>
}}
 
Orcan
UNITрон
 
Сообщения: 191
Зарегистрирован: 25 сен 2016, 04:22

Re: Как создать свои Дополнительные теги в Тексте

Сообщение samana 18 сен 2017, 16:19

Предлагаю переделать немного логику.
Вам будет удобнее, если просто запускать корутину, передавая ей текст, который будет печататься на экране.
В самой корутине разбивайте этот текст по нужному разделителю и затем уже выводите на экран поочерёдно строки и буквы.

Синтаксис:
Используется csharp
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    public string SText = "Каждый веб-разработчик знает, что такое текст-«рыба».<wc> Текст этот, несмотря на название, не имеет никакого отношения к обитателям водоемов.<wc> Используется он веб-дизайнерами для вставки на интернет-страницы и демонстрации внешнего вида контента<wc>, просмотра шрифтов, абзацев<wc>, отступов и т.д. ";

    private Text _UIText;

    void Start()
    {
        _UIText = GetComponent<Text>();
        StartCoroutine(showText(SText));
    }

    private IEnumerator showText(string text)
    {
        // разбиваем текст на строки заданным разделителем и заносим каждую строку в массив
        string[] textArray = text.Split(new string[] { "<wc>" }, StringSplitOptions.RemoveEmptyEntries);
        int rowCount = 0; // счётчик строк

        // до тех пор, пока все строки не перейдём
        while (rowCount < textArray.Length)
        {
            //счётчик букв в текущей строке
            int lettersCount = 0;
            // до тех пор, пока не выведем все буквы в строке
            while (lettersCount < textArray[rowCount].Length)
            {
                // выводим текст побуквенно
                _UIText.text = textArray[rowCount].Substring(0, lettersCount);
                lettersCount++;
                yield return null;
            }

            // после того, как текст напечатался, ждём нажатие "пробела"
            while (Input.GetKeyUp(KeyCode.Space) == false)
            {
                yield return null;
            }

            //переходим на следующую строку, если она есть.
            rowCount++;
        }

        _UIText.text = "";
        yield return null;
    }
 
}
 
Аватара пользователя
samana
Адепт
 
Сообщения: 4738
Зарегистрирован: 21 фев 2015, 13:00
Откуда: Днепропетровск

Re: Как создать свои Дополнительные теги в Тексте

Сообщение Orcan 20 сен 2017, 06:34

спс Samana, но нужно чтоб он не стирал текст после {wc} а продолжал писать дальше, стирать по другому тегу например {c} Ну а что делать если у меня несколько моих тегов в тексте {wc} - wait clic {music=2} -сиграть музыку в этот момент {fade =1sec} - затемнение экрана на 1 секунду. Я так понимаю такой способ уже не подойдёт c несколькими тегами?
Например так выглядит
Синтаксис:
Используется csharp
 public string SText = "Каждый веб-разработчик знает, что такое текст-«рыба».<wc> Текст этот, несмотря на название, не имеет никакого отношения к обитателям водоемов.<w=3sec> Используется он веб-дизайнерами для вставки на интернет-страницы и демонстрации внешнего вида контента {music=2}, просмотра шрифтов, абзацев{fade =1sec}, отступов и т.д. {c} ";
 
Последний раз редактировалось Orcan 20 сен 2017, 12:13, всего редактировалось 2 раз(а).
Orcan
UNITрон
 
Сообщения: 191
Зарегистрирован: 25 сен 2016, 04:22

Re: Как создать свои Дополнительные теги в Тексте

Сообщение samana 20 сен 2017, 07:13

Orcan писал(а):Ну а что делать если у меня несколько моих тегов

Да, тогда мой способ отпадает.
Возможно тогда при печатании текста побуквенно, необходимо сначала проверить каждое печатаемое слово - не является ли это слово командой, например команда всегда будет либо в фигурных скобках, либо в угловых. И если это слово является командой, то парсить её (расшифровывать).
Парсить можно конечно разными способами, либо методами класса String либо Regex (регулярные выражения). Допустим когда попадаем на знак "{" то останавливаем печатание и ищем индекс закрывающей фигурной скобки в тексте. Затем вырезаем текст который оказался между этими { и }. Узнаём имя команды, это должно быть слово до знака = (равно) . После знака равно рассматриваем слова как параметры метода.
Всё это конечно теория. Мне было бы интересно попробовать написать нечто подобное, но сейчас просто нет времени экспериментировать, поэтому обещать ничего не буду.
А вообще задача заинтересовала.
Аватара пользователя
samana
Адепт
 
Сообщения: 4738
Зарегистрирован: 21 фев 2015, 13:00
Откуда: Днепропетровск

Re: Как создать свои Дополнительные теги в Тексте

Сообщение samana 20 сен 2017, 21:55

Ещё подумал, что можно попробовать использовать рефлексию. Тогда в вашем тексте вы можете прописывать "вызов метода", например
Синтаксис:
Используется csharp
public string SText = "Каждый веб-разработчик знает, что такое текст-«рыба».<WaitClick()>, просмотра шрифтов, абзацев <Fade(1)>".;

Опять же выводя побуквенно текст, и наткнувшись на знак < запускаете анализатор, вытаскиваете команду до первых круглых скобок - имя метода, а затем и параметры метода - всё что в скобках через запятую. Если параметр всегда будет один и числовой, то это проще конечно. А затем рефлексией вызываете этот метод через строковое имя и передаёте параметры в этот метод.
Аватара пользователя
samana
Адепт
 
Сообщения: 4738
Зарегистрирован: 21 фев 2015, 13:00
Откуда: Днепропетровск

Re: Как создать свои Дополнительные теги в Тексте

Сообщение Orcan 21 сен 2017, 13:17

Предыдущий вариант кажется получше(он идеальней кажется)Надо будет посмотреть как делают это правильно в Новелах в кодах покопаться
Orcan
UNITрон
 
Сообщения: 191
Зарегистрирован: 25 сен 2016, 04:22

Re: Как создать свои Дополнительные теги в Тексте

Сообщение Orcan 21 сен 2017, 21:04

Нашёл 3 как минимум скрипта которые это делают. Но для меня пока что как фильме сказано "Здесь что-о на эльфийском "
Вот основная часть
Синтаксис:
Используется csharp
    public enum WriterState
    {
        /// <summary> Invalid state. </summary>
        Invalid,
        /// <summary> Writer has started writing. </summary>
        Start,
        /// <summary> Writing has been paused. </summary>
        Pause,
        /// <summary> Writing has resumed after a pause. </summary>
        Resume,
        /// <summary> Writing has ended. </summary>
        End
    }

    /// <summary>
    /// Writes text using a typewriter effect to a UI text object.
    /// </summary>
    public class Writer : MonoBehaviour, IDialogInputListener
    {
        [Tooltip("Gameobject containing a Text, Inout Field or Text Mesh object to write to")]
        [SerializeField] protected GameObject targetTextObject;

        [Tooltip("Gameobject to punch when the punch tags are displayed. If none is set, the main camera will shake instead.")]
        [SerializeField] protected GameObject punchObject;

        [Tooltip("Writing characters per second")]
        [SerializeField] protected float writingSpeed = 60;

        [Tooltip("Pause duration for punctuation characters")]
        [SerializeField] protected float punctuationPause = 0.25f;

        [Tooltip("Color of text that has not been revealed yet")]
        [SerializeField] protected Color hiddenTextColor = new Color(1,1,1,0);

        [Tooltip("Write one word at a time rather one character at a time")]
        [SerializeField] protected bool writeWholeWords = false;

        [Tooltip("Force the target text object to use Rich Text mode so text color and alpha appears correctly")]
        [SerializeField] protected bool forceRichText = true;

        [Tooltip("Click while text is writing to finish writing immediately")]
        [SerializeField] protected bool instantComplete = true;

        // This property is true when the writer is waiting for user input to continue
        protected bool isWaitingForInput;

        // This property is true when the writer is writing text or waiting (i.e. still processing tokens)
        protected bool isWriting;

        protected float currentWritingSpeed;
        protected float currentPunctuationPause;
        protected Text textUI;
        protected InputField inputField;
        protected TextMesh textMesh;
        protected Component textComponent;
        protected PropertyInfo textProperty;

        protected bool boldActive = false;
        protected bool italicActive = false;
        protected bool colorActive = false;
        protected string colorText = "";
        protected bool sizeActive = false;
        protected float sizeValue = 16f;
        protected bool inputFlag;
        protected bool exitFlag;

        protected List<IWriterListener> writerListeners = new List<IWriterListener>();

        protected StringBuilder openString = new StringBuilder(256);
        protected StringBuilder closeString = new StringBuilder(256);
        protected StringBuilder leftString = new StringBuilder(1024);
        protected StringBuilder rightString = new StringBuilder(1024);
        protected StringBuilder outputString = new StringBuilder(1024);
        protected StringBuilder readAheadString = new StringBuilder(1024);

        protected string hiddenColorOpen = "";
        protected string hiddenColorClose = "";

        protected int visibleCharacterCount = 0;

        protected virtual void Awake()
        {
            GameObject go = targetTextObject;
            if (go == null)
            {
                go = gameObject;
            }

            textUI = go.GetComponent<Text>();
            inputField = go.GetComponent<InputField>();
            textMesh = go.GetComponent<TextMesh>();

            // Try to find any component with a text property
            if (textUI == null && inputField == null && textMesh == null)
            {
                var allcomponents = go.GetComponents<Component>();
                for (int i = 0; i < allcomponents.Length; i++)
                {
                    var c = allcomponents[i];
                    textProperty = c.GetType().GetProperty("text");
                    if (textProperty != null)
                    {
                        textComponent = c;
                        break;
                    }
                }
            }

            // Cache the list of child writer listeners
            var allComponents = GetComponentsInChildren<Component>();
            for (int i = 0; i < allComponents.Length; i++)
            {
                var component = allComponents[i];
                IWriterListener writerListener = component as IWriterListener;
                if (writerListener != null)
                {
                    writerListeners.Add(writerListener);
                }
            }

            CacheHiddenColorStrings();
        }

        protected virtual void CacheHiddenColorStrings()
        {
            // Cache the hidden color string
            Color32 c = hiddenTextColor;
            hiddenColorOpen = String.Format("<color=#{0:X2}{1:X2}{2:X2}{3:X2}>", c.r, c.g, c.b, c.a);
            hiddenColorClose = "</color>";
        }

        protected virtual void Start()
        {
            if (forceRichText)
            {
                if (textUI != null)
                {
                    textUI.supportRichText = true;
                }

                // Input Field does not support rich text

                if (textMesh != null)
                {
                    textMesh.richText = true;
                }
            }
        }
       
        protected virtual void UpdateOpenMarkup()
        {
            openString.Length = 0;
           
            if (SupportsRichText())
            {
                if (sizeActive)
                {
                    openString.Append("<size=");
                    openString.Append(sizeValue);
                    openString.Append(">");
                }
                if (colorActive)
                {
                    openString.Append("<color=");
                    openString.Append(colorText);
                    openString.Append(">");
                }
                if (boldActive)
                {
                    openString.Append("<b>");
                }
                if (italicActive)
                {
                    openString.Append("<i>");
                }          
            }
        }
       
        protected virtual void UpdateCloseMarkup()
        {
            closeString.Length = 0;
           
            if (SupportsRichText())
            {
                if (italicActive)
                {
                    closeString.Append("</i>");
                }          
                if (boldActive)
                {
                    closeString.Append("</b>");
                }
                if (colorActive)
                {
                    closeString.Append("</color>");
                }
                if (sizeActive)
                {
                    closeString.Append("</size>");
                }
            }
        }

        protected virtual bool CheckParamCount(List<string> paramList, int count)
        {
            if (paramList == null)
            {
                UnityEngine.Debug.LogError("paramList is null");
                return false;
            }
            if (paramList.Count != count)
            {
                UnityEngine.Debug.LogError("There must be exactly " + paramList.Count + " parameters.");
                return false;
            }
            return true;
        }

        protected virtual bool TryGetSingleParam(List<string> paramList, int index, float defaultValue, out float value)
        {
            value = defaultValue;
            if (paramList.Count > index)
            {
                Single.TryParse(paramList[index], out value);
                return true;
            }
            return false;
        }

        protected virtual IEnumerator ProcessTokens(List<TextTagToken> tokens, bool stopAudio, Action onComplete)
        {
            // Reset control members
            boldActive = false;
            italicActive = false;
            colorActive = false;
            sizeActive = false;
            colorText = "";
            sizeValue = 16f;
            currentPunctuationPause = punctuationPause;
            currentWritingSpeed = writingSpeed;

            exitFlag = false;
            isWriting = true;

            TokenType previousTokenType = TokenType.Invalid;

            visibleCharacterCount = 0;

            for (int i = 0; i < tokens.Count; ++i)
            {
                // Pause between tokens if Paused is set
                while (Paused)
                {
                    yield return null;
                }

                var token = tokens[i];

                // Notify listeners about new token
                WriterSignals.DoTextTagToken(this, token, i, tokens.Count);
               
                // Update the read ahead string buffer. This contains the text for any
                // Word tags which are further ahead in the list.
                // Обновите буфер строки чтения вперед. Он содержит текст для любого
                 // Теги Word, которые дальше в списке.
                readAheadString.Length = 0;
                for (int j = i + 1; j < tokens.Count; ++j)
                {
                    var readAheadToken = tokens[j];

                    if (readAheadToken.type == TokenType.Words &&
                        readAheadToken.paramList.Count == 1)
                    {
                        readAheadString.Append(readAheadToken.paramList[0]);
                    }
                    else if (readAheadToken.type == TokenType.WaitForInputAndClear)
                    {
                        break;
                    }
                }

                switch (token.type)
                {
                case TokenType.Words:
                    yield return StartCoroutine(DoWords(token.paramList, previousTokenType));
                    break;
                   
                case TokenType.BoldStart:
                    boldActive = true;
                    break;
                   
                case TokenType.BoldEnd:
                    boldActive = false;
                    break;
                   
                case TokenType.ItalicStart:
                    italicActive = true;
                    break;
                   
                case TokenType.ItalicEnd:
                    italicActive = false;
                    break;
                   
                case TokenType.ColorStart:
                    if (CheckParamCount(token.paramList, 1))
                    {
                        colorActive = true;
                        colorText = token.paramList[0];
                    }
                    break;
                   
                case TokenType.ColorEnd:
                    colorActive = false;
                    break;

                case TokenType.SizeStart:
                    if (TryGetSingleParam(token.paramList, 0, 16f, out sizeValue))
                    {
                        sizeActive = true;
                    }
                    break;

                case TokenType.SizeEnd:
                    sizeActive = false;
                    break;

                case TokenType.Wait:
                    yield return StartCoroutine(DoWait(token.paramList));
                    break;
                   
                case TokenType.WaitForInputNoClear:
                    yield return StartCoroutine(DoWaitForInput(false));
                    break;
                   
                case TokenType.WaitForInputAndClear:
                    yield return StartCoroutine(DoWaitForInput(true));
                    break;
                   
                case TokenType.WaitOnPunctuationStart:
                    TryGetSingleParam(token.paramList, 0, punctuationPause, out currentPunctuationPause);
                    break;
                   
                case TokenType.WaitOnPunctuationEnd:
                    currentPunctuationPause = punctuationPause;
                    break;
                   
                case TokenType.Clear:
                    Text = "";
                    break;
                   
                case TokenType.SpeedStart:
                    TryGetSingleParam(token.paramList, 0, writingSpeed, out currentWritingSpeed);
                    break;
                   
                case TokenType.SpeedEnd:
                    currentWritingSpeed = writingSpeed;
                    break;
                   
                case TokenType.Exit:
                    exitFlag = true;
                    break;

                case TokenType.Message:
                    if (CheckParamCount(token.paramList, 1))
                    {
                        Flowchart.BroadcastFungusMessage(token.paramList[0]);
                    }
                    break;
                   
                case TokenType.VerticalPunch:
                    {
                        float vintensity;
                        float time;
                        TryGetSingleParam(token.paramList, 0, 10.0f, out vintensity);
                        TryGetSingleParam(token.paramList, 1, 0.5f, out time);
                        Punch(new Vector3(0, vintensity, 0), time);
                    }
                    break;
                   
                case TokenType.HorizontalPunch:
                    {
                        float hintensity;
                        float time;
                        TryGetSingleParam(token.paramList, 0, 10.0f, out hintensity);
                        TryGetSingleParam(token.paramList, 1, 0.5f, out time);
                        Punch(new Vector3(hintensity, 0, 0), time);
                    }
                    break;
                   
                case TokenType.Punch:
                    {
                        float intensity;
                        float time;
                        TryGetSingleParam(token.paramList, 0, 10.0f, out intensity);
                        TryGetSingleParam(token.paramList, 1, 0.5f, out time);
                        Punch(new Vector3(intensity, intensity, 0), time);
                    }
                    break;
                   
                case TokenType.Flash:
                    float flashDuration;
                    TryGetSingleParam(token.paramList, 0, 0.2f, out flashDuration);
                    Flash(flashDuration);
                    break;

                case TokenType.Audio:
                    {
                        AudioSource audioSource = null;
                        if (CheckParamCount(token.paramList, 1))
                        {
                            audioSource = FindAudio(token.paramList[0]);
                        }
                        if (audioSource != null)
                        {
                            audioSource.PlayOneShot(audioSource.clip);
                        }
                    }
                    break;
                   
                case TokenType.AudioLoop:
                    {
                        AudioSource audioSource = null;
                        if (CheckParamCount(token.paramList, 1))
                        {
                            audioSource = FindAudio(token.paramList[0]);
                        }
                        if (audioSource != null)
                        {
                            audioSource.Play();
                            audioSource.loop = true;
                        }
                    }
                    break;
                   
                case TokenType.AudioPause:
                    {
                        AudioSource audioSource = null;
                        if (CheckParamCount(token.paramList, 1))
                        {
                            audioSource = FindAudio(token.paramList[0]);
                        }
                        if (audioSource != null)
                        {
                            audioSource.Pause();
                        }
                    }
                    break;
                   
                case TokenType.AudioStop:
                    {
                        AudioSource audioSource = null;
                        if (CheckParamCount(token.paramList, 1))
                        {
                            audioSource = FindAudio(token.paramList[0]);
                        }
                        if (audioSource != null)
                        {
                            audioSource.Stop();
                        }
                    }
                    break;
                }

                previousTokenType = token.type;

                if (exitFlag)
                {
                    break;
                }
            }

            inputFlag = false;
            exitFlag = false;
            isWaitingForInput = false;
            isWriting = false;

            NotifyEnd(stopAudio);

            if (onComplete != null)
            {
                onComplete();
            }
        }

        protected virtual IEnumerator DoWords(List<string> paramList, TokenType previousTokenType)
        {
            if (!CheckParamCount(paramList, 1))
            {
                yield break;
            }

            string param = paramList[0];

            // Trim whitespace after a {wc} or {c} tag
            if (previousTokenType == TokenType.WaitForInputAndClear ||
                previousTokenType == TokenType.Clear)
            {
                param = param.TrimStart(' ', '\t', '\r', '\n');
            }

            // Start with the visible portion of any existing displayed text.
           // Начнем с видимой части любого существующего текста.
            string startText = "";
            if (visibleCharacterCount > 0 &&
                visibleCharacterCount <= Text.Length)
            {
                startText = Text.Substring(0, visibleCharacterCount);
            }
               
            UpdateOpenMarkup();
            UpdateCloseMarkup();

            float timeAccumulator = Time.deltaTime;

            for (int i = 0; i < param.Length + 1; ++i)
            {
                // Exit immediately if the exit flag has been set
                if (exitFlag)
                {
                    break;
                }

                // Pause mid sentence if Paused is set
                while (Paused)
                {
                    yield return null;
                }

                PartitionString(writeWholeWords, param, i);
                ConcatenateString(startText);
                Text = outputString.ToString();

                NotifyGlyph();

                // No delay if user has clicked and Instant Complete is enabled
                if (instantComplete && inputFlag)
                {
                    continue;
                }

                // Punctuation pause
                if (leftString.Length > 0 &&
                    rightString.Length > 0 &&
                    IsPunctuation(leftString.ToString(leftString.Length - 1, 1)[0]))
                {
                    yield return StartCoroutine(DoWait(currentPunctuationPause));
                }

                // Delay between characters
                if (currentWritingSpeed > 0f)
                {
                    if (timeAccumulator > 0f)
                    {
                        timeAccumulator -= 1f / currentWritingSpeed;
                    }
                    else
                    {
                        yield return new WaitForSeconds(1f / currentWritingSpeed);
                    }
                }
            }
        }

        protected virtual void PartitionString(bool wholeWords, string inputString, int i)
        {
            leftString.Length = 0;
            rightString.Length = 0;

            // Reached last character
            leftString.Append(inputString);
            if (i >= inputString.Length)
            {
                return;
            }

            rightString.Append(inputString);

            if (wholeWords)
            {
                // Look ahead to find next whitespace or end of string
                for (int j = i; j < inputString.Length + 1; ++j)
                {
                    if (j == inputString.Length || Char.IsWhiteSpace(inputString[j]))
                    {
                        leftString.Length = j;
                        rightString.Remove(0, j);
                        break;
                    }
                }
            }
            else
            {
                leftString.Remove(i, inputString.Length - i);
                rightString.Remove(0, i);
            }
        }

        protected virtual void ConcatenateString(string startText)
        {
            outputString.Length = 0;

            // string tempText = startText + openText + leftText + closeText;
            outputString.Append(startText);
            outputString.Append(openString);
            outputString.Append(leftString);
            outputString.Append(closeString);

            // Track how many visible characters are currently displayed so
            // we can easily extract the visible portion again later.
            visibleCharacterCount = outputString.Length;

            // Make right hand side text hidden
            if (SupportsRichText() &&
                rightString.Length + readAheadString.Length > 0)
            {
                // Ensure the hidden color strings are populated
                if (hiddenColorOpen.Length == 0)
                {
                    CacheHiddenColorStrings();
                }

                outputString.Append(hiddenColorOpen);
                outputString.Append(rightString);
                outputString.Append(readAheadString);
                outputString.Append(hiddenColorClose);
            }
        }

        protected virtual IEnumerator DoWait(List<string> paramList)
        {
            var param = "";
            if (paramList.Count == 1)
            {
                param = paramList[0];
            }

            float duration = 1f;
            if (!Single.TryParse(param, out duration))
            {
                duration = 1f;
            }

            yield return StartCoroutine( DoWait(duration) );
        }

        protected virtual IEnumerator DoWait(float duration)
        {
            NotifyPause();

            float timeRemaining = duration;
            while (timeRemaining > 0f && !exitFlag)
            {
                if (instantComplete && inputFlag)
                {
                    break;
                }

                timeRemaining -= Time.deltaTime;
                yield return null;
            }

            NotifyResume();
        }

        protected virtual IEnumerator DoWaitForInput(bool clear)
        {
            NotifyPause();

            inputFlag = false;
            isWaitingForInput = true;

            while (!inputFlag && !exitFlag)
            {
                yield return null;
            }
       
            isWaitingForInput = false;          
            inputFlag = false;

            if (clear)
            {
                textUI.text = "";
            }

            NotifyResume();
        }
       
        protected virtual bool IsPunctuation(char character)
        {
            return character == '.' ||
                character == '?' ||  
                    character == '!' ||
                    character == ',' ||
                    character == ':' ||
                    character == ';' ||
                    character == ')';
        }
       
        protected virtual void Punch(Vector3 axis, float time)
        {
            GameObject go = punchObject;
            if (go == null)
            {
                go = Camera.main.gameObject;
            }

            if (go != null)
            {
                iTween.ShakePosition(go, axis, time);
            }
        }
       
        protected virtual void Flash(float duration)
        {
            var cameraManager = FungusManager.Instance.CameraManager;

            cameraManager.ScreenFadeTexture = CameraManager.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32);
            cameraManager.Fade(1f, duration, delegate {
                cameraManager.ScreenFadeTexture = CameraManager.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32);
                cameraManager.Fade(0f, duration, null);
            });
        }
       
        protected virtual AudioSource FindAudio(string audioObjectName)
        {
            GameObject go = GameObject.Find(audioObjectName);
            if (go == null)
            {
                return null;
            }
           
            return go.GetComponent<AudioSource>();
        }

        protected virtual void NotifyInput()
        {
            WriterSignals.DoWriterInput(this);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnInput();
            }
        }

        protected virtual void NotifyStart(AudioClip audioClip)
        {
            WriterSignals.DoWriterState(this, WriterState.Start);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnStart(audioClip);
            }
        }

        protected virtual void NotifyPause()
        {
            WriterSignals.DoWriterState(this, WriterState.Pause);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnPause();
            }
        }

        protected virtual void NotifyResume()
        {
            WriterSignals.DoWriterState(this, WriterState.Resume);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnResume();
            }
        }

        protected virtual void NotifyEnd(bool stopAudio)
        {
            WriterSignals.DoWriterState(this, WriterState.End);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnEnd(stopAudio);
            }
        }

        protected virtual void NotifyGlyph()
        {
            WriterSignals.DoWriterGlyph(this);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnGlyph();
            }
        }

        #region Public members

        /// <summary>
        /// Gets or sets the text property of the attached text object.
        /// </summary>
        public virtual string Text
        {
            get
            {
                if (textUI != null)
                {
                    return textUI.text;
                }
                else if (inputField != null)
                {
                    return inputField.text;
                }
                else if (textMesh != null)
                {
                    return textMesh.text;
                }
                else if (textProperty != null)
                {
                    return textProperty.GetValue(textComponent, null) as string;
                }

                return "";
            }

            set
            {
                if (textUI != null)
                {
                    textUI.text = value;
                }
                else if (inputField != null)
                {
                    inputField.text = value;
                }
                else if (textMesh != null)
                {
                    textMesh.text = value;
                }
                else if (textProperty != null)
                {
                    textProperty.SetValue(textComponent, value, null);
                }
            }
        }

        /// <summary>
        /// This property is true when the writer is writing text or waiting (i.e. still processing tokens).
        /// </summary>
        public virtual bool IsWriting { get { return isWriting; } }

        /// <summary>
        /// This property is true when the writer is waiting for user input to continue.
        /// </summary>
        public virtual bool IsWaitingForInput { get { return isWaitingForInput; } }

        /// <summary>
        /// Pauses the writer.
        /// </summary>
        public virtual bool Paused { set; get; }

        /// <summary>
        /// Stop writing text.
        /// </summary>
        public virtual void Stop()
        {
            if (isWriting || isWaitingForInput)
            {
                exitFlag = true;
            }
        }

        /// <summary>
        /// Writes text using a typewriter effect to a UI text object.
        /// </summary>
        /// <param name="content">Text to be written</param>
        /// <param name="clear">If true clears the previous text.</param>
        /// <param name="waitForInput">Writes the text and then waits for player input before calling onComplete.</param>
        /// <param name="stopAudio">Stops any currently playing audioclip.</param>
        /// <param name="audioClip">Audio clip to play when text starts writing.</param>
        /// <param name="onComplete">Callback to call when writing is finished.</param>
        public virtual IEnumerator Write(string content, bool clear, bool waitForInput, bool stopAudio, AudioClip audioClip, Action onComplete)
        {
            if (clear)
            {
                this.Text = "";
            }

            if (!HasTextObject())
            {
                yield break;
            }

            // If this clip is null then WriterAudio will play the default sound effect (if any)
            NotifyStart(audioClip);

            string tokenText = content;
            if (waitForInput)
            {
                tokenText += "{wi}";
            }

            List<TextTagToken> tokens = TextTagParser.Tokenize(tokenText);

            gameObject.SetActive(true);

            yield return StartCoroutine(ProcessTokens(tokens, stopAudio, onComplete));
        }

        /// <summary>
        /// Sets the color property of the text UI object.
        /// </summary>
        public virtual void SetTextColor(Color textColor)
        {
            if (textUI != null)
            {
                textUI.color = textColor;
            }
            else if (inputField != null)
            {
                if (inputField.textComponent != null)
                {
                    inputField.textComponent.color = textColor;
                }
            }
            else if (textMesh != null)
            {
                textMesh.color = textColor;
            }
        }
         public virtual void Math(float textAlpha)
        {
            if (textUI != null)
            {
                Color tempColor = textUI.color;
                tempColor.a = textAlpha;
                textUI.color = tempColor;
            }
            else if (inputField != null)
            {
                if (inputField.textComponent != null)
                {
                    Color tempColor = inputField.textComponent.color;
                    tempColor.a = textAlpha;
                    inputField.textComponent.color = tempColor;
                }
            }
            else if (textMesh != null)
            {
                Color tempColor = textMesh.color;
                tempColor.a = textAlpha;
                textMesh.color = tempColor;
            }
        }

        /// <summary>
        /// Sets the alpha component of the color property of the text UI object.
        /// </summary>
        public virtual void SetTextAlpha(float textAlpha)
        {
            if (textUI != null)
            {
                Color tempColor = textUI.color;
                tempColor.a = textAlpha;
                textUI.color = tempColor;
            }
            else if (inputField != null)
            {
                if (inputField.textComponent != null)
                {
                    Color tempColor = inputField.textComponent.color;
                    tempColor.a = textAlpha;
                    inputField.textComponent.color = tempColor;
                }
            }
            else if (textMesh != null)
            {
                Color tempColor = textMesh.color;
                tempColor.a = textAlpha;
                textMesh.color = tempColor;
            }
        }

        /// <summary>
        /// Returns true if there is a supported text object attached to this writer.
        /// </summary>
        public virtual bool HasTextObject()
        {
            return (textUI != null || inputField != null || textMesh != null || textComponent != null);
        }

        /// <summary>
        /// Returns true if the text object has rich text support.
        /// </summary>
        public virtual bool SupportsRichText()
        {
            if (textUI != null)
            {
                return textUI.supportRichText;
            }
            if (inputField != null)
            {
                return false;
            }
            if (textMesh != null)
            {
                return textMesh.richText;
            }
            return false;
        }

        #endregion

        #region IDialogInputListener implementation

        public virtual void OnNextLineEvent()
        {
            inputFlag = true;

            if (isWriting)
            {
                NotifyInput();
            }
        }

        #endregion
    }
}

 
Последний раз редактировалось Orcan 21 сен 2017, 21:14, всего редактировалось 1 раз.
Orcan
UNITрон
 
Сообщения: 191
Зарегистрирован: 25 сен 2016, 04:22

Re: Как создать свои Дополнительные теги в Тексте

Сообщение samana 21 сен 2017, 21:07

Orcan писал(а):Нашёл 3 как минимум скрипта которые это делают. Но для меня пока что как фильме сказано "Здесь что-о на эльфийском "

Как раз такие моменты и заставляют прогрессивно становиться на уровень выше в знаниях, когда приходится вникнуть в совершенно незнакомую тему, понять её и научится этому всему. Так что всё в порядке, вы на правильном пути!
Аватара пользователя
samana
Адепт
 
Сообщения: 4738
Зарегистрирован: 21 фев 2015, 13:00
Откуда: Днепропетровск

Re: Как создать свои Дополнительные теги в Тексте

Сообщение Orcan 21 сен 2017, 21:27

вторая часть отвечающия исполнение команд
Синтаксис:
Используется csharp
public enum WriterState
    {
        /// <summary> Invalid state. </summary>
        Invalid,
        /// <summary> Writer has started writing. </summary>
        Start,
        /// <summary> Writing has been paused. </summary>
        Pause,
        /// <summary> Writing has resumed after a pause. </summary>
        Resume,
        /// <summary> Writing has ended. </summary>
        End
    }

    /// <summary>
    /// Writes text using a typewriter effect to a UI text object.
    /// </summary>
    public class Writer : MonoBehaviour, IDialogInputListener
    {
        [Tooltip("Gameobject containing a Text, Inout Field or Text Mesh object to write to")]
        [SerializeField] protected GameObject targetTextObject;

        [Tooltip("Gameobject to punch when the punch tags are displayed. If none is set, the main camera will shake instead.")]
        [SerializeField] protected GameObject punchObject;

        [Tooltip("Writing characters per second")]
        [SerializeField] protected float writingSpeed = 60;

        [Tooltip("Pause duration for punctuation characters")]
        [SerializeField] protected float punctuationPause = 0.25f;

        [Tooltip("Color of text that has not been revealed yet")]
        [SerializeField] protected Color hiddenTextColor = new Color(1,1,1,0);

        [Tooltip("Write one word at a time rather one character at a time")]
        [SerializeField] protected bool writeWholeWords = false;

        [Tooltip("Force the target text object to use Rich Text mode so text color and alpha appears correctly")]
        [SerializeField] protected bool forceRichText = true;

        [Tooltip("Click while text is writing to finish writing immediately")]
        [SerializeField] protected bool instantComplete = true;

        // This property is true when the writer is waiting for user input to continue
        protected bool isWaitingForInput;

        // This property is true when the writer is writing text or waiting (i.e. still processing tokens)
        protected bool isWriting;

        protected float currentWritingSpeed;
        protected float currentPunctuationPause;
        protected Text textUI;
        protected InputField inputField;
        protected TextMesh textMesh;
        protected Component textComponent;
        protected PropertyInfo textProperty;

        protected bool boldActive = false;
        protected bool italicActive = false;
        protected bool colorActive = false;
        protected string colorText = "";
        protected bool sizeActive = false;
        protected float sizeValue = 16f;
        protected bool inputFlag;
        protected bool exitFlag;

        protected List<IWriterListener> writerListeners = new List<IWriterListener>();

        protected StringBuilder openString = new StringBuilder(256);
        protected StringBuilder closeString = new StringBuilder(256);
        protected StringBuilder leftString = new StringBuilder(1024);
        protected StringBuilder rightString = new StringBuilder(1024);
        protected StringBuilder outputString = new StringBuilder(1024);
        protected StringBuilder readAheadString = new StringBuilder(1024);

        protected string hiddenColorOpen = "";
        protected string hiddenColorClose = "";

        protected int visibleCharacterCount = 0;

        protected virtual void Awake()
        {
            GameObject go = targetTextObject;
            if (go == null)
            {
                go = gameObject;
            }

            textUI = go.GetComponent<Text>();
            inputField = go.GetComponent<InputField>();
            textMesh = go.GetComponent<TextMesh>();

            // Try to find any component with a text property
            if (textUI == null && inputField == null && textMesh == null)
            {
                var allcomponents = go.GetComponents<Component>();
                for (int i = 0; i < allcomponents.Length; i++)
                {
                    var c = allcomponents[i];
                    textProperty = c.GetType().GetProperty("text");
                    if (textProperty != null)
                    {
                        textComponent = c;
                        break;
                    }
                }
            }

            // Cache the list of child writer listeners
            var allComponents = GetComponentsInChildren<Component>();
            for (int i = 0; i < allComponents.Length; i++)
            {
                var component = allComponents[i];
                IWriterListener writerListener = component as IWriterListener;
                if (writerListener != null)
                {
                    writerListeners.Add(writerListener);
                }
            }

            CacheHiddenColorStrings();
        }

        protected virtual void CacheHiddenColorStrings()
        {
            // Cache the hidden color string
            Color32 c = hiddenTextColor;
            hiddenColorOpen = String.Format("<color=#{0:X2}{1:X2}{2:X2}{3:X2}>", c.r, c.g, c.b, c.a);
            hiddenColorClose = "</color>";
        }

        protected virtual void Start()
        {
            if (forceRichText)
            {
                if (textUI != null)
                {
                    textUI.supportRichText = true;
                }

                // Input Field does not support rich text

                if (textMesh != null)
                {
                    textMesh.richText = true;
                }
            }
        }
       
        protected virtual void UpdateOpenMarkup()
        {
            openString.Length = 0;
           
            if (SupportsRichText())
            {
                if (sizeActive)
                {
                    openString.Append("<size=");
                    openString.Append(sizeValue);
                    openString.Append(">");
                }
                if (colorActive)
                {
                    openString.Append("<color=");
                    openString.Append(colorText);
                    openString.Append(">");
                }
                if (boldActive)
                {
                    openString.Append("<b>");
                }
                if (italicActive)
                {
                    openString.Append("<i>");
                }          
            }
        }
       
        protected virtual void UpdateCloseMarkup()
        {
            closeString.Length = 0;
           
            if (SupportsRichText())
            {
                if (italicActive)
                {
                    closeString.Append("</i>");
                }          
                if (boldActive)
                {
                    closeString.Append("</b>");
                }
                if (colorActive)
                {
                    closeString.Append("</color>");
                }
                if (sizeActive)
                {
                    closeString.Append("</size>");
                }
            }
        }

        protected virtual bool CheckParamCount(List<string> paramList, int count)
        {
            if (paramList == null)
            {
                UnityEngine.Debug.LogError("paramList is null");
                return false;
            }
            if (paramList.Count != count)
            {
                UnityEngine.Debug.LogError("There must be exactly " + paramList.Count + " parameters.");
                return false;
            }
            return true;
        }

        protected virtual bool TryGetSingleParam(List<string> paramList, int index, float defaultValue, out float value)
        {
            value = defaultValue;
            if (paramList.Count > index)
            {
                Single.TryParse(paramList[index], out value);
                return true;
            }
            return false;
        }

        protected virtual IEnumerator ProcessTokens(List<TextTagToken> tokens, bool stopAudio, Action onComplete)
        {
            // Reset control members
            boldActive = false;
            italicActive = false;
            colorActive = false;
            sizeActive = false;
            colorText = "";
            sizeValue = 16f;
            currentPunctuationPause = punctuationPause;
            currentWritingSpeed = writingSpeed;

            exitFlag = false;
            isWriting = true;

            TokenType previousTokenType = TokenType.Invalid;

            visibleCharacterCount = 0;

            for (int i = 0; i < tokens.Count; ++i)
            {
                // Pause between tokens if Paused is set
                while (Paused)
                {
                    yield return null;
                }

                var token = tokens[i];

                // Notify listeners about new token
                WriterSignals.DoTextTagToken(this, token, i, tokens.Count);
               
                // Update the read ahead string buffer. This contains the text for any
                // Word tags which are further ahead in the list.
                // Обновите буфер строки чтения вперед. Он содержит текст для любого
                 // Теги Word, которые дальше в списке.
                readAheadString.Length = 0;
                for (int j = i + 1; j < tokens.Count; ++j)
                {
                    var readAheadToken = tokens[j];

                    if (readAheadToken.type == TokenType.Words &&
                        readAheadToken.paramList.Count == 1)
                    {
                        readAheadString.Append(readAheadToken.paramList[0]);
                    }
                    else if (readAheadToken.type == TokenType.WaitForInputAndClear)
                    {
                        break;
                    }
                }

                switch (token.type)
                {
                case TokenType.Words:
                    yield return StartCoroutine(DoWords(token.paramList, previousTokenType));
                    break;
                   
                case TokenType.BoldStart:
                    boldActive = true;
                    break;
                   
                case TokenType.BoldEnd:
                    boldActive = false;
                    break;
                   
                case TokenType.ItalicStart:
                    italicActive = true;
                    break;
                   
                case TokenType.ItalicEnd:
                    italicActive = false;
                    break;
                   
                case TokenType.ColorStart:
                    if (CheckParamCount(token.paramList, 1))
                    {
                        colorActive = true;
                        colorText = token.paramList[0];
                    }
                    break;
                   
                case TokenType.ColorEnd:
                    colorActive = false;
                    break;

                case TokenType.SizeStart:
                    if (TryGetSingleParam(token.paramList, 0, 16f, out sizeValue))
                    {
                        sizeActive = true;
                    }
                    break;

                case TokenType.SizeEnd:
                    sizeActive = false;
                    break;

                case TokenType.Wait:
                    yield return StartCoroutine(DoWait(token.paramList));
                    break;
                   
                case TokenType.WaitForInputNoClear:
                    yield return StartCoroutine(DoWaitForInput(false));
                    break;
                   
                case TokenType.WaitForInputAndClear:
                    yield return StartCoroutine(DoWaitForInput(true));
                    break;
                   
                case TokenType.WaitOnPunctuationStart:
                    TryGetSingleParam(token.paramList, 0, punctuationPause, out currentPunctuationPause);
                    break;
                   
                case TokenType.WaitOnPunctuationEnd:
                    currentPunctuationPause = punctuationPause;
                    break;
                   
                case TokenType.Clear:
                    Text = "";
                    break;
                   
                case TokenType.SpeedStart:
                    TryGetSingleParam(token.paramList, 0, writingSpeed, out currentWritingSpeed);
                    break;
                   
                case TokenType.SpeedEnd:
                    currentWritingSpeed = writingSpeed;
                    break;
                   
                case TokenType.Exit:
                    exitFlag = true;
                    break;

                case TokenType.Message:
                    if (CheckParamCount(token.paramList, 1))
                    {
                        Flowchart.BroadcastFungusMessage(token.paramList[0]);
                    }
                    break;
                   
                case TokenType.VerticalPunch:
                    {
                        float vintensity;
                        float time;
                        TryGetSingleParam(token.paramList, 0, 10.0f, out vintensity);
                        TryGetSingleParam(token.paramList, 1, 0.5f, out time);
                        Punch(new Vector3(0, vintensity, 0), time);
                    }
                    break;
                   
                case TokenType.HorizontalPunch:
                    {
                        float hintensity;
                        float time;
                        TryGetSingleParam(token.paramList, 0, 10.0f, out hintensity);
                        TryGetSingleParam(token.paramList, 1, 0.5f, out time);
                        Punch(new Vector3(hintensity, 0, 0), time);
                    }
                    break;
                   
                case TokenType.Punch:
                    {
                        float intensity;
                        float time;
                        TryGetSingleParam(token.paramList, 0, 10.0f, out intensity);
                        TryGetSingleParam(token.paramList, 1, 0.5f, out time);
                        Punch(new Vector3(intensity, intensity, 0), time);
                    }
                    break;
                   
                case TokenType.Flash:
                    float flashDuration;
                    TryGetSingleParam(token.paramList, 0, 0.2f, out flashDuration);
                    Flash(flashDuration);
                    break;

                case TokenType.Audio:
                    {
                        AudioSource audioSource = null;
                        if (CheckParamCount(token.paramList, 1))
                        {
                            audioSource = FindAudio(token.paramList[0]);
                        }
                        if (audioSource != null)
                        {
                            audioSource.PlayOneShot(audioSource.clip);
                        }
                    }
                    break;
                   
                case TokenType.AudioLoop:
                    {
                        AudioSource audioSource = null;
                        if (CheckParamCount(token.paramList, 1))
                        {
                            audioSource = FindAudio(token.paramList[0]);
                        }
                        if (audioSource != null)
                        {
                            audioSource.Play();
                            audioSource.loop = true;
                        }
                    }
                    break;
                   
                case TokenType.AudioPause:
                    {
                        AudioSource audioSource = null;
                        if (CheckParamCount(token.paramList, 1))
                        {
                            audioSource = FindAudio(token.paramList[0]);
                        }
                        if (audioSource != null)
                        {
                            audioSource.Pause();
                        }
                    }
                    break;
                   
                case TokenType.AudioStop:
                    {
                        AudioSource audioSource = null;
                        if (CheckParamCount(token.paramList, 1))
                        {
                            audioSource = FindAudio(token.paramList[0]);
                        }
                        if (audioSource != null)
                        {
                            audioSource.Stop();
                        }
                    }
                    break;
                }

                previousTokenType = token.type;

                if (exitFlag)
                {
                    break;
                }
            }

            inputFlag = false;
            exitFlag = false;
            isWaitingForInput = false;
            isWriting = false;

            NotifyEnd(stopAudio);

            if (onComplete != null)
            {
                onComplete();
            }
        }

        protected virtual IEnumerator DoWords(List<string> paramList, TokenType previousTokenType)
        {
            if (!CheckParamCount(paramList, 1))
            {
                yield break;
            }

            string param = paramList[0];

            // Trim whitespace after a {wc} or {c} tag
            if (previousTokenType == TokenType.WaitForInputAndClear ||
                previousTokenType == TokenType.Clear)
            {
                param = param.TrimStart(' ', '\t', '\r', '\n');
            }

            // Start with the visible portion of any existing displayed text.
           // Начнем с видимой части любого существующего текста.
            string startText = "";
            if (visibleCharacterCount > 0 &&
                visibleCharacterCount <= Text.Length)
            {
                startText = Text.Substring(0, visibleCharacterCount);
            }
               
            UpdateOpenMarkup();
            UpdateCloseMarkup();

            float timeAccumulator = Time.deltaTime;

            for (int i = 0; i < param.Length + 1; ++i)
            {
                // Exit immediately if the exit flag has been set
                if (exitFlag)
                {
                    break;
                }

                // Pause mid sentence if Paused is set
                while (Paused)
                {
                    yield return null;
                }

                PartitionString(writeWholeWords, param, i);
                ConcatenateString(startText);
                Text = outputString.ToString();

                NotifyGlyph();

                // No delay if user has clicked and Instant Complete is enabled
                if (instantComplete && inputFlag)
                {
                    continue;
                }

                // Punctuation pause
                if (leftString.Length > 0 &&
                    rightString.Length > 0 &&
                    IsPunctuation(leftString.ToString(leftString.Length - 1, 1)[0]))
                {
                    yield return StartCoroutine(DoWait(currentPunctuationPause));
                }

                // Delay between characters
                if (currentWritingSpeed > 0f)
                {
                    if (timeAccumulator > 0f)
                    {
                        timeAccumulator -= 1f / currentWritingSpeed;
                    }
                    else
                    {
                        yield return new WaitForSeconds(1f / currentWritingSpeed);
                    }
                }
            }
        }

        protected virtual void PartitionString(bool wholeWords, string inputString, int i)
        {
            leftString.Length = 0;
            rightString.Length = 0;

            // Reached last character
            leftString.Append(inputString);
            if (i >= inputString.Length)
            {
                return;
            }

            rightString.Append(inputString);

            if (wholeWords)
            {
                // Look ahead to find next whitespace or end of string
                for (int j = i; j < inputString.Length + 1; ++j)
                {
                    if (j == inputString.Length || Char.IsWhiteSpace(inputString[j]))
                    {
                        leftString.Length = j;
                        rightString.Remove(0, j);
                        break;
                    }
                }
            }
            else
            {
                leftString.Remove(i, inputString.Length - i);
                rightString.Remove(0, i);
            }
        }

        protected virtual void ConcatenateString(string startText)
        {
            outputString.Length = 0;

            // string tempText = startText + openText + leftText + closeText;
            outputString.Append(startText);
            outputString.Append(openString);
            outputString.Append(leftString);
            outputString.Append(closeString);

            // Track how many visible characters are currently displayed so
            // we can easily extract the visible portion again later.
            visibleCharacterCount = outputString.Length;

            // Make right hand side text hidden
            if (SupportsRichText() &&
                rightString.Length + readAheadString.Length > 0)
            {
                // Ensure the hidden color strings are populated
                if (hiddenColorOpen.Length == 0)
                {
                    CacheHiddenColorStrings();
                }

                outputString.Append(hiddenColorOpen);
                outputString.Append(rightString);
                outputString.Append(readAheadString);
                outputString.Append(hiddenColorClose);
            }
        }

        protected virtual IEnumerator DoWait(List<string> paramList)
        {
            var param = "";
            if (paramList.Count == 1)
            {
                param = paramList[0];
            }

            float duration = 1f;
            if (!Single.TryParse(param, out duration))
            {
                duration = 1f;
            }

            yield return StartCoroutine( DoWait(duration) );
        }

        protected virtual IEnumerator DoWait(float duration)
        {
            NotifyPause();

            float timeRemaining = duration;
            while (timeRemaining > 0f && !exitFlag)
            {
                if (instantComplete && inputFlag)
                {
                    break;
                }

                timeRemaining -= Time.deltaTime;
                yield return null;
            }

            NotifyResume();
        }

        protected virtual IEnumerator DoWaitForInput(bool clear)
        {
            NotifyPause();

            inputFlag = false;
            isWaitingForInput = true;

            while (!inputFlag && !exitFlag)
            {
                yield return null;
            }
       
            isWaitingForInput = false;          
            inputFlag = false;

            if (clear)
            {
                textUI.text = "";
            }

            NotifyResume();
        }
       
        protected virtual bool IsPunctuation(char character)
        {
            return character == '.' ||
                character == '?' ||  
                    character == '!' ||
                    character == ',' ||
                    character == ':' ||
                    character == ';' ||
                    character == ')';
        }
       
        protected virtual void Punch(Vector3 axis, float time)
        {
            GameObject go = punchObject;
            if (go == null)
            {
                go = Camera.main.gameObject;
            }

            if (go != null)
            {
                iTween.ShakePosition(go, axis, time);
            }
        }
       
        protected virtual void Flash(float duration)
        {
            var cameraManager = FungusManager.Instance.CameraManager;

            cameraManager.ScreenFadeTexture = CameraManager.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32);
            cameraManager.Fade(1f, duration, delegate {
                cameraManager.ScreenFadeTexture = CameraManager.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32);
                cameraManager.Fade(0f, duration, null);
            });
        }
       
        protected virtual AudioSource FindAudio(string audioObjectName)
        {
            GameObject go = GameObject.Find(audioObjectName);
            if (go == null)
            {
                return null;
            }
           
            return go.GetComponent<AudioSource>();
        }

        protected virtual void NotifyInput()
        {
            WriterSignals.DoWriterInput(this);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnInput();
            }
        }

        protected virtual void NotifyStart(AudioClip audioClip)
        {
            WriterSignals.DoWriterState(this, WriterState.Start);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnStart(audioClip);
            }
        }

        protected virtual void NotifyPause()
        {
            WriterSignals.DoWriterState(this, WriterState.Pause);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnPause();
            }
        }

        protected virtual void NotifyResume()
        {
            WriterSignals.DoWriterState(this, WriterState.Resume);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnResume();
            }
        }

        protected virtual void NotifyEnd(bool stopAudio)
        {
            WriterSignals.DoWriterState(this, WriterState.End);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnEnd(stopAudio);
            }
        }

        protected virtual void NotifyGlyph()
        {
            WriterSignals.DoWriterGlyph(this);

            for (int i = 0; i < writerListeners.Count; i++)
            {
                var writerListener = writerListeners[i];
                writerListener.OnGlyph();
            }
        }

        #region Public members

        /// <summary>
        /// Gets or sets the text property of the attached text object.
        /// </summary>
        public virtual string Text
        {
            get
            {
                if (textUI != null)
                {
                    return textUI.text;
                }
                else if (inputField != null)
                {
                    return inputField.text;
                }
                else if (textMesh != null)
                {
                    return textMesh.text;
                }
                else if (textProperty != null)
                {
                    return textProperty.GetValue(textComponent, null) as string;
                }

                return "";
            }

            set
            {
                if (textUI != null)
                {
                    textUI.text = value;
                }
                else if (inputField != null)
                {
                    inputField.text = value;
                }
                else if (textMesh != null)
                {
                    textMesh.text = value;
                }
                else if (textProperty != null)
                {
                    textProperty.SetValue(textComponent, value, null);
                }
            }
        }

        /// <summary>
        /// This property is true when the writer is writing text or waiting (i.e. still processing tokens).
        /// </summary>
        public virtual bool IsWriting { get { return isWriting; } }

        /// <summary>
        /// This property is true when the writer is waiting for user input to continue.
        /// </summary>
        public virtual bool IsWaitingForInput { get { return isWaitingForInput; } }

        /// <summary>
        /// Pauses the writer.
        /// </summary>
        public virtual bool Paused { set; get; }

        /// <summary>
        /// Stop writing text.
        /// </summary>
        public virtual void Stop()
        {
            if (isWriting || isWaitingForInput)
            {
                exitFlag = true;
            }
        }

        /// <summary>
        /// Writes text using a typewriter effect to a UI text object.
        /// </summary>
        /// <param name="content">Text to be written</param>
        /// <param name="clear">If true clears the previous text.</param>
        /// <param name="waitForInput">Writes the text and then waits for player input before calling onComplete.</param>
        /// <param name="stopAudio">Stops any currently playing audioclip.</param>
        /// <param name="audioClip">Audio clip to play when text starts writing.</param>
        /// <param name="onComplete">Callback to call when writing is finished.</param>
        public virtual IEnumerator Write(string content, bool clear, bool waitForInput, bool stopAudio, AudioClip audioClip, Action onComplete)
        {
            if (clear)
            {
                this.Text = "";
            }

            if (!HasTextObject())
            {
                yield break;
            }

            // If this clip is null then WriterAudio will play the default sound effect (if any)
            NotifyStart(audioClip);

            string tokenText = content;
            if (waitForInput)
            {
                tokenText += "{wi}";
            }

            List<TextTagToken> tokens = TextTagParser.Tokenize(tokenText);

            gameObject.SetActive(true);

            yield return StartCoroutine(ProcessTokens(tokens, stopAudio, onComplete));
        }

        /// <summary>
        /// Sets the color property of the text UI object.
        /// </summary>
        public virtual void SetTextColor(Color textColor)
        {
            if (textUI != null)
            {
                textUI.color = textColor;
            }
            else if (inputField != null)
            {
                if (inputField.textComponent != null)
                {
                    inputField.textComponent.color = textColor;
                }
            }
            else if (textMesh != null)
            {
                textMesh.color = textColor;
            }
        }
         public virtual void Math(float textAlpha)
        {
            if (textUI != null)
            {
                Color tempColor = textUI.color;
                tempColor.a = textAlpha;
                textUI.color = tempColor;
            }
            else if (inputField != null)
            {
                if (inputField.textComponent != null)
                {
                    Color tempColor = inputField.textComponent.color;
                    tempColor.a = textAlpha;
                    inputField.textComponent.color = tempColor;
                }
            }
            else if (textMesh != null)
            {
                Color tempColor = textMesh.color;
                tempColor.a = textAlpha;
                textMesh.color = tempColor;
            }
        }

        /// <summary>
        /// Sets the alpha component of the color property of the text UI object.
        /// </summary>
        public virtual void SetTextAlpha(float textAlpha)
        {
            if (textUI != null)
            {
                Color tempColor = textUI.color;
                tempColor.a = textAlpha;
                textUI.color = tempColor;
            }
            else if (inputField != null)
            {
                if (inputField.textComponent != null)
                {
                    Color tempColor = inputField.textComponent.color;
                    tempColor.a = textAlpha;
                    inputField.textComponent.color = tempColor;
                }
            }
            else if (textMesh != null)
            {
                Color tempColor = textMesh.color;
                tempColor.a = textAlpha;
                textMesh.color = tempColor;
            }
        }

        /// <summary>
        /// Returns true if there is a supported text object attached to this writer.
        /// </summary>
        public virtual bool HasTextObject()
        {
            return (textUI != null || inputField != null || textMesh != null || textComponent != null);
        }

        /// <summary>
        /// Returns true if the text object has rich text support.
        /// </summary>
        public virtual bool SupportsRichText()
        {
            if (textUI != null)
            {
                return textUI.supportRichText;
            }
            if (inputField != null)
            {
                return false;
            }
            if (textMesh != null)
            {
                return textMesh.richText;
            }
            return false;
        }

        #endregion

        #region IDialogInputListener implementation

        public virtual void OnNextLineEvent()
        {
            inputFlag = true;

            if (isWriting)
            {
                NotifyInput();
            }
        }

        #endregion
    }
}
 
Orcan
UNITрон
 
Сообщения: 191
Зарегистрирован: 25 сен 2016, 04:22

Re: Как создать свои Дополнительные теги в Тексте

Сообщение Orcan 21 сен 2017, 21:28

и простенький енум
Синтаксис:
Используется csharp
  public enum TokenType
    {
        /// <summary> Invalid token type. </summary>
        Invalid,
        /// <summary> A string of words. </summary>
        Words,
        /// <summary> b </summary>
        BoldStart,              //
        /// <summary> /b </summary>
        BoldEnd,
        /// <summary> i </summary>
        ItalicStart,
        /// <summary> /i </summary>
        ItalicEnd,
        /// <summary> color=red </summary>
        ColorStart,
        /// <summary> /color </summary>
        ColorEnd,
        /// <summary> size=20 </summary>
        SizeStart,
        /// <summary> /size </summary>
        SizeEnd,
        /// <summary> w, w=0.5 </summary>
        Wait,
        /// <summary> wi </summary>
        WaitForInputNoClear,
        /// <summary> wc </summary>
        WaitForInputAndClear,
        /// <summary> wp, wp=0.5 </summary>
        WaitOnPunctuationStart,
        /// <summary> /wp </summary>
        WaitOnPunctuationEnd,
        /// <summary> c </summary>
        Clear,
        /// <summary> s, s=60 </summary>
        SpeedStart,
        /// <summary> /s </summary>
        SpeedEnd,
        /// <summary> x </summary>
        Exit,
        /// <summary> m=MessageName </summary>
        Message,
        /// <summary> vpunch=0.5 </summary>
        VerticalPunch,
        /// <summary> hpunch=0.5 </summary>
        HorizontalPunch,
        /// <summary> punch=0.5 </summary>
        Punch,
        /// <summary> flash=0.5 </summary>
        Flash,
        /// <summary> audio=Sound </summary>
        Audio,
        /// <summary> audioloop=Sound </summary>
        AudioLoop,
        /// <summary> audiopause=Sound </summary>
        AudioPause,
        /// <summary> audiostop=Sound </summary>
        AudioStop
    }

    /// <summary>
    /// Represents a token of story text. The text is broken into a list of tokens.
    /// Представляет маркер текста истории. Текст разбит на список маркеров.
    /// </summary>
    public class TextTagToken
    {
        #region Public members

        /// <summary>
        /// The type of the token.
        /// </summary>
        public TokenType type = TokenType.Invalid;

        /// <summary>
        /// List of comma separated parameters.Список параметров, разделенных запятыми.
        /// </summary>
        public List<string> paramList;

        #endregion
    }
Orcan
UNITрон
 
Сообщения: 191
Зарегистрирован: 25 сен 2016, 04:22

Re: Как создать свои Дополнительные теги в Тексте

Сообщение samana 21 сен 2017, 21:38

Я испугался бы такого длинного и незнакомого кода и поленившись в нём разбираться - начал попытки просто делать что-то своё.
Аватара пользователя
samana
Адепт
 
Сообщения: 4738
Зарегистрирован: 21 фев 2015, 13:00
Откуда: Днепропетровск

Re: Как создать свои Дополнительные теги в Тексте

Сообщение Orcan 21 сен 2017, 22:58

я хочу посмотреть как делается это правильно (так как способ много функциональный и прошёл множество испытаний) про теги я уже давно думал как их делают интересно же. Так же=много методов мне не известных тут хочется перенять опыт. будем расшифровывать. Так же в расшифровке чужого кода опыт получу. Надеюсь за день разберусь что тут к чему (там ещё пару скриптов связаны конечно) Тем более тут половину кода можно не смотреть так как там методы действий тегов. Надеюсь я не переоценил свои возможности :D
Orcan
UNITрон
 
Сообщения: 191
Зарегистрирован: 25 сен 2016, 04:22


Вернуться в Общие вопросы

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 11