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

OnEndDrag only runs every other time


I have imported ienddraghandler, and my other drag methods work just fine. However, my onEndDrag is activated only every other (end of drag). Example: Grab Object, drag object from point a to point b, let go, and onEndDrag is not called. Go through same exact process again and it is called.

It doesn't just happen the first time the app is booted up, it will continue to happen exactly in that order.

My guess is that it has something to do with a bool which would explain the "every other time" (possibly the dragging bool associated with drag handlers?)

Couldn't find others with the same problem. Thanks and have a great day.

public class VideoMovement : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        public VideoModule videoModule;
        Camera mainCamera;
        private bool dragging1 = false;
        
        

        protected void OnEnable()
        {
            mainCamera = Camera.main;
            m_MapMovementListener = GameObject.Find("EditorManagers").GetComponent<MapMovementListener>();
            reSizeArrow = videoModule.transform.GetChild(1).gameObject;
        }

        protected void Update()
        {
            Debug.Log(dragging);
            if (SiteCameraEditor.instance.shouldShowUI)
            {
                SizeManager();
            }
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (SiteCameraEditor.instance.shouldShowUI)
            {
                VisualRepresentationManager(false);
                m_Video_Transform = transform.parent;
                m_VideoInitialPosition_Vector3 = m_Video_Transform.position;
                m_Initial_Position = eventData.pointerCurrentRaycast.worldPosition;
                videoModule.ElementData.Imager.homePresetPoint = m_Video_Transform.position;
                m_Imager = FindImagerWithID(m_Video_Transform.GetComponent<VideoModule>().ElementData.Imager.Id);
                SiteCameraEditor.instance.DrawLine(m_Imager.transform.position, m_Video_Transform.position, Color.red);
                SiteCameraEditor.instance.SelectCamera(m_Video_Transform.GetComponent<VideoModule>().ElementData.Imager.gameObject);
                //SiteCameraEditor.instance.UpdateUI();
                m_MapMovementListener.SetObjectToMove(this);
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if(SiteCameraEditor.instance.shouldShowUI)
            {
                VisualRepresentationManager(true);
                Debug.Log("OnEndDrag worked");
                SiteCameraEditor.instance.UpdateUI();
            }
        }

SiteCameraEditor.instance.shouldShowUI is always true deleted most of the code, this is what should be relevant

However there is an update loop that contains

protected void Update()
        {
            //Debug.Log(dragging);
            if (SiteCameraEditor.instance.shouldShowUI)
            {
                SizeManager();
            }
            if(SiteCameraEditor.instance.shouldShowUI && reSizeArrow.activeInHierarchy)
            {
                reSizeArrow.SetActive(false);
            }
            
            
            try
            {
                if (!dragging1)
                {
                    return;
                }

                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                Vector3 wolrdSpaceNormal = transform.TransformDirection(Vector3.back);
                Plane moveAxis = new(wolrdSpaceNormal, transform.position);

                if (moveAxis.Raycast(ray, out float intersect))
                {
                    Vector3 movePosition = ray.GetPoint(intersect);

                    Vector3 localClickPosition = transform.parent.transform.InverseTransformPoint(clickPosition);
                    Vector3 localMovePosition = transform.parent.transform.InverseTransformPoint(movePosition);

                    float offsetX = offsetRatioX * (localMovePosition.x - localClickPosition.x);
                    float offsetY = offsetRatioY * (localMovePosition.y - localClickPosition.y);

                    Mesh mesh = GetComponentInChildren<MeshFilter>().mesh;
                    Vector2[] uvs = mesh.uv;

                    float uvsSizeX = originalUVs[1].x - originalUVs[0].x;
                    float uvsSizeY = originalUVs[1].y - originalUVs[0].y;

                    float startX = originalUVs[0].x - offsetX;
                    float endX = originalUVs[1].x - offsetX;
                    float startY = originalUVs[0].y - offsetY;
                    float endY = originalUVs[1].y - offsetY;

                    // X TOO LEFT
                    if (startX <= 0)
                    {
                        startX = 0;
                        endX = startX + uvsSizeX;
                    }
                    // X TOO RIGHT
                    else if (endX >= 1)
                    {
                        endX = 1;
                        startX = endX - uvsSizeX;
                    }
                    // Y TOO DOWN
                    if (startY <= 0)
                    {
                        startY = 0;
                        endY = startY + uvsSizeY;
                    }
                    // X TOO UP
                    else if (endY >= 1)
                    {
                        endY = 1;
                        startY = endY - uvsSizeY;
                    }

                    uvs[0] = new Vector2(startX, startY);
                    uvs[1] = new Vector2(endX, endY);
                    uvs[2] = new Vector2(endX, startY);
                    uvs[3] = new Vector2(startX, endY);
                    mesh.uv = uvs;
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }
            
            
        }

however even when commented out, the onEndDrag isn't fixed


Solution

  • Can be easily fixed by including IDragHandler in the same class. IDragHandler is necessary for IEndDragHandler to function properly.