首先是效果图:
单个item的
每个item的位置排序根据:
public static Vector2 GetPosByIndex(int index)
{ //宽度都为75,需要和Layout Element配合使用
return new Vector2(0, -75 * index);
}通过资源载入的item,这个只是生成了GameObject,还咩有对其进行位置调整
static GameObject _downloadItem;
static GameObject downloadItem
{
get
{
if (_downloadItem == null)
_downloadItem = GameObject.Instantiate(Resources.Load("offlineMapItem")) as GameObject;
return _downloadItem;
}
}
对item限制高宽,位置的调整
public static void SetMapDataItemView(MapDataItem item, GameObject root, Vector2 pos)
{
RectTransform rt = GameObject.Instantiate(downloadItem, root.transform).transform as RectTransform;
rt.anchoredPosition = pos;
rt.offsetMax = new Vector2(0, rt.offsetMax.y);
rt.offsetMin = new Vector2(0, rt.offsetMin.y);
rt.localScale = Vector3.one;
rt.GetComponent<OfflineMapItem>().Show(item);//进行每个item的数据绑定
}开始创建item
public void Test() {
int mainIndex = 0;
foreach (var item in downloadedList)
{
MapDownloadHelper.SetMapDataItemView(item, mainRoot,GetPosByIndex(mainIndex));
mainIndex++;
}
}单个Item的数据类
public class OfflineMapItem : MonoBehaviour
{
public Text mTitle;
public Text mSize;
public Button mDownloadButton;
public Button mDeletedButton;
public Slider mProgress;
MapDataItem data;
private bool isDownload = false;
public void Show(MapDataItem item)
{
data = item;
mTitle.text = data.getName();
mSize.text = data.getSize();
mDownloadButton.onClick.AddListener(OnDownloadClick);
mDeletedButton.onClick.AddListener(OnDeletedClick);
}
本文介绍了一种在Unity中实现UI布局的方法,通过自定义脚本动态加载和定位UI元素,并展示了如何根据不同的索引调整UI元素的位置。
1460

被折叠的 条评论
为什么被折叠?



