📜  统一列表视图 - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:24.110000             🧑  作者: Mango

代码示例1
/*
The "easiest" and most elegant way I can think about is using a Vertical Layout Group

Add a new empty gameobject under your canvas
Set the desired dimensions using the RectTransform component
Attach the "Vertical Layout Group" component
In your code, for each string in your list :
Create a new GameObject :
Attach a text component to it
Fill the text attribute with your string
Set the parent of the transform to be the first empty gameobject in 1st step
Here is a piece of code I haven't tested :*/

// Drag & Drop the vertical layout group here
public UnityEngine.UI.VerticalLayoutGroup verticalLayoutGroup ;

// ... In your function
RectTransform parent = verticalLayoutGroup.GetComponent() ;
for( int index = 0 ; index < stringList.Count ; ++index )
{
     GameObject g = new GameObject( stringList[index] ) ;
     UnityEngine.UI.Text t = g.AddComponent();
     t.addComponent().setParent( parent ) ;
     t.text = stringList[index] ;
}
/*
If you need more customization, you can instantiate a prefab instead of
manually create the texts gameobjects.*/