Search code examples
unity-game-enginedrag-and-drop2d

I can't drag text ui in unity


I'm making a 2d unity game for my senior project.I wrote a drag and drop script, it's working perfectly with sprites but when I tried to use to drag and drop text or any other ui, it failed and it's not even dragging!! I appreciate your help in advance..

enter image description here

the code:

    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;
        }
        
    }
}

Solution

  • If you need basic drag and drop of UI object's here is the working script, I added some comments for better understanding.

    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;
        }
    }
    

    If I misunderstood your problem or this script is not enough to fully solve your problem, let me know in comment and we can extend it by your needs