unity3d - 我无法统一拖动文本ui
问题描述
我正在为我的高级项目制作一个 2d 统一游戏。我编写了一个拖放脚本,它与精灵完美配合,但是当我尝试使用拖放文本或任何其他 ui 时,它失败了,甚至没有拖动! !我提前感谢您的帮助..
编码:
void Update()
{
if (!finish) {
curr = new Vector3(this.gameObject.transform.localPosition.x, this.gameObject.transform.localPosition.y, this.gameObject.transform.localPosition.z);
if (moving)
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
this.gameObject.transform.localPosition = new Vector3(mousePos.x - StartPosX, mousePos.y - StartPosY, this.gameObject.transform.localPosition.z);
}
}
}
public void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
StartPosX = mousePos.x - this.transform.localPosition.x;
StartPosY = mousePos.y - this.transform.localPosition.y;
this.transform.localScale = new Vector3(7f, 7f, 0f);
textbox.SetActive(false);
moving = true;
}
}
public void OnMouseUp()
{
moving = false;
if ((Mathf.Abs(this.gameObject.transform.position.x - correctform.gameObject.transform.position.x) <= 10.5f) && (Mathf.Abs(this.gameObject.transform.localPosition.y - correctform.gameObject.transform.localPosition.y) <= 10.5f))
{
//this.transform.position = new Vector3(correctform.gameObject.transform.position.x, correctform.gameObject.transform.position.y, correctform.gameObject.transform.position.z);
correctform.SetActive(true);
finish = true;
this.transform.position = new Vector3(correctform.gameObject.transform.position.x, correctform.gameObject.transform.position.y, correctform.gameObject.transform.position.z-1f);
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
spriteRenderer.sprite = newSprite;
GameObject.Find("finishmanage").GetComponent<mercury>().setDone();
}
else
{
textbox.SetActive(true);
this.transform.position = new Vector3(resetPos.x, resetPos.y, resetPos.z);
this.transform.localScale = initialScale;
}
}
}
解决方案
如果您需要 UI 对象的基本拖放,这里是工作脚本,我添加了一些注释以便更好地理解。
using UnityEngine;
using UnityEngine.EventSystems;
[RequireComponent(typeof(RectTransform))]
public class UIObjectDragger : MonoBehaviour, IDragHandler, IDropHandler
{
private Vector3 _startPos;
void Awake()
{
_startPos = transform.position;
}
//this works like other unity message methods like update, start or any other
public void OnDrag(PointerEventData eventData)
{
//since Input.mousePosition return cursor position on screen in pixels
//assigning it directly to UI objects transform.position, works perfectly
transform.position = Input.mousePosition;
}
public void OnDrop(PointerEventData eventData)
{
//this is just my custom sample logic after we release dragging object
//you can write your logic here and also you can delete _startPos variable
transform.position = _startPos;
}
}
如果我误解了您的问题或此脚本不足以完全解决您的问题,请在评论中告诉我,我们可以根据您的需要扩展它
推荐阅读
- apache-kafka - 支持集群的 Apache Camel 幂等存储库
- python - 同时运行多个函数(线程)
- flutter - 如何在 Flutter 的 Futures 中使用 Futures?
- r - 如何使用 r 中的 wordcloud2 包将 wordcloud 保存为 .png?
- visual-studio - 我可以从现有 C 代码创建 Visual Studio 2019 项目吗?
- excel - 如何以 12:07 而不是 12:7 格式显示时间?
- python - Txt 到 csv 格式的行和列 [python]
- git - 为什么 git repack 在“filter-repo”中失败?
- android - 片段变化后失去焦点(ANDROID TV)
- php - 如何在准备好的语句中执行多个查询?