简体   繁体   English

触摸拖动 android UI

[英]Drag by touch android UI

I have problem with UI image on canvas for android in unity, the object does not follow the exact touch coordinates it moves slightly, but not enough to actually be functional我在 canvas 上的 UI 图像有问题,android 统一,object 不遵循精确的触摸坐标,它略微移动,但不足以实际发挥作用

I tried everything, including creating a new project that apparently works as intended but without image from UI only by move sprite to the scene我尝试了一切,包括创建一个显然按预期工作但没有来自 UI 的图像的新项目,仅通过将精灵移动到场景

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragScript : MonoBehaviour{

//offset dotkniecia objektu
float deltaX, deltaY;

//dostep do komponentu rigibody
Rigidbody2D rb;

//bool do dostepu poruszania pilki
bool moveAllowed = false;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
}

void Update(){

    /// <summary>
    /// inicjowanie zdarzenia dotykowego
    /// jezeli ma miejsce zdarzenie dotykowe czyli jest > 0
    /// </summary>
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);

        // uzyskuje pozycje dotykowe
        Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);

        //przetwarzanie faz dotykowych
        switch (touch.phase)
        {
            //jezeli dotykasz ekranu
            case TouchPhase.Began:
                Debug.Log("DOTKNALES PALCEM WTFF");
                // jezeli dotkniesz elementu
                if (GetComponent<BoxCollider2D>() == Physics2D.OverlapPoint(touchPos)){

                    Debug.Log("DZIALA IF");
                    /// <summary>
                    /// pobierz offset pomiedzy pozycjami ktore dotkniesz
                    /// i srodek objektu gry
                    /// </summary>
                    deltaX = touchPos.x - transform.position.x;
                    deltaY = touchPos.y - transform.position.y;

                    /// <summary>
                    /// jezeli dotyk zaczyna sie w ciagu colidera obiektu
                    /// wtedy może się poruszać
                    /// </summary>
                    moveAllowed = true;

                    /// <summary>
                    /// ogranicza troche rigibody aby sie poruszał
                    /// bardziej płynnie i poprawnie
                    /// </summary>
                    //rb.mass = 1;
                    rb.freezeRotation = true;
                    //rb.velocity = new Vector2(0, 0);
                    rb.gravityScale = 0;
                    GetComponent<PolygonCollider2D>().sharedMaterial = null;
                }
                break;
            // Gdy przesuwasz palcem po ekranie
            case TouchPhase.Moved:
                // jezeli przesuwam obiekt i moveallowed = true
                if (GetComponent<BoxCollider2D>() == Physics2D.OverlapPoint(touchPos) && moveAllowed)
                {   
                    rb.MovePosition(new Vector2(touchPos.x - deltaX, touchPos.y - deltaY));
                }
                break;

            // puszczasz palce
            case TouchPhase.Ended:
                Debug.Log("KONIEC");
                moveAllowed = false;
                //rb.mass = 17;
                rb.freezeRotation = false;
                rb.gravityScale = 45;
                break;
        }
    }
}

} }

it must move an UI image with fingers no error messages it just doesn't move correctly它必须用手指移动 UI 图像 没有错误消息 它只是不能正确移动

The script is attached to the wrong object in the Hierarchy.该脚本附加到层次结构中的错误 object。 You need to attach it to the Canvas and not the image.您需要将其附加到 Canvas 而不是图像。 The script uses the transform of the object it is placed on.该脚本使用它所在的 object 的变换。 Thus, if you place it on a child, the parent object will not follow.因此,如果您将其放在孩子身上,则父 object 将不会跟随。

To resolve this, try adding the script to the canvas.要解决此问题,请尝试将脚本添加到 canvas。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM