Reputation: 543
I would like to output a list (C# Scripting), which contains elements of objects' names. Since this list is generating dynamacally, I just get a output in every single line. This is my code:
public static List<string> myList= new List<string>();
//...
public static void myMethod (Renderer[] renderers) {
foreach (var renderer in renderers) {
if (IsVisible(renderer)) {
//print (renderer.name + " is visible!");
myList.Add (renderer.name);
}
print (myList);
}
myList.Clear ();
print ("--");
}
The output is like System.Collections.Generic.List'1[System.String]
in every line.
After a reseach I also tried
foreach (var item in myList) {
print (item);
}
This method prints every object in a single line. But I want all names of the objects to be together in one line until there will be "--" printed. How can I realize it? Thanks!
Example:
output now:
object0
object1
object2
etc.
what I want to achive: object0, object1, object2, etc.
Upvotes: 0
Views: 307
Reputation: 645
If your output coming as a string then u have to take one string variable and concatenate all items with "--" into the variable.
string temp = string.Empty;
foreach(var item in items)
{
temp += item + "--";
}
print(temp)
Upvotes: 1
Reputation: 1086
You can print all the names of visible renderers with the following code using String.Join and LINQ
public static void myMethod(Renderer[] renderers)
{
print(string.Join(",", renderers.Where(IsVisible).Select(r => r.name)));
print("--");
}
This should output:
object1,object2,object3
--
Upvotes: 1
Reputation: 6155
You can use string.Join
for this.
string itemsInOneLine = string.Join(", ", myList);
print(itemsInOneLine);
For further information have a look at the MSDN string.Join page.
Upvotes: 0
Reputation: 1339
You can use String.Join method. It recevies the separator and an array of strings as parameters. For example:
print(String.Join(", ", myList.ToArray());
Upvotes: 1