前言
在开发中,会遇到某种列表的元素需要顺序调整的功能,在查看完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
关注
打赏
热门博文
- Unity3d 插件着色器(Shader)报错Too many texture interpolators...和‘vert‘: output parameter ‘o‘ not ...修复记录
- Unity3d C# 网址url未转义问题(https%3A%2F%2F => https://)网址无法打开问题 URL is missing a protocol e.g. http://...
- Unity3d C# 使用Universal Media Player(ump)插件播放视频的众坑之无法播放视频和VLC播放器依赖的问题
- 软考高级软件架构风格定义以及分类
- Unity 使用NVIDIA FleX for Unity插件实现制作软体、水流流体、布料等效果学习教程
- Unity3d 使用统一可寻址资产系统(Addressables)实现服务器资源热更新和资源版本管理等功能(含源码)
- Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
- Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法
- Unity3d C# 开发微信小游戏分享图片、朋友圈等功能实现(含源码)
- Unity3d Windows播放视频(视频流)功能组/插件支持对比