当前位置:   article > 正文

Unity Text一个简单的输入特效_unity 给text 添加控制代码

unity 给text 添加控制代码

这个脚本是来模拟一个键盘输入的效果,如下图

而且同样适用于英文,如图

主要实现是把当前的Text先存入一个变量中,然后往当前Text加随机某个字符到时间后再还原至之 前的Text,本人是业余选手,代码可能比较不规范。

  1. //加入随机字符后重置为原来的字符
  2. targetText.text += thisText[Random.Range(0, textNum)];
  3. if (isNeedInputLine)
  4. {
  5. targetText.text += InputLine;
  6. }
  7. yield return new WaitForSeconds(randomTextPerTime);

参数有相应的解释,只需要将脚本挂到Text身上,并在Text文本框输入想有效果的内容即可

InputLineStatus--如果打开此选项,则会有输入线闪烁

InputLine--输入线的文本,建议为 |或者▐ ,当然其他的字符也是可行的

InputLinePerTime--每次输入线闪烁所需要的时间

RandomTimes--每个字出现之前所需要随机的次数

RandomTextPerTime--每次随机需要的时间

最后推荐几个看着比较舒服的参数 左边为中文 右边为英文

脚本:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class TextEffectScripts : MonoBehaviour
  6. {
  7. private Text targetText;
  8. private string thisText;
  9. private int textNum;
  10. private int textIndex;
  11. private int textStaus;
  12. private char textChar;
  13. private bool textHaveFinished;
  14. private bool isNeedInputLine;
  15. private int textInputLine;
  16. IEnumerator textEffectIEnumerator;
  17. [Header("InputLineStatus/是否需要输入线滞留")]
  18. public bool InputLineStatus;
  19. [Header("InputLine/输入线文本不需要留空")]
  20. public string InputLine;
  21. [Header("InputLinePerTime/输入线闪烁时间")]
  22. public float InputLineTime;
  23. [Header("RandomTimes/每字体所需随机次数")]
  24. [Range(0,20)]
  25. public int randomTimes;
  26. [Header("RandomTextPerTime/每字体所需随机时间")]
  27. [Range(0, 1)]
  28. public float randomTextPerTime;
  29. private string thisTextBefore;
  30. // Start is called before the first frame update
  31. void Start()
  32. {
  33. textEffectIEnumerator = TimerTextEffect();
  34. if (InputLine == string.Empty)
  35. {
  36. isNeedInputLine = false;
  37. }
  38. else if(InputLine.Trim() != string.Empty)
  39. {
  40. isNeedInputLine = true;
  41. }
  42. targetText = this.GetComponent<Text>();
  43. thisText = targetText.text;
  44. textNum = thisText.Length;
  45. targetText.text = string.Empty;
  46. thisTextBefore = string.Empty;
  47. StartCoroutine(textEffectIEnumerator);
  48. }
  49. IEnumerator TextInputLineIEnumerator()
  50. {
  51. while (true)
  52. {
  53. textInputLine++;
  54. if (textInputLine == 1)
  55. {
  56. targetText.text += InputLine;
  57. }
  58. else if(textInputLine == 2)
  59. {
  60. targetText.text = thisTextBefore;
  61. }
  62. else
  63. {
  64. textInputLine = 0;
  65. }
  66. yield return new WaitForSeconds(InputLineTime);
  67. }
  68. }
  69. IEnumerator TimerTextEffect()
  70. {
  71. while (!textHaveFinished)
  72. {
  73. //判断循环次数
  74. textStaus++;
  75. if (textStaus != randomTimes+1)
  76. {
  77. targetText.text = thisTextBefore;
  78. }
  79. targetText.text = thisTextBefore;
  80. if (textStaus <= randomTimes)
  81. {
  82. //加入随机字符后重置为原来的字符
  83. targetText.text += thisText[Random.Range(0, textNum)];
  84. if (isNeedInputLine)
  85. {
  86. targetText.text += InputLine;
  87. }
  88. yield return new WaitForSeconds(randomTextPerTime);
  89. }
  90. else
  91. {
  92. if (textIndex != textNum)
  93. {
  94. targetText.text += thisText[textIndex];
  95. textStaus = 0;
  96. thisTextBefore = targetText.text;
  97. textIndex++;
  98. if (randomTimes == 0)
  99. {
  100. yield return new WaitForSeconds(randomTextPerTime);
  101. }
  102. }
  103. else
  104. {
  105. if (isNeedInputLine)
  106. {
  107. if (InputLineStatus)
  108. {
  109. StartCoroutine(TextInputLineIEnumerator());
  110. }
  111. }
  112. textHaveFinished=true;
  113. yield break;
  114. //结束字体效果
  115. }
  116. }
  117. }
  118. }
  119. }

只需要复制然后挂到Text脚本上即可使用。

最后这是Github地址:

https://github.com/WeekenicX/UnityTextEInputEffect

 谢谢您看到最后,如果有问题欢迎提出来。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/496854
推荐阅读
相关标签
  

闽ICP备14008679号