wangguan
2020-12-24 c5679cb58551843e74d31a839de649f16885fa8b
Assets/Scripts/Guide/CharForeach.cs
@@ -12,138 +12,139 @@
    private Text text;
    public delegate void CallBack();
    private Action callBack;
    private Action callBackFinal = null;
    RectTransform contentRect;
    Vector2 offsetV;
    void Awake()
    {
        contentRect = transform.Find("Scroll View/Viewport/Content").GetComponent<RectTransform>();
        text = transform.Find("Scroll View/Viewport/Content/Text").GetComponent<Text>();
        offsetV = new Vector2(0, 20);
        text = transform.Find("Text").GetComponent<Text>();
        transform.Find("BGPanel").GetComponent<Button>().onClick.AddListener(OnClickBtn);
    }
    public void StartShowWord(string str, Action cb)
    {
        Debug.Log("StartShowWord" + str);
        word = str;
        text.text = "";
        callBack = cb;
        if (contentRect.anchoredPosition.y != 0)
        {
            contentRect.anchoredPosition = Vector2.zero;
        }
        StartCoroutine("TypeText");
    }
    string[] wordArray;
    int arrayIndex;
    bool isStarting;
    public void StartShowWord(string[] str, Action cb)
    {
        if (!isStarting)
        {
            wordArray = str;
            arrayIndex = 0;
            word = wordArray[arrayIndex];
            text.text = "";
            callBack = cb;
            StartCoroutine("TypeText");
        }
    }
    public void StartFinalShowWord(string[] str, Action cb, Action cb2)
    {
        if (!isStarting)
        {
            wordArray = str;
            arrayIndex = 0;
            word = wordArray[arrayIndex];
            text.text = "";
            callBack = cb;
            callBackFinal = cb2;
            StartCoroutine("TypeText");
        }
    }
    private void OnClickBtn()
    {
        if (isStarting)
        {
            ShowWordImmediately();
        }
        else
        {
            if (wordArray != null && arrayIndex != wordArray.Length - 1)
            {
                arrayIndex++;
                word = wordArray[arrayIndex];
                text.text = "";
                StartCoroutine("TypeText");
            }
            else
            {
                if (callBack != null)
                {
                    callBack();
                }
            }
        }
    }
    public void ShowWordImmediately()
    {
        Debug.Log("ShowWordImmediately");
        StopCoroutine("TypeText");
        text.text = "";
        char[] charArray = word.ToCharArray();
        length = 0;
        for (int i = 0; i < charArray.Length; i++)
        if (isStarting)
        {
            if (charArray[i].Equals('$'))
            StopCoroutine("TypeText");
            isStarting = false;
            text.text = "";
            char[] charArray = word.ToCharArray();
            for (int i = 0; i < charArray.Length; i++)
            {
                text.text += GuideConfig.showWordsUBB[0];
                length += 19;
                SetText(charArray[i]);
            }
            else if (charArray[i].Equals('%'))
            {
                length += 19;
                text.text += GuideConfig.showWordsUBB[1];
            }
            else if (charArray[i].Equals('`'))
            {
                length += 23;
                text.text += GuideConfig.showWordsUBB[2];
            }
            else if (charArray[i].Equals('&'))
            {
                length += 19;
                text.text += GuideConfig.showWordsUBB[3];
            }
            else if (charArray[i].Equals('*'))
            {
                length += 23;
                text.text += GuideConfig.showWordsUBB[4];
            }
            else
            {
                text.text += charArray[i];
            }
            callBackFinal?.Invoke();
        }
        //text.text = word;
        if (text.text.Length - length >= 39 && contentRect.anchoredPosition != offsetV)
        {
            contentRect.anchoredPosition = offsetV;
        }
        else
        {
            contentRect.anchoredPosition = Vector2.zero;
        }
        if (callBack != null)
        {
            callBack();
            callBack = null;
        }
    }
    int length = 0;
    private IEnumerator TypeText()
    {
        isStarting = true;
        char[] charArray = word.ToCharArray();
        length = 0;
        for (int i = 0; i < charArray.Length; i++)
        {
            if (charArray[i].Equals('$'))
            {
                text.text += GuideConfig.showWordsUBB[0];
                length += 19;
            }
            else if (charArray[i].Equals('%'))
            {
                length += 19;
                text.text += GuideConfig.showWordsUBB[1];
            }
            else if (charArray[i].Equals('`'))
            {
                length += 23;
                text.text += GuideConfig.showWordsUBB[2];
            }
            else if (charArray[i].Equals('&'))
            {
                length += 19;
                text.text += GuideConfig.showWordsUBB[3];
            }
            else if (charArray[i].Equals('*'))
            {
                length += 23;
                text.text += GuideConfig.showWordsUBB[4];
            }
            else
            {
                text.text += charArray[i];
            }
            if (text.text.Length - length >= 39 && contentRect.anchoredPosition != offsetV)
            {
                contentRect.anchoredPosition = offsetV;
            }
            SetText(charArray[i]);
            yield return new WaitForSeconds(letterPause);
        }
        isStarting = false;
        callBackFinal?.Invoke();
        yield break;
    }
        if (callBack != null)
    private void SetText(char c)
    {
        if (c.Equals('$'))
        {
            callBack();
            callBack = null;
            text.text += GuideConfig.showWordsUBB[0];
        }
        else if (c.Equals('%'))
        {
            text.text += GuideConfig.showWordsUBB[1];
        }
        else if (c.Equals('`'))
        {
            text.text += GuideConfig.showWordsUBB[2];
        }
        else if (c.Equals('&'))
        {
            text.text += GuideConfig.showWordsUBB[3];
        }
        else if (c.Equals('*'))
        {
            text.text += GuideConfig.showWordsUBB[4];
        }
        else if (c.Equals('<'))
        {
            text.text += GuideConfig.showWordsUBB[5];
        }
        else
        {
            text.text += c;
        }
    }
}