我在制作一串字符数组以在 Unity 引擎中打印到屏幕时遇到问题
Posted
技术标签:
【中文标题】我在制作一串字符数组以在 Unity 引擎中打印到屏幕时遇到问题【英文标题】:I am having trouble with making a string of arrays of chars to print to screen in the Unity engine 【发布时间】:2021-12-05 03:33:03 【问题描述】:。
这里是代码
public string[] wordGoDownString = new string[12];
public void Start()
char[] chwordGoDownString = wordGoDownString.ToCharArray();
foreach (char ch in wordGoDownString)
//char[]wordGoDownString[0] = 'O';
//char[]wordGoDownString[1] = 'U';
//char[]wordGoDownString[2] = 'R';
//char[]wordGoDownString[3] = " ";
//char[]wordGoDownString[4] = 'P';
//char[]wordGoDownString[5] = 'R';
//
//Print.ToScreen(chars);
//Print.ToScreen(' ');
//wordGoDownString[6] =
//wordGoDownString[7] =
//wordGoDownString[8] =
//wordGoDownString[9] =
//wordGoDownString[10] =
//wordGoDownString[11] =
// [SerializeField] private string thatString = "b";
// string yellTheString;
//
// public void Start()
//
// foreach (char chars in thatString)
//
// char higherChars = char.ToUpper(chars);
// yellTheString += higherChars;
//
// Print.ToScreen(yellTheString);
//
// // YellTheString
//
//SerializeField] private char charLettersDown1 = 'O';
//
//rivate char string [] thatString = new string[12];
//
//ublic void Start()
//
//thatString[0] = 'O';
//thatString[1] = 'U';
//thatString[2] = 'R';
//thatString[3] = ' ';
//thatString[4] = 'P';
//thatString[5] = 'R';
//thatString[6] = 'I';
//thatString[7] = 'N';
//thatString[8] = 'C';
//thatString[9] = 'E';
//thatString[10] = 'S';
//thatString[11] = 'S';
//
//
//
/// Borrowing this from the school's code documents.
//
//
//SerializeField] private char charLettersDown1 = 'O';
//
//rivate char string [] thatString = new string[12];
//
//ublic void Start()
//
//thatString[0] = 'O';
//thatString[1] = 'U';
//thatString[2] = 'R';
//thatString[3] = ' ';
//thatString[4] = 'P';
//thatString[5] = 'R';
//thatString[6] = 'I';
//thatString[7] = 'N';
//thatString[8] = 'C';
//thatString[9] = 'E';
//thatString[10] = 'S';
//thatString[11] = 'S';
//
//
//
/// Borrowing this from the school's code documents.
//
//
//
(https://pastebin.com/gEJT2uXc)
【问题讨论】:
不要发布代码链接。而是在这里粘贴代码。 请使用正确的标签!请注意,unityscript
是或更好地曾经是一种 javascript 风格,类似于早期 Unity 版本中使用的自定义语言,并且现在已经不推荐使用了!这也是不是C++
!您的代码显然是 c#
...也仅仅因为您使用某个 IDE 并不意味着您的问题是关于特定的 IDE ...您应该使用 unity3d
虽然这是您的主要框架/API在这里使用...
你的大部分代码都被注释掉了..那么你到底想做什么,因为目前你的代码什么都不做......而不是分配多个字符......为什么不使用字符串@首先是987654328@?
【参考方案1】:
在我看来,您可能想要一个一次只显示一个字符的横幅,例如
public float delayPerChar = 1f;
public float delayBetweenWords = 1f;
public string[] wordGoDownString = new string[12];
private string currentWord;
private int currentWordIndex;
private int currentCharIndex;
private float timer;
private string currentPrint;
private void Update ()
timer += Time.deltaTime;
// Is currentWord null or whitespace?
if(string.NullOrWhiteSpace(currentWord))
// If that's the case we are currently between two words
// Has the delays exceeded?
if(timer >= delayBetweenWords)
// If so pick the next word from the list with wrap around at the end
currentWordIndex = (currentWordIndex + 1) % wordGoDownString.Length;
currentWord = wordGoDownString[currentWordIndex];
// Reset the chat index
currentCharIndex = 0;
// Reset the timer
timer = 0f;
// Reset the current Print
currentPrint = "";
// print the first char of the current word
currentPrint += currentWorld[currentCharIndex];
Print.PrintToScreen(currentPrint);
// Rest if the word is handled in the else case
else
// Go to the next char
currentCharIndex += 1;
currentPrint += currentWord [currentCharIndex];
// reset timer to wait for next char
timer = 0f;
// Is this the last in the current word?
if(currentCharIndex => currentWord.Length - 1)
// reset currentWord in order to go to next word
currentWord = null;
使用Coroutines 会更容易一些!
与 Coroutine 的代码基本相同
// Yes, if you make Start return IEnumerator then Unity automatically runs it as Coroutine
private IEnumerator Start ()
// This is OK in Coroutines as long as you yield somewhere inside
while(true)
// Simply iterate the words
foreach(var currentWord in wordGoDownString)
// For each word start with an empty print
var currentPrint = "";
// Simply iterate the chars
foreach(var currentChar in currentWord.ToCharArray())
// Append the char and print
currentPrint += currentChar;
Print.PrintToScreen(currentPrint);
// Then wait for the delay
yield return new WaitForSeconds (delayPerChar);
// after each word wait for the other delay
yield return new WaitForSeconds(delayBetweenWords);
【讨论】:
以上是关于我在制作一串字符数组以在 Unity 引擎中打印到屏幕时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章
什么是循环销售数据的JSON以在Unity中创建图形的最佳方法?
如何使用 C# 从字符串数组制作类似枚举的 Unity 检查器下拉菜单?