Unity 之 UGUI Dropdown下拉控件展开方向控制
有个需要控制下拉控件展开方向的需求,不得探索一下这个下拉方向是由那些属性控制的。
其实我们正常使用的时候你可以发现,下拉控件默认向下展开,当向下展开显示不全时会自动向上展示,效果如下:
默认状态:
向下展开显示不开状态(下拉超出边界):
经过多次调试,我发现这个下拉方向是可以根据“Template” 的RectTransform属性PosY的修改,对其显示位置进行调整。
结论:Pos Y 值为生成下拉列表位置距原控件的间隔。
示例如下图: 下图分别是Pos Y值为0,-20, 110是控件显示结果;
若通过计算Pos Y值来控制下拉控件显示位置,不太可行;因为实际项目中下拉控件个数和间隔都不是确定的,每个都进行这种计算明显是不可取的;
那么这个问题要怎么解决呢,如果这个问题能使用Pos Y这个方式来达到效果,那么是否可以通过锚点和坐标的方式进行配合来实现效果呢?
经过测试我发现是可以的,将"Template"的Pivot属性Y值设置为0,然后将锚点Min ,Max分别调至:(0,1),(1,1),最后将==Pos Y ==调至0,则可实现向上展开。效果图如下:
对比下向下展开时的各个属性值:
上述操作的代码实现:
using UnityEngine;
using UnityEngine.UI;
public class DropDownTest : MonoBehaviour {
//下拉控件
public Dropdown drop;
void Start ()
{
// 调整为默认向上展开
drop.template.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0);
drop.template.GetComponent<RectTransform>().anchorMin = new Vector2(0, 1);
drop.template.GetComponent<RectTransform>().anchorMax = Vector2.one;
drop.template.GetComponent<RectTransform>().anchoredPosition = Vector3.zero; // 调整为默认向下展开
//drop.template.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 1);
//drop.template.GetComponent<RectTransform>().anchorMin = Vector2.zero;
//drop.template.GetComponent<RectTransform>().anchorMax = new Vector2(1, 0);
//drop.template.GetComponent<RectTransform>().anchoredPosition = Vector3.zero;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
相关推荐:
Unity 之 UGUI Dropdown组件使用简析
Unity 之 UGUI Dropdown下拉选单组件详解
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/108416542
- 点赞
- 收藏
- 关注作者
评论(0)