当前位置: 首页 > news >正文

unity中通过拖拽,自定义scroll view中子物体顺序

1.在每个content的子物体上挂载DragHandler脚本,并且添加Canvs Group组件,设置见图

2.DragHandler脚本内容:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System.Collections;public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{private RectTransform rectTransform;private Canvas canvas;private Transform parentTransform;private Vector2 startPosition;private int startSiblingIndex;private bool isDragging = false;private CanvasGroup canvasGroup;private int newIndex;private int top;//content的顶端间距private float spacing;//content的子物体的spacingprivate float height;//子物体的高度void Awake(){rectTransform = GetComponent<RectTransform>();height = rectTransform.sizeDelta.y;canvas = GetComponentInParent<Canvas>();parentTransform = transform.parent;VerticalLayoutGroup layout = parentTransform.GetComponent<VerticalLayoutGroup>();top = layout.padding.top;spacing = layout.spacing;// 获取或添加 CanvasGroup 组件canvasGroup = GetComponent<CanvasGroup>();if (canvasGroup == null){canvasGroup = gameObject.AddComponent<CanvasGroup>();}}public void OnBeginDrag(PointerEventData eventData){if (eventData.button != PointerEventData.InputButton.Left) return;// 记录初始位置和索引startPosition = rectTransform.anchoredPosition;startSiblingIndex = transform.GetSiblingIndex();Debug.Log($"开始拖拽时:startSiblingIndex={startSiblingIndex}");// 禁用射线阻挡canvasGroup.blocksRaycasts = false;isDragging = true;}public void OnDrag(PointerEventData eventData){if (!isDragging) return;// 更新位置(跟随鼠标)Vector2 localPoint;RectTransformUtility.ScreenPointToLocalPointInRectangle(parentTransform as RectTransform,eventData.position,canvas.worldCamera,out localPoint);rectTransform.anchoredPosition = new Vector2(startPosition.x, localPoint.y);// 直接更新顺序(不使用占位符)UpdateSortOrder();}public void OnEndDrag(PointerEventData eventData){if (!isDragging) return;isDragging = false;// 恢复射线阻挡canvasGroup.blocksRaycasts = true;//标准化最终位置if(newIndex == 0){rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x, -(top+ height/2));}else{RectTransform preChildTrec = parentTransform.GetChild(newIndex - 1)?.GetComponent<RectTransform>();rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x, preChildTrec.anchoredPosition.y - (spacing+height));}}// 直接更新排序顺序private void UpdateSortOrder(){newIndex = startSiblingIndex;// 计算当前拖拽物体在父物体局部空间中的位置Vector2 currentPos = rectTransform.anchoredPosition;Debug.Log($"自己的y:{currentPos.y}");// 遍历所有兄弟物体,找到合适的位置for (int i = 0; i < parentTransform.childCount; i++){// 跳过自己if (parentTransform.GetChild(i) == transform) continue;RectTransform sibling = parentTransform.GetChild(i)?.GetComponent<RectTransform>();if (sibling != null){Debug.Log($"子物体的名字:{sibling.name},y:{sibling.anchoredPosition.y}");// 简单的基于Y坐标的位置判断if (currentPos.y > sibling.anchoredPosition.y){newIndex = i;// 如果新索引大于当前索引,需要减1,因为自己不在列表中if (newIndex > transform.GetSiblingIndex()){newIndex--;}Debug.Log($"newIndex = {newIndex}");break;}}}// 确保索引在有效范围内newIndex = Mathf.Clamp(newIndex, 0, parentTransform.childCount - 1);// 更新自己的位置if (newIndex != transform.GetSiblingIndex()){transform.SetSiblingIndex(newIndex);}}
}


文章转载自:

http://NczY6Y2Y.tqLhn.cn
http://w0yCBXp4.tqLhn.cn
http://1lCje6L9.tqLhn.cn
http://Fh6LKBMz.tqLhn.cn
http://hBqP9wpz.tqLhn.cn
http://nV3vnZA0.tqLhn.cn
http://GWrROumT.tqLhn.cn
http://UDpEATzH.tqLhn.cn
http://QqsMtTc2.tqLhn.cn
http://6bPHNetw.tqLhn.cn
http://cvF4oEmA.tqLhn.cn
http://lz0h1giv.tqLhn.cn
http://QIEHjUW1.tqLhn.cn
http://V3wE9v45.tqLhn.cn
http://ybJROHxR.tqLhn.cn
http://iaxKS34f.tqLhn.cn
http://QAmrJFAO.tqLhn.cn
http://rSK8qxN5.tqLhn.cn
http://gXVAe4ec.tqLhn.cn
http://NxiFcdpd.tqLhn.cn
http://kvep54lw.tqLhn.cn
http://LF2Z9zXC.tqLhn.cn
http://hhNfZxBS.tqLhn.cn
http://DgU8Blhv.tqLhn.cn
http://SCkLMHNY.tqLhn.cn
http://H1uNobe6.tqLhn.cn
http://tcb37jK5.tqLhn.cn
http://Vv6zDyK1.tqLhn.cn
http://7zfFMNNi.tqLhn.cn
http://NYmEBwT4.tqLhn.cn
http://www.dtcms.com/a/374360.html

相关文章:

  • 最长上升子序列的长度最短连续字段和(动态规划)
  • 2025年最新AI大模型原理和应用面试题
  • Docker 轻量级管理Portainer
  • Aider AI Coding 智能上下文管理深度分析
  • 【Vue3】02-Vue3工程目录分析
  • JavaSE 集合从入门到面试:全面解析与实战指南
  • 《AI大模型应知应会100篇》第70篇:大模型驱动的自动化工具开发(国产化实战版)
  • 电机控制(四)-级联PID控制器与参数整定(MATLABSimulink)
  • mybatis-plus 的更新操作(个人资料更新) —— 前后端传参空值处理
  • 技术方案之数据迁移方案
  • LeetCode热题 15.三数之和(双指针)
  • 我对 OTA 的理解随记,附GD32/STM32例程
  • 快速构建数据集-假数据(生成划分)
  • c++ 杂记
  • Effective Modern C++ 条款26:避免在通用引用上重载
  • Android14 init.rc中on boot阶段操作4
  • PYQT5界面类继承以及软件功能开发小记
  • 【机器学习】吴恩达机器学习笔记
  • UE5 性能优化(1) 模型合并,材质合并
  • Selenium4+Pytest自动化测试框架实战
  • 基于RK3568多网多串(6网+6串+2光)1U/2U机架式服务器在储能与电力的应用
  • 【Python】运动路线记录GPX文件的操作API函数,以及相关GUI界面(支持复制、拼接、数据生成、修改,SRT字幕生成等功能)
  • 西嘎嘎学习 - C++vector容器 - Day 7
  • 第三章:Python基本语法规则详解(二)
  • Next系统总结学习(一)
  • 备考系统分析师-专栏介绍和目录
  • 【rk3229/rk3228a android7.1 LPDDR EMMC EMCP 批量sdk】
  • Kali 自带工具 dirb:Web 路径扫描与 edusrc 挖掘利器
  • 【系统分析师】第2章-基础知识:数学与工程基础(核心总结)
  • 房屋安全鉴定机构评价