Почему мой сенсорный код ввода не работает в AR Foundation? - PullRequest
2 голосов
/ 18 октября 2019

Попытка создать приложение AR, в котором у меня может быть несколько касаний ввода, таких как перетаскивание, поворот, масштабирование, двойное нажатие на событие, удержание объекта на событие и т. Д. Все отлично работает в тестовой сцене, которую я построил [не AR]. После того, как я включил код в свой префаб AR placeOnPlane [шаблон сцены - место на плоскости], когда я касаюсь объекта, он исчезает, и я не могу понять, что я делаю неправильно!

Наконец, я воспользовался преимуществом LeanTouch, и все работает отлично (почему? Потому что это плохой актив), но я обычно ненавижу использовать активы, когда мой код работает одинаково хорошо, и я потратил на это дни! Некоторая помощь, пожалуйста.

Я попытался закомментировать встроенную функцию перетаскивания в коде PlacedOnPlane, который поставляется вместе со сценой ARfoundation, но он не работал.

using UnityEngine;
using System.Collections;
using UnityEngine.iOS;

public class InputTouchUnity : MonoBehaviour
{
    private Vector3 position;
    private float width;
    private float height;
    public float speedDrag= 0.1f;
    float initialFingersDistance;
    float speedTwist = -4000f;
    private float baseAngle = 0.0f;
    Vector3 initialScale;

 // scale clamp

     //public float scalingSpeed = 0.03f;
     public Vector3 min = new Vector3(292f, 292f, 292f);
     public Vector3 max = new Vector3(800f, 800f, 800f);

    // int tapCount;
    // float doubleTapTimer;

    void Awake()
    {
        width = (float)Screen.width / 2.0f;
        height = (float)Screen.height / 2.0f;

        //Position used for the cube.
        position = this.transform.position;
    }

    void OnGUI() // TO OBSERVE MOTION
    {
        // Compute a fontSize based on the size of the screen width.
        GUI.skin.label.fontSize = (int)(Screen.width / 25.0f);

        GUI.Label(new Rect(20, 20, width, height * 0.25f),
            "x = " + position.x.ToString("f2") +
            ", y = " + position.z.ToString("f2"));
    }
    void Update()
    {



        // Handle screen touches.
        if (Input.touchCount > 0)
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))

            initialScale = transform.localScale;

            {

                {

                    {
                         //DRAG - got rid of it because conflicting the AR drag
                            Touch touch = Input.GetTouch(0);
                          Move the cube if the screen has the finger moving.
                          if (Input.touchCount == 2)
                         {

                           if (touch.phase == TouchPhase.Moved)
                          {
                              Vector2 pos = touch.position;
                              pos.x = (pos.x - width) / width;
                              pos.y = (pos.y - height) / height;
                              position = new Vector3(transform.position.x + pos.x * speedDrag, 0, transform.position.y + pos.y * speedDrag);
                              // Position the cube.
                             transform.position = position;
                          }
                        }

                        //SCALE
                         if (Input.touchCount == 2)
                         {
                             Touch touch1 = Input.GetTouch(0);

                             if (touch1.phase == TouchPhase.Began)
                             {
                                 initialFingersDistance = Vector2.Distance(Input.touches[0].position , Input.touches[1].position);
                                 initialScale = transform.localScale;
                             }
                             else
                             {
                                 var currentFingersDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
                                var scaleFactor = (currentFingersDistance  / initialFingersDistance );
                                 transform.localScale = initialScale * scaleFactor;
                                 Debug.Log(transform.localScale);

                                 GameObject[] models = GameObject.FindGameObjectsWithTag ("ARobject");
                                   newScale.x = Mathf.Clamp(model.localScale.x - scaleFactor, min.x, max.x);
                                newScale.y = Mathf.Clamp(model.localScale.y - scaleFactor, min.y, max.y);
                                  newScale.z = Mathf.Clamp(model.localScale.z - scaleFactor, min.z, max.z);
                                   model.localScale = newScale;

                           }
                         }
                        //TWIST

                        if (Input.touchCount == 2)
                        {
                            Touch touch2 = Input.GetTouch(0);

                            if (touch2.phase == TouchPhase.Began)
                            {
                                Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
                                pos = Input.mousePosition - pos;
                                baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Deg2Rad;
                                baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
                            }
                            if (touch2.phase == TouchPhase.Moved)
                            {
                                Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
                                pos = Input.mousePosition - pos;
                                float ang = Mathf.Atan2(pos.y, pos.x) * Mathf.Deg2Rad - baseAngle;
                                transform.rotation = Quaternion.AngleAxis(ang * speedTwist, Vector3.up);
                            }
                        }

                    }
                }
            }

        }
    }
}
//}
...