Unity 多点触控无法达到其方法

Unity multitouch cant reach its methods

我遵循了 this 教程,基本上我所做的一切都是一样的。但是我无法访问 testTouch class 中的方法。这是 TouchInput class:

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

public class touchInput : MonoBehaviour
{
    public LayerMask touchInputMask;
    private List<GameObject> touchList = new List<GameObject>();
    private GameObject[] touchesOld;
    private RaycastHit hit;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

#if UNITY_EDITOR

//与教程中所示的统一编辑器相同 #endif

        if (Input.touchCount > 0)
        {
            touchesOld = new GameObject[touchList.Count];
            touchList.CopyTo(touchesOld);
            touchList.Clear();
            foreach (Touch touch in Input.touches)
            {
                Ray ray = GetComponent<Camera>().ScreenPointToRay(touch.position);


                if (Physics.Raycast(ray, out hit, touchInputMask))
                {
                    GameObject recepient = hit.transform.gameObject;
                    touchList.Add(recepient);

                    if (touch.phase == TouchPhase.Began)
                    {
                        recepient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Ended)
                    {
                        recepient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Stationary)
                    {
                        recepient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Canceled)
                    {
                        recepient.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                    }
                }
            }
            foreach (GameObject g in touchesOld)
            {
                if (!touchList.Contains(g))
                    g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
            }
        }
    }
}

这里是 testTouch class:

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

public class testTouch : MonoBehaviour
{

    public Color defaultColor;  //set manually in the editor
    public Color selectedColor;  //set manually in the editor
    public Material mat;  

    void Start()
    {
        mat = this.GetComponent<SpriteRenderer>().material;
     // mat.color = selectedColor;
    }

    void OnTouchDown()
    {
        mat.color = selectedColor;
        Debug.Log("hello");
    }

    void OnTouchUp()
    {
        mat.color = defaultColor;
        Debug.Log("hello");
    }

    void OnTouchStay()
    {
        mat.color = selectedColor;
        Debug.Log("hello");
    }

    void OnTouchExit()
    {
        mat.color = defaultColor;
        Debug.Log("hello");
    }

我没有收到任何错误,我也无法访问那些 Debug.Logs。当我点击 2Dcircle 时没有任何反应。

我在编辑器和 android phone 上试过。他们两个都不工作。

3D 光线投射 (Physics.Raycast()) 在 2D 碰撞器上不起作用。

用 3D 圆形碰撞器替换 2D 圆形碰撞器。

没有将 layerMask 作为第三个参数的 Raycast 方法重载。

https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

您错误地调用了 Raycast 方法的另一个重载。

public static bool Raycast(Ray ray, out RaycastHit hitInfo, 
float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, 
QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);