C# c将for循环中的值分配给各个变量
我有单元格的值和for循环中的值。例如,当i=0时,值将是我要分配给A0Text.text的第一个值。当i=1时,该值将是我要分配给A1Text.text的第二个值C# c将for循环中的值分配给各个变量,c#,for-loop,unity3d,C#,For Loop,Unity3d,我有单元格的值和for循环中的值。例如,当i=0时,值将是我要分配给A0Text.text的第一个值。当i=1时,该值将是我要分配给A1Text.text的第二个值 for (int i = 0; i < BLOCKS.Count; i++) { //possible values of Cell are A0, A1, A2 ... var Cell = currentBLOCKData["cell"]; var Value = currentBLOCKData["value"];
for (int i = 0; i < BLOCKS.Count; i++)
{
//possible values of Cell are A0, A1, A2 ...
var Cell = currentBLOCKData["cell"];
var Value = currentBLOCKData["value"];
....
我想分配
A0Text.text = First value in above field - Value
A1Text.text = 2nd value in above field - Value
A2Text.text = 3rd value in above field - Value
我如何进行上述分配?是-我找到了-搜索和分配如下:
txtRef = GameObject.Find(cellName).GetComponent<Text>();
txtRef.text = Value.ToString();
查找可能相当慢,我认为它效率很低。在你可以避免使用它的地方,尽你最大的努力。为什么不为文本组件创建一个公共数组,在检查器中分配它们,然后在循环中遍历数组呢
public Text[] textArray; //assign in inspector
for(int i = 0; i < BLOCKS.Count; i++){
//.. your other code here
textArray[i].text = Value.ToString();
}
使用模%运算符,i%长度,然后重新运行值始终在边界内。所以你不需要40个这样的任务。你能详细说明你的答案吗?例如,当i=0时,值将是我要分配给A0Text.text的第一个值。当i=1时,值将是我要分配给A1Text.text.的第二个值。您可以搜索单元格并将值设置为该值,谢谢-听起来不错,但是脚本是否需要分配给每个文本标签?在这种情况下,这可能比GameObject效率低。找到了吗?我还得到了一个超出范围的数组异常。反过来说:每个文本标签都需要分配给脚本。你能给我看看你的密码吗?你需要帮助避免这个异常吗?我不得不退一步,因为当我在android上测试时,我甚至不能读取json文件,因为我使用Application.Datapath。你能帮我回答这个问题吗。谢谢
public Text[] textArray; //assign in inspector
for(int i = 0; i < BLOCKS.Count; i++){
//.. your other code here
textArray[i].text = Value.ToString();
}