Unity实现QQ列表折叠菜单

编辑: admin 分类: c#语言 发布时间: 2022-01-18 来源:互联网

本文实例为大家分享了Unity实现QQ列表折叠菜单的具体代码,供大家参考,具体内容如下

主要用到了GUI的自动布局功能,VerticalLayoutGroup,注意ChildControlsSize 和ChildForceExpand属性设置为 Width

效果:

实现代码:

/// <summary>
/// 折叠菜单
/// </summary>
public class FoldPanel : MonoBehaviour
{
  [SerializeField]
  private GameObject panelItem; // 折叠页
  [SerializeField]
  private TitleItem titleItem;
  [SerializeField]
  private DataItem dataItem;

  public List<FoldData> dataList = new List<FoldData>();

  private void Start()
  {
    Create();
  }

  public void Create()
  {
    for (int i = 0; i < dataList.Count; i++)
    {
      // 创建标题
      TitleItem title = Instantiate(titleItem).GetComponent<TitleItem>();
      title.SetTitle(dataList[i].titleName);
      title.transform.SetParent(this.transform);

      // 创建子折叠面板
      GameObject panel = Instantiate(panelItem);
      panel.transform.SetParent(this.transform);
      // 260是折叠页的宽度,30DataItem的高度
      panel.GetComponent<RectTransform>().sizeDelta = new Vector3(260,30 * dataList[i].data.Count);
      title.SetFoldPanel(panel)【本文由:http://www.1234xp.com/ip.html 提供,感谢支持】;
      panel.SetActive(false);

      // 创建折叠页数据
      for (int j = 0; j < dataList[i].data.Count; j++)
      {
        DataItem item = Instantiate(dataItem).GetComponent<DataItem>();
        item.transform.SetParent(panel.transform);
        item.SetInfo(dataList[i].data[j]);
      }
    }
  }
}

[System.Serializable]
public class FoldData
{
  public string titleName;
  public List<ItemData> data;
}

[System.Serializable]
public class ItemData
{
  public string userName;
  //public string imageName;
  public Sprite imageName;
}

TitleItem.cs

public class TitleItem : MonoBehaviour,IPointerClickHandler
{
  [SerializeField]
  private Text title;
  [SerializeField]
  private Transform arrow;

  public bool isFold = true; // 是否是折叠状态
  public Transform foldPanel; 

  public void OnPointerClick(PointerEventData eventData)
  {
    if (isFold)
    {
      isFold = false;

      arrow.DORotate(Vector3.zero, 0.1f);

      if (foldPanel != null)
      {
        foldPanel.gameObject.SetActive(true);
        foldPanel.DOScaleY(1, 0.1f);
      }
    }
    else
    {
      isFold = true;
      arrow.DORotate(new Vector3(0, 0, 90), 0.1f);
      
      if (foldPanel != null)
      {
        foldPanel.DOScaleY(0, 0.1f).OnComplete(() => { foldPanel.gameObject.SetActive(false); });
      }
    }
  }

  public void SetTitle(string _titleName)
  {
    title.text = _titleName;
  }

  public void SetFoldPanel(GameObject panel)
  {
    foldPanel = panel.transform;
  }
}

列表的数据来源是在界面上手动配置的,当然如果想要读取本地或者服务器的数据也是可以的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。