2019-05-20

it2025-09-30  6

简介

UGUI

图片拖拽排序

demo.gif

DropZone.cs

拖拽Continer public class DropZone : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler { public void OnPointerEnter(PointerEventData eventData) { if (eventData.pointerDrag == null) return; Draggable d = eventData.pointerDrag.GetComponent<Draggable>(); if (d != null) { d.placeHolderParent = this.transform; } } public void OnPointerExit(PointerEventData eventData) { if (eventData.pointerDrag == null) return; Draggable d = eventData.pointerDrag.GetComponent<Draggable>(); if (d != null && d.placeHolderParent == this.transform) { d.placeHolderParent = d.parentToReturnTo; } } public void OnDrop(PointerEventData eventData) { Debug.Log(eventData.pointerDrag.name + " dropped on " + gameObject.name); Draggable d = eventData.pointerDrag.GetComponent<Draggable>(); if (d != null) { d.parentToReturnTo = this.transform; } } } PointerEventData.pointerDrag:触发OnPointerEnter的物体OnPointerEnter:Called when a pointer enters the objectOnPointerExit:Called when a pointer exits the objectOnDrop:Called on the object where a drag finishes(有拖拽结束时调用)

Draggable.cs

拖拽Item拖拽出去 原位置创建空物体占位置排序 public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { [HideInInspector] public Transform parentToReturnTo = null; [HideInInspector] public Transform placeHolderParent = null; private GameObject placeHolder = null; public void OnBeginDrag(PointerEventData eventData) { placeHolder = new GameObject(); placeHolder.transform.SetParent( this.transform.parent ); LayoutElement le = placeHolder.AddComponent<LayoutElement>(); le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth; le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight; le.flexibleWidth = 0; le.flexibleHeight = 0; placeHolder.transform.SetSiblingIndex(this.transform.GetSiblingIndex() ); parentToReturnTo = this.transform.parent; placeHolderParent = parentToReturnTo; this.transform.SetParent(this.transform.parent.parent); this.GetComponent<CanvasGroup>().blocksRaycasts = false; } public void OnDrag(PointerEventData eventData) { this.transform.position = eventData.position; if (placeHolder.transform.parent != placeHolderParent) { placeHolder.transform.SetParent(placeHolderParent); } int newSiblingIndex = placeHolderParent.childCount; for (int i = 0; i < placeHolderParent.childCount; i++) { if (this.transform.position.x < placeHolderParent.GetChild(i).position.x) { newSiblingIndex = i; if (placeHolder.transform.GetSiblingIndex() < newSiblingIndex) newSiblingIndex--; break; } } placeHolder.transform.SetSiblingIndex(newSiblingIndex); } public void OnEndDrag(PointerEventData eventData) { this.transform.SetParent(parentToReturnTo); this.transform.SetSiblingIndex(placeHolder.transform.GetSiblingIndex()); this.GetComponent<CanvasGroup>().blocksRaycasts = true; Destroy(placeHolder); } } OnEndDrag会在OnDrop之后执行,保证松开时父物体改变

改善:

目前上下两横排,增加横竖排都可以穿插排序目前两排,支持三排多排排序 public void OnDrag(PointerEventData eventData) { this.transform.position = eventData.position; if (placeHolder.transform.parent != placeHolderParent) { placeHolder.transform.SetParent(placeHolderParent); isVerticalLayout = (!placeHolder.transform.parent.GetComponent<HorizontalLayoutGroup>()); } int newSiblingIndex = placeHolderParent.childCount; if (isVerticalLayout) { for (int i = 0; i < placeHolderParent.childCount; i++) { if (this.transform.position.y > placeHolderParent.GetChild(i).position.y) { newSiblingIndex = i; if (placeHolder.transform.GetSiblingIndex() < newSiblingIndex) newSiblingIndex--; break; } } } else { for (int i = 0; i < placeHolderParent.childCount; i++) { if (this.transform.position.x < placeHolderParent.GetChild(i).position.x) { newSiblingIndex = i; if (placeHolder.transform.GetSiblingIndex() < newSiblingIndex) newSiblingIndex--; break; } } } placeHolder.transform.SetSiblingIndex(newSiblingIndex); }

参考

https://lab.uwa4d.com/lab/5b6630ebd7f10a201ffa5aef

最新回复(0)