gpt4 book ai didi

c# - 防止对象在拖动开始时跳到中心点

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:45 25 4
gpt4 key购买 nike

我有一个用于拖动 GameObjects 的脚本,但目前,每当我开始拖动对象时,它都会跳到指针的中心。我想要实现的是,无论我从哪里开始拖动对象,它都会在该点开始拖动,而不是先跳到对象中心。我需要在 OnDrag 或 OnBeginDrag 方法中修改什么才能实现此目的?

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

public class DragHandling : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler
{
public float partScale;

[HideInInspector] public Transform placeholderParent = null;
[HideInInspector] public Transform parentToReturnTo = null;
[HideInInspector] public GameObject trashCan;
[HideInInspector] public GameObject partsPanel;
[HideInInspector] public GameObject partsWindow;
[HideInInspector] public GameObject buildBoard;

GameObject placeholder = null;
GameObject dragLayer;
Vector3 buildPanelScale;
Vector3 partsPanelScale = new Vector3(1.0f, 1.0f, 1.0f);
Vector3 startPosition;

// PolygonCollider2D collider;

void Start ()
{
dragLayer = GameObject.FindGameObjectWithTag("DragLayer");
buildBoard = GameObject.FindGameObjectWithTag("Board");
partsPanel = GameObject.FindGameObjectWithTag("Parts");
partsWindow = GameObject.FindGameObjectWithTag("PartsWindow");
trashCan = GameObject.FindGameObjectWithTag("Trash");

// collider = transform.GetComponent<PolygonCollider2D>();
}

#region IPointerClickHandler implementation

public void OnPointerClick (PointerEventData eventData)
{
if(transform.parent.gameObject == buildBoard)
transform.SetAsLastSibling();
}

#endregion

#region IBeginDragHandler implementation

public void OnBeginDrag (PointerEventData eventData)
{
// create placeholder gap and hold correct position in layout
placeholder = new GameObject();
placeholder.transform.SetParent(transform.parent);
placeholder.transform.SetSiblingIndex(transform.GetSiblingIndex());
parentToReturnTo = transform.parent; // store original parent location
placeholderParent = parentToReturnTo; // set placeholder gameobject transform
startPosition = transform.position;
GetComponent<CanvasGroup>().blocksRaycasts = false; // turn off image raycasting when dragging image in order to see what's behind the image
}

#endregion

#region IDragHandler implementation

public void OnDrag (PointerEventData eventData)
{
Vector3 mousePosition = new Vector3(eventData.position.x, eventData.position.y, 0);
transform.position = Input.mousePosition; // set object coordinates to mouse coordinates

if(transform.parent.gameObject == partsPanel)
transform.SetParent(dragLayer.transform); // pop object to draglayer to move object out of parts Panel
if(transform.parent.gameObject == buildBoard)
transform.SetParent(dragLayer.transform);
}

#endregion

#region IEndDragHandler implementation

public void OnEndDrag (PointerEventData eventData)
{
transform.SetParent(parentToReturnTo); // Snaps object back to orginal parent if dropped outside of a dropzone
transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex()); // Returns card back to placeholder location
GetComponent<CanvasGroup>().blocksRaycasts = true; // turn Raycast back on
Destroy(placeholder); // kill the placeholder if object hits a drop zone or returns to parts panel

if(transform.parent.gameObject == buildBoard)
{
buildPanelScale = new Vector3(partScale, partScale, partScale);
transform.localScale = buildPanelScale;
transform.SetAsLastSibling(); // always place last piece on top
}
if(transform.parent.gameObject == partsPanel)
transform.localScale = partsPanelScale;
}

#endregion

}

最佳答案

我从来没有使用任何接口(interface)来实现这个,但解决方案应该与使用 Sprite 的 OnMouseDown、OnMouseUp 和 OnMouseDrag 相同。用你当前的实现试试这个

 using UnityEngine;
using System.Collections;

public class Drag : MonoBehaviour {

private Vector3 offset = Vector3.zero;

void OnMouseDown () {
Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
worldPos.z = transform.position.z;
offset = worldPos - transform.position;
}


void OnMouseDrag () {
Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
worldPos.z = transform.position.z;
worldPos = worldPos - offset;
transform.position = worldPos;
}


void OnMouseUp () {
offset = Vector3.zero;
}

}

我在 Sprites 上将其与正交相机一起使用。希望这会有所帮助。

编辑

尝试在您的代码中使用 UI 元素实现接口(interface)。它按预期工作。唯一的事情是将 Canvas 设置为 ScreenSpace-Camera 在各自的接口(interface)中实现代码

关于c# - 防止对象在拖动开始时跳到中心点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31954840/

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