您当前的位置: 首页 >  unity

十幺卜入

暂无认证

  • 4浏览

    0关注

    119博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity3d C#实现UI节点切换顺序SiblingIndex(上移、下移和快速置顶、置底)等功能(含源码)

十幺卜入 发布时间:2021-06-30 15:43:37 ,浏览量:4

前言

在开发中,会遇到某种列表的元素需要顺序调整的功能,在查看完transform节点的操作发现用自带的内置函数可以直接实现上述功能的UI节点切换,至于数据需要自己进行替换。

效果

在这里插入图片描述

接口函数


//
// 摘要:
//     Move the transform to the start of the local transform list.
 public void SetAsFirstSibling();
//
// 摘要:
//     Move the transform to the end of the local transform list.
public void SetAsLastSibling();
//
// 摘要:
//     Sets the sibling index.
//
// 参数:
//   index:
//     Index to set.
public void SetSiblingIndex(int index);

如上函数: SetAsFirstSibling是快速置顶,等同于SetSiblingIndex(0)。 SetAsLastSibling是快速置底,等同于SetSiblingIndex(兄弟节点数 - 1)。 SetSiblingIndex 直接设置节点的下标。

功能实现

功能实现就是UI配合如上的三个接口,实现上移、下移、置顶、置底等功能。

UI搭建

UI的搭建如图: 在这里插入图片描述

主要是一个列表中有几个节点,每个节点有几个操作按钮,上移下移等。

编码实现

直接上代码:



using UnityEngine;

public class SiblingTest : MonoBehaviour
{
    public void ClickTop(GameObject obj) {
        obj.transform.SetAsFirstSibling();
    }
    public void ClickBottom(GameObject obj)
    {
        obj.transform.SetAsLastSibling();
    }
    public void ClickMoveUp(GameObject obj)
    {
        int idx = obj.transform.GetSiblingIndex();
        if (idx > 0)
            obj.transform.SetSiblingIndex(idx - 1);
    }
    public void ClickMoveDown(GameObject obj)
    {
        int idx = obj.transform.GetSiblingIndex();
        int count = obj.transform.parent.childCount;
        if(idx             
关注
打赏
1663314737
查看更多评论
0.1186s