博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UGUI 下拉滚动框
阅读量:4959 次
发布时间:2019-06-12

本文共 2631 字,大约阅读时间需要 8 分钟。

开始制作好友系统了, 发现有一个UI跟QQ的面板一模一样. 于是就写了一个公共的下拉滚动框.需要把按钮的中心点(pivot.y = 1),描点为最上方 直接上图吧

代码如下:

using UnityEngine;using System.Collections;using UnityEngine.UI;using UnityEngine.EventSystems;using System.Collections.Generic;using UnityEngine.Events;public class DropScroll : MonoBehaviour {    [SerializeField]    private Button[] btnList;               //提供多个按钮    private RectTransform thisRT;    [SerializeField]    private GameObject scrollViewTmp;       //提供一个滚动视图模版    private List
scrollViewList = new List
(); public void Start() { Init(); } public void Init() { RectTransform rt = null; foreach (var btn in btnList) { EventTriggerListener.Get(btn.gameObject).OnClick += BtnDropClickEvent; rt = GameObject.Instantiate(scrollViewTmp).GetComponent
(); scrollViewList.Add(rt); rt.gameObject.SetActive(false); rt.transform.SetParent(this.transform,false); } thisRT = this.GetComponent
(); } private void BtnDropClickEvent(GameObject go) { RectTransform rt = go.GetComponent
(); RectTransform btnRt = null; float height = thisRT.sizeDelta.y; int index = rt.GetSiblingIndex(); HideAllScrollView(); scrollViewList[index].sizeDelta = new Vector2(thisRT.sizeDelta.x, height - btnList.Length * rt.sizeDelta.y); scrollViewList[index].anchoredPosition = new Vector2(0, -((index + 1) * rt.sizeDelta.y)); scrollViewList[index].gameObject.SetActive(true); for (int i = 0; i < btnList.Length; i++) { btnRt = btnList[i].GetComponent
(); if (i > index) { btnRt.anchoredPosition = new Vector2(btnRt.anchoredPosition.x, -height + ((btnList.Length - i) * btnRt.sizeDelta.y)); } else { btnRt.anchoredPosition = new Vector2(btnRt.anchoredPosition.x, -(i * btnRt.sizeDelta.y)); } } } private void HideAllScrollView() { for (int i = 0; i < scrollViewList.Count; i++) { scrollViewList[i].gameObject.SetActive(false); } }}public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger { public delegate void VoidDelegate(GameObject go); public event VoidDelegate OnClick; static public EventTriggerListener Get(GameObject go) { EventTriggerListener listener = go.GetComponent
(); if (listener == null) listener = go.AddComponent
(); return listener; } public override void OnPointerClick(PointerEventData eventData) { if (OnClick != null) OnClick(gameObject); }}

 

层次图:

转载于:https://www.cnblogs.com/plateFace/p/5364026.html

你可能感兴趣的文章
20180418小测
查看>>
Spring Cloud是怎么运行的?
查看>>
12 联结表
查看>>
数字三角形
查看>>
NGUI 减少drawcall规则
查看>>
xss攻击
查看>>
使用Calendar加一天,减一天
查看>>
MVC中AuthorizeAttribute用法并实现权限控制
查看>>
Broken pipe错误终极解释
查看>>
oracle数据库基本命令
查看>>
WLAN热点创建
查看>>
开发环境中快速部署Oracle Essbase(Rapid deployment of oracle essbase in development envrioments)...
查看>>
Lodop Web打印插件使用
查看>>
sha1 加密 2...
查看>>
[GX/GZOI2019]旧词(树上差分+树剖+线段树)
查看>>
第509篇-Delegate和Event异同--(内容篇5:共6篇)
查看>>
设计模式--6大原则--开闭原则
查看>>
高德地图JSapi
查看>>
团队协作第八周个人PSP
查看>>
centos-linux热拔插scsi硬盘
查看>>