我需要有关如何修复拖动对象的帮助。当我向右移动相机(也带有图像/精灵对象)并拖动对象时,它会出现在先前的位置。如果我将“渲染模式”更改为“屏幕空间-叠加”,则它只能在图像上显示,而不能用于精灵。当我移动相机图像时,对象会随相机(我想要的)一起移动,但是精灵并不代表我将渲染模式更改为屏幕空间的原因-相机。我需要移动带有精灵对象的相机,并能够拖动该对象。
可拖动对象具有以下代码:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Dragable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
public Transform parentToReturnTo = null;
public enum Slot { TYPE1, TYPE2 };
public Slot typeOfItem = Slot.INVENTORY;
GameObject placeholder = null;
Animator animDrop;
void Start()
{
animDrop = GetComponentInChildren<Animator>();
}
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("OnBeginDrag");
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;
this.transform.SetParent(this.transform.parent.parent);
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
this.transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("OnEndDrag");
this.transform.SetParent(parentToReturnTo);
this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());
GetComponent<CanvasGroup>().blocksRaycasts = true;
Destroy(placeholder);
animDrop.SetBool("OnDropAnimation", true);
}
}
要移动相机,我需要执行以下脚本
using UnityEngine;
using UnityEngine.EventSystems;
public class TouchController : MonoBehaviour
{
float touchStart = 0f;
Vector3 cameraDestination;
public float cameraSpeed = 0.1f;
public float maxPosX = 2900f;
public float minPosX = -2900f;
public bool DragUI = false;
public static bool IsPointerOverGameObject()
{
if (EventSystem.current.IsPointerOverGameObject())
return true;
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
return true;
}
return false;
}
void Start()
{
cameraDestination = Camera.main.transform.position;
}
void Update()
{
if (IsPointerOverGameObject() && Input.GetMouseButtonDown(0))
{
DragUI = true;
}
if (Input.GetMouseButtonDown(0))
{
touchStart = Input.mousePosition.x;
}
if (Input.GetMouseButtonUp(0))
{
float delta = Input.mousePosition.x - touchStart;
if (delta < -50f && !DragUI)
{
if (cameraDestination.x < maxPosX)
{
cameraDestination = new Vector3(Camera.main.transform.position.x + 1600,
Camera.main.transform.position.y, Camera.main.transform.position.z);
}
}
else if (delta > 50f && !DragUI)
{
if (cameraDestination.x > minPosX)
{
cameraDestination = new Vector3(Camera.main.transform.position.x - 1600,
Camera.main.transform.position.y, Camera.main.transform.position.z);
Debug.Log(cameraDestination.x + " cam dest x left");
}
}
else { DragUI = false;}
}
if (Vector3.Distance(Camera.main.transform.position, cameraDestination) > 0.1f && !DragUI)
{
if (Camera.main.transform.position.x > cameraDestination.x)
{
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x - cameraSpeed,
Camera.main.transform.position.y, Camera.main.transform.position.z);
DragUI = false;
}
else
{
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + cameraSpeed,
Camera.main.transform.position.y, Camera.main.transform.position.z);
DragUI = false;
}
}
}
}
答案 0 :(得分:0)
精灵是在世界空间中“存在”的对象,因此您需要更改画布以在世界空间中操作以混合图像和精灵。但是看来您的方法并不是解决问题的最佳方法,因为从您的描述中看起来有点奇怪。