using TouchScript;
using UnityEngine;
public class Player : MonoBehaviour
{
// 用于射线检测的的摄像头
public Camera RayCamera;
private void OnEnable()
{
if (TouchManager.Instance != null)
TouchManager.Instance.PointersPressed += pointersPressedHandler;
}
private void OnDisable()
{
if (TouchManager.Instance != null)
TouchManager.Instance.PointersPressed -= pointersPressedHandler;
}
void Start()
{
// 获取用于射线检测的的摄像头
RayCamera = GameObject.FindGameObjectWithTag("RayCamera").GetComponent<Camera>();
}
// 射线检测
void RayHitFunction(Vector2 pos)
{
UnityEngine.Ray ray = RayCamera.ScreenPointToRay(pos);
RaycastHit[] hits = Physics.RaycastAll(ray);
if (hits.Length > 0)
{
foreach (var item in hits)
{
GameObject gameObject = item.collider.gameObject;
print(item.collider.gameObject.name);
if (gameObject.CompareTag("AAA") && gameObject.activeSelf)
{
}
}
}
}
/// <summary>
/// 鼠标点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pointersPressedHandler(object sender, PointerEventArgs e)
{
foreach (var pointer in e.Pointers)
{
Debug.Log(pointer.Id + " touched down at " + pointer.Position);
RayHitFunction(pointer.Position);
}
}
}
|