物体使用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;
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)));
}
}
}
效果:点击任一张牌可返回对应的编号(具体功能直接在最后三行改)
|