简体   繁体   English

使用组件的长度分配字符串数组

[英]Assign array of string with the Length of a component

How do I automatically assign the length of an array based on the length of another and push a string to it?如何根据另一个数组的长度自动分配一个数组的长度并将一个字符串推送给它? Such that in for loop when I copy the string, it will automatically get copied to all the array strings.这样当我复制字符串时在 for 循环中,它会自动复制到所有数组字符串。

public string[] ArrayOfStrings;
public Image[] images;

void Start()
{
    for(int i = 0; i < images.Length; i++)
    {
        ArrayOfStrings[i] = "Testing String";
    }
}
ArrayOfStrings = new string[images.Length];

Then you can do the loop然后你可以做循环

for(int i = 0; i < ArrayOfStrings.Length; i++)
{
    ArrayOfStrings[i] = "Testing String";
}

Or you can use List and linq. For example:或者您可以使用List和 linq。例如:

public List<string> ArrayOfStrings;
public List<Image> Images;

void Start()
{
    ArrayOfStrings.AddRange(Images.Select(x => $"the text you want for {x}").ToList());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM