touchScript的按下:

void Start()
    {

        TouchManager.Instance.PointersPressed += Instance_PointersPressed;//按下
    }
/// <summary>
    /// TUIO触碰点击后触发(可以获取到坐标的点击)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    private void Instance_PointersPressed(object sender, PointerEventArgs e)
    {
        var count = e.Pointers.Count;
        //Debug.Log(count);
        for (int i = 0; i < count; i++)
        {
            Pointer pointer = e.Pointers[i];
            Debug.Log(pointer.Position+" - "+count);

            if (count >0)
            {
                TouchFunc(pointer.Position);//EventSystem
                CreateRay(pointer.Position);//射线
            }

        }
    }

 

touchScript点击后,使用eventSystem达到点击对象的功能:

void TouchFunc(Vector3 _vec)
    {
        gameManage.ClearAllSpawnGridLayout();
        
        PointerEventData eventData = new PointerEventData(EventSystem.current);
        //eventData.position = Input.mousePosition;
        eventData.position = _vec;

        List<RaycastResult> results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventData, results);
        //Debug.LogError(EventSystem.current.currentSelectedGameObject.name);

        /*foreach (RaycastResult result in results)
        {
            // 处理点击事件,例如调用方法等
            Debug.Log("Clicked on: " + result.gameObject.name);
        }*/
        //Debug.Log("Clicked on: " + results[0].gameObject.name);

        
        
    }

 

射线检测:

//射线检测
    public void CreateRay(Vector3 _vec)
    {
        //Ray ray = Camera.main.ScreenPointToRay(transform.position);
        Ray ray = Camera.main.ScreenPointToRay(_vec);
        //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        //Ray ray = Camera.main.ViewportPointToRay(Input.mousePosition);

        //Debug.LogError( ray.GetPoint(0));

        RaycastHit hitInfo;

        if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity))
        {
            // 检测到物体
            //Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
            //Debug.Log("检测到了 "+hitInfo.collider.gameObject.name);

            Vector3 _tempVec = ray.GetPoint(0);
            Vector3 vec = new Vector3(_tempVec.x, _tempVec.y, this.transform.position.z);
            //BeStruck(vec);

            
        }
        else
        {
            // 未检测到物体 画出长度为80的射线
            //Debug.DrawLine(ray.origin, ray.GetPoint(80), Color.red);
            //Debug.Log("未检测到");
        }
    }

 

posted on 2025-03-26 10:21  凌落成迷  阅读(126)  评论(0)    收藏  举报