开始制作好友系统了, 发现有一个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<RectTransform> scrollViewList = new List<RectTransform>(); 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<RectTransform>();
scrollViewList.Add(rt);
rt.gameObject.SetActive(false);
rt.transform.SetParent(this.transform,false);
} thisRT = this.GetComponent<RectTransform>();
} private void BtnDropClickEvent(GameObject go)
{
RectTransform rt = go.GetComponent<RectTransform>();
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<RectTransform>();
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<EventTriggerListener>(); if (listener == null)
listener = go.AddComponent<EventTriggerListener>(); return listener;
} public override void OnPointerClick(PointerEventData eventData)
{
if (OnClick != null)
OnClick(gameObject);
}
}

 

层次图:

UGUI 下拉滚动框的更多相关文章

  1. java、easyui-combotree树形下拉选择框

    最近一直在研究这个树形的下拉选择框,感觉非常的有用,现在整理下来供大家使用: 首先数据库的表架构设计和三级菜单联动的表结构是一样,(父子关系) 1.下面我们用hibernate建一下对应的额实体类: ...

  2. FancySelect – 更好用的 jQuery 下拉选择框插件

    FancySelect 这款插件是 Web 开发中下拉框功能的一个更好的选择.FancySelect 使用方便,只要绑定页面上的任何 Select 元素,并调用就 .fancySelect() 就可以 ...

  3. [js开源组件开发]模拟下拉选项框select

    模拟下拉选项框select 在css3流行的情况下,下拉框还是无法满足PD的需求,所以有了autosearch,有了模拟下拉框.效果如下图: select DEMO请案例点击这里查看.http://w ...

  4. HTML、CSS小知识--兼容IE的下拉选择框select

    HTML <div class="s_h_ie"> <select id="Select1" disabled="disabled& ...

  5. Bootstrap系列 -- 15. 下拉选择框select

    Bootstrap框架中的下拉选择框使用和原始的一致,多行选择设置multiple属性的值为multiple.Bootstrap框架会为这些元素提供统一的样式风格 <form role=&quo ...

  6. CSS自定义select下拉选择框(不用其他标签模拟)

    今天群里有人问到怎么自定义select下拉选择框的样式,于是群里就展开了激烈的讨论,刚开始一直就是考虑怎样使用纯CSS实现,把浏览器默认的样式覆盖掉,但最后均因兼容问题处理不好而失败告终,最后的解决方 ...

  7. 基于jQuery美化联动下拉选择框

    今天给大家介绍一款基于jQuery美化联动下拉选择框.这款下下拉选择框js里自带了全国所有城市的数数库.下拉选择框适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲 ...

  8. Bootstrap-风格的下拉按框:Bootstrap Select

    Bootstrap Select 是一个jQuery插件,提供了Bootstrap 风格的下拉选择框.拥有许多自定义的选项,可多选. 效果图: 源代码: <select class=" ...

  9. ul+jquery自定义下拉选择框

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

随机推荐

  1. codeforces Arrival of the General 题解

    A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command o ...

  2. [ES6] Function Params

    1. Default Value of function param: The function displayTopicsPreview() raises an error on the very ...

  3. iOS 画音频波形曲线 根据音频数据版

    效果图 DrawView.h #import <UIKit/UIKit.h> @interface DrawView : UIView @property shortshort *draw ...

  4. mysql索引简单介绍

    索引从本质上来说也是一种表,这样的表存储被列为索引的列项值和指向真正完整记录的指针.索引对用户透明.仅仅被数据库引擎用来加速检索真实记录.有索引的表.insert和update操作会耗费很多其它时间而 ...

  5. [Linux 小技巧] Ubuntu 14.04 下编译、安装、配置最新开发版 GoldenDict

    1. 背景介绍 GoldenDict 是一款非常优秀的跨平台电子词典软件,支持 StarDict.Babylon 等多种词典.其 PC 版基于 Qt 编写,完全免费.开源且没有广告.GoldenDic ...

  6. 输入内容, 列出可选的项: QComboBox

    #include "widget.h" #include "ui_widget.h" #include <QtGui/QCompleter> #in ...

  7. django: db howto - 1

    以在 Django 中使用 MySQL 为例,首先要安装 MySQL 和 MySQL-python 组件,确保 python 能执行 import MySQLdb. MySQL 中创建数据库: [ro ...

  8. 递归生成树对象,应用于Easyui,Tree控件

    1.生成树节点对象 /// <summary> /// 生成树的节点 /// </summary> public class TreeNode { public TreeNod ...

  9. (转)C#中的委托,匿名方法和Lambda表达式

    简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个Fir ...

  10. css3滚动提示

    <css揭秘>书中,滚动提示的实现 <!DOCTYPE html> <html lang="en"> <head> <meta ...