Unity C# 获取手机手势坐标检测物体(easytouch替换方案)

物体使用box collider碰撞检测太消耗性能,用鼠标坐标检测可以去掉碰撞器,直接贴代码

using System;
using System.Collections;
using System.Collections.Generic;
using Spine.Unity;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class touch_position : MonoBehaviour
{
    public GameObject _gameObject1;
    public GameObject _gameObject2;
    void Start()
    {
        
    }
    void Update()
    {
    	//鼠标坐标
        float touchx = Input.mousePosition.x;
        float touchy = Input.mousePosition.y;
        //将3D物体坐标映射到2D屏幕上    
        Vector2 convertedGUIPos1 = HandleUtility.WorldToGUIPoint(_gameObject1.transform.position);
        Vector2 convertedGUIPos2 = HandleUtility.WorldToGUIPoint(_gameObject2.transform.position);
        float pai_width = convertedGUIPos2.x - convertedGUIPos1.x;
        Vector3 length = _gameObject1.GetComponent<Renderer>().bounds.size;
        float pai_height = length.y / length.x * pai_width;

        float fixedright = convertedGUIPos2.x + pai_width / 2;
        float workHeight = Screen.height;
        float pai_y = workHeight - convertedGUIPos2.y;
        //划分点击响应的范围
        if (touchx >= fixedright - pai_width * 13 && touchx <= fixedright && touchy <=200 
                                                  && touchy <= pai_y + pai_height / 2
                                                  && touchy >= pai_y - pai_height / 2)
        {
            float num = (fixedright - touchx) / pai_width;
            Debug.Log("mouse:" + Input.mousePosition);
            Debug.Log("number:" + (13 - Math.Floor(num)));
        }
    }
}

效果:点击任一张牌可返回对应的编号(具体功能直接在最后三行改)
在这里插入图片描述
但是导出打包的时候console报错:
error CS0103: The name ‘HandleUtility’ does not exist in the current context

百度后发现代码中使用HandleUtility这个类来获取手机点击坐标,而这个API属于EditorUtility类,这个类又隶属于UnityEditor这个命名空间,就必须引入using UnityEditor,但是此命名空间是Unity在编辑器模式下才可以使用的库,所以打包时使用UnityEditor的脚本是会报错的,只能替换了,把WorldToGUIPoint换成WorldToGUIPoint,&在手机上测试要换成获取手机手势,不过还是保留了获取鼠标点击的部分,方便测试。

using System;
using System.Collections;
using System.Collections.Generic;
//using UnityEditor;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.UI;


public class touch_position : MonoBehaviour
{
    public GameObject _gameObject1;
    public GameObject _gameObject2;
    public GameObject _gameObject3;

    public Camera camera;
    public float num;
    public Touch touch;
    //public float touchx;
    void Start()
    {
        
    }
    void Update()
    {
        if (Input.touchCount > 0)
        {
            touch = Input.GetTouch(0);
            Debug.Log("Position:" + touch.position);
            float phonex = touch.position.x;
            float phoney = touch.position.y;
            Position(phonex, phoney);

        }
        //float touchx = Input.mousePosition.x;
        //float touchy = Input.mousePosition.y;
        //Position(touchx, touchy);
        
        
    }

    public void Position(float x, float y)
    {
        //Vector2 convertedGUIPos1 = HandleUtility.WorldToGUIPoint(_gameObject1.transform.position);
       // Vector2 convertedGUIPos2 = HandleUtility.WorldToGUIPoint(_gameObject2.transform.position);
       
       Vector2 convertedGUIPos1 = camera.WorldToScreenPoint(_gameObject1.transform.position);
       Vector2 convertedGUIPos2 = camera.WorldToScreenPoint(_gameObject2.transform.position);
       Vector2 convertedGUIPos3 = camera.WorldToScreenPoint(_gameObject3.transform.position);
       convertedGUIPos1.y = Screen.height - convertedGUIPos1.y;
       convertedGUIPos2.y = Screen.height - convertedGUIPos2.y;
        
        //Debug.Log("2:"+convertedGUIPos1);
        
        
        
        float pai_width = convertedGUIPos2.x - convertedGUIPos1.x;
        float pai_width1 = convertedGUIPos3.x - convertedGUIPos2.x;
        Vector3 length = _gameObject1.GetComponent<Renderer>().bounds.size;
        float pai_height = length.y / length.x * pai_width;
    
        float fixedright = convertedGUIPos2.x + pai_width / 2;
        float workHeight = Screen.height;
        float pai_y = workHeight - convertedGUIPos2.y;
        if (x >= fixedright - pai_width * 13 && x <= fixedright && y <=200 
            && y <= pai_y + pai_height / 2
            && y >= pai_y - pai_height / 2)
        {
            num = (fixedright - x) / pai_width;
            //Debug.Log("mouse:" + Input.mousePosition);
            //Debug.Log("number:" + (13 - Math.Floor(num)));
        }
        else if (x >= fixedright + pai_width1 - pai_width && x <= fixedright + pai_width1
                                                          && y <= pai_y + pai_height / 2
                                                          && y >= pai_y - pai_height / 2)
        {
            num = -1;
        }
    }
    
    private void OnGUI()
    {
        GUIStyle fontStyle = new GUIStyle();  
        fontStyle.alignment=TextAnchor.MiddleCenter;
        fontStyle.fontSize=45;
        fontStyle.normal.textColor=Color.white;
        
        GUI.Button(new Rect(10, 10, 300, 300), touch.position.ToString(),fontStyle);
        GUI.Button(new Rect(320, 10, 300, 300), (13 - Math.Floor(num)).ToString(),fontStyle);//手机
        //GUI.Button(new Rect(120, 10, 100, 100), (13 - Math.Floor(num)).ToString());//电脑
    }
}

打包到真机测试,搞定!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值