C# 为什么我的输入触摸代码不';在AR基础上工作?

C# 为什么我的输入触摸代码不';在AR基础上工作?,c#,unity3d,input,touch,augmented-reality,C#,Unity3d,Input,Touch,Augmented Reality,试图建立一个AR应用程序,我可以有一些输入触摸,如拖动,旋转,缩放,双击到事件,按住对象到事件等。 在我构建的测试场景中,一切正常[不是AR]。 一旦将代码包含在我的AR placedOnPlane预置[模板场景-在平面上放置],一旦我触摸对象,它就会消失,我无法找出我做错了什么 最后,我利用了LeanTouch,一切都很好(为什么?因为它是一个糟糕的资产),但我通常不喜欢使用资产,因为我的代码工作得同样好,我花了几天的时间在上面!请帮忙 我试着在ARfoundation场景附带的PlacedO

试图建立一个AR应用程序,我可以有一些输入触摸,如拖动,旋转,缩放,双击到事件,按住对象到事件等。 在我构建的测试场景中,一切正常[不是AR]。 一旦将代码包含在我的AR placedOnPlane预置[模板场景-在平面上放置],一旦我触摸对象,它就会消失,我无法找出我做错了什么

最后,我利用了LeanTouch,一切都很好(为什么?因为它是一个糟糕的资产),但我通常不喜欢使用资产,因为我的代码工作得同样好,我花了几天的时间在上面!请帮忙

我试着在ARfoundation场景附带的PlacedOnPlane代码中注释内置的拖动功能,但没有成功

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

                    }
                }
            }

        }
    }
}
//}

这是因为您使用的是Physics.Raycast,它不适用于AR可跟踪对象(平面),因为它们没有任何与之关联的特定几何体。 因此,为了与可跟踪数据交互,unity提供了单独的光线投射方法,该方法在ARFoundation的ARRaycastManager中可用。 在ARFoundation的早期版本中,它在ARSessionOrigin中可用。所以检查你使用的是哪一个版本的AR基础。 你可以这样使用它

在这里输入代码

`

`

你也可以参考这里提供的AR基础样本场景的Simulink场景:

[SerializeField] ARRaycast​Manager raycastManager;
    void Update()
        {
             if (Input.touchCount == 0)
                return;
           Touch touch = Input.GetTouch(0);

           if (raycastManager.Raycast(touch.position,s_Hits,TrackableType.PlaneWithinPolygon))
            {
                // Raycast hits are sorted by distance, so the first one
                // will be the closest hit.
                var hitPose = s_Hits[0].pose;
                if (spawnedObject == null)
                {
                    spawnedObject = Instantiate(cube, hitPose.position, hitPose.rotation);
                }
                else
                {
                    spawnedObject.transform.position = hitPose.position;
                }
            }
        }