gpt4 book ai didi

unity3d - 在Unity3D中拖拽ScrollRect(ScrollView)

转载 作者:行者123 更新时间:2023-12-05 08:17:23 29 4
gpt4 key购买 nike

我想在 ScrollView 的内容上实现拖放。

问题是当您尝试在 ScrollView 中拖动项目时,您无法 ScrollView 。

首先,我尝试通过 IDragHandler、IBeginDragHandler、IEndDragHandle 和 IDropHandler 接口(interface)实现拖放。乍一看,它工作得很好,但问题是您无法滚动 ScrollRect。

我认为问题是因为重写,当我使用与滚动矩形相同的事件触发器(如拖动)时,父触发器无法正常工作。

所以后来,我自己想了想,通过IPointerDown、IPointerUp接口(interface)和在ScrollRect中拖拽UI的特定时间实现,如果你在特定时间不持有,滚动效果很好。

但问题是启用我在 OnDrag、OnBeginDrag 和 OnEndDrag 函数之前编写的 DragHandler 脚本在保持时间结束时不起作用。

首先我想知道有什么方法可以调用这些函数?

其次有没有什么方法可以在不使用拖动界面的情况下实现拖放 UI?

拖动处理程序:

using System;
using UnityEngine;
using UnityEngine.EventSystems;

public class DragHandler : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
public static GameObject itemBeingDragged;

private Vector3 startPos;

private Transform startParent;

DragHandler dragHandler;

public void Awake()
{
dragHandler = GetComponent<DragHandler>();
}

public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("Begin");
itemBeingDragged = gameObject;
startPos = transform.position;
startParent = transform.parent;
}

public void OnDrag(PointerEventData eventData)
{
Debug.Log("Drag");
transform.position = Input.mousePosition;
}

public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("End");
itemBeingDragged = null;
if (transform.parent == startParent)
{
dragHandler.enabled = false;
transform.SetParent(startParent);
transform.position = startPos;
}
}
}

滚动矩形 Controller :

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ScrollRectController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public float holdTime;
public float maxVelocity;

private Transform scrollRectParent;

private DragHandler dragHandler;

private ScrollRect scrollRect;

private float timer;

private bool isHolding;

void Awake()
{
scrollRectParent = GameObject.FindGameObjectWithTag("rec_dlg").transform;
dragHandler = GetComponent<DragHandler>();
dragHandler.enabled = false;
}

// Use this for initialization
void Start()
{
timer = holdTime;
}

// Update is called once per frame
void Update()
{

}

public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Down");
scrollRect = scrollRectParent.GetComponent<ScrollRect>();
isHolding = true;
StartCoroutine(Holding());
}

public void OnPointerUp(PointerEventData eventData)
{
Debug.Log("Up");
isHolding = false;
}

IEnumerator Holding()
{
while (timer > 0)
{
//if (scrollRect.velocity.x >= maxVelocity)
//{
// isHolding = false;
//}

if (!isHolding)
{
timer = holdTime;
yield break;
}

timer -= Time.deltaTime;
Debug.Log(timer);
yield return null;
}

dragHandler.enabled = true;
//dragHandler.OnBeginDrag();
}
}

插槽:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Slot : MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
DragHandler.itemBeingDragged.transform.SetParent(transform);
}
}

最佳答案

答案:

我在不使用 DragHandler 接口(interface)的情况下编写了一些代码来处理 scrollRect(scrollView) 中的拖放。

拖动处理程序:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DragHandler : MonoBehaviour, IPointerExitHandler
{
public static GameObject itemBeingDragged;

public static bool isCustomerDragged;

public Transform customerScrollRect;
public Transform dragParent;

public float holdTime;
public float maxScrollVelocityInDrag;

private Transform startParent;

private ScrollRect scrollRect;

private float timer;

private bool isHolding;
private bool canDrag;
private bool isPointerOverGameObject;

private CanvasGroup canvasGroup;

private Vector3 startPos;

public Transform StartParent
{
get { return startParent; }
}

public Vector3 StartPos
{
get { return startPos; }
}

void Awake()
{
canvasGroup = GetComponent<CanvasGroup>();
}

// Use this for initialization
void Start()
{
timer = holdTime;
}

// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (EventSystem.current.currentSelectedGameObject == gameObject)
{
//Debug.Log("Mouse Button Down");
scrollRect = customerScrollRect.GetComponent<ScrollRect>();
isPointerOverGameObject = true;
isHolding = true;
StartCoroutine(Holding());
}
}

if (Input.GetMouseButtonUp(0))
{
if (EventSystem.current.currentSelectedGameObject == gameObject)
{
//Debug.Log("Mouse Button Up");
isHolding = false;

if (canDrag)
{
itemBeingDragged = null;
isCustomerDragged = false;
if (transform.parent == dragParent)
{
canvasGroup.blocksRaycasts = true;
transform.SetParent(startParent);
transform.localPosition = startPos;
}
canDrag = false;
timer = holdTime;
}
}
}

if (Input.GetMouseButton(0))
{
if (EventSystem.current.currentSelectedGameObject == gameObject)
{
if (canDrag)
{
//Debug.Log("Mouse Button");
transform.position = Input.mousePosition;
}
else
{
if (!isPointerOverGameObject)
{
isHolding = false;
}
}
}
}
}

public void OnPointerExit(PointerEventData eventData)
{
isPointerOverGameObject = false;
}

IEnumerator Holding()
{
while (timer > 0)
{
if (scrollRect.velocity.x >= maxScrollVelocityInDrag)
{
isHolding = false;
}

if (!isHolding)
{
timer = holdTime;
yield break;
}

timer -= Time.deltaTime;
//Debug.Log("Time : " + timer);
yield return null;
}

isCustomerDragged = true;
itemBeingDragged = gameObject;
startPos = transform.localPosition;
startParent = transform.parent;
canDrag = true;
canvasGroup.blocksRaycasts = false;
transform.SetParent(dragParent);
}

public void Reset()
{
isHolding = false;
canDrag = false;
isPointerOverGameObject = false;
}
}

对这段代码的一些解释:

  1. 您的可拖动 UI 元素需要棘手的选项,对我来说,我使用了按钮。
  2. 您需要将此脚本附加到您的可拖动项。
  3. 您还需要添加 Canvas Group 组件。
  4. customerScrollRect 是您项目的 ScrollRect 父级。
  5. dragParent 可以是一个空的游戏对象,因为视口(viewport)的掩码而被使用。

插槽:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Slot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
bool isEntered;

public void OnPointerEnter(PointerEventData eventData)
{
isEntered = true;
}

public void OnPointerExit(PointerEventData eventData)
{
isEntered = false;
}

void Update()
{
if (Input.GetMouseButtonUp(0))
{
if (isEntered)
{
if (DragHandler.itemBeingDragged)
{
GameObject draggedItem = DragHandler.itemBeingDragged;
DragHandler dragHandler = draggedItem.GetComponent<DragHandler>();
Vector3 childPos = draggedItem.transform.position;
//Debug.Log("On Pointer Enter");
draggedItem.transform.SetParent(dragHandler.StartParent);
draggedItem.transform.localPosition = dragHandler.StartPos;
draggedItem.transform.parent.SetParent(transform);
draggedItem.transform.parent.position = childPos;
isEntered = false;
}
}
}
}
}

对这个脚本的一些解释:

1.将脚本附加到掉落的元素上。

关于unity3d - 在Unity3D中拖拽ScrollRect(ScrollView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44515498/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com