首页 > 解决方案 > 我无法统一拖动文本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;
        }
        
    }
}

标签: unity3ddrag-and-drop2d

解决方案


如果您需要 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;
    }
}

如果我误解了您的问题或此脚本不足以完全解决您的问题,请在评论中告诉我,我们可以根据您的需要扩展它


推荐阅读