第一种方式:
1.百度的第三方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TouchType : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{//判断是否是点击事件
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
//如果是一根手指触摸屏幕而且是刚开始触摸屏幕
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Debug.Log("触碰的对象 :" + hitInfo.collider.gameObject.name);
GameObject.Find("title/Text").GetComponent<Text>().text = hitInfo.collider.gameObject.name;
if (Input.GetTouch(0).tapCount == 2 && hitInfo.collider.gameObject.name == "wolun")
//判断点击的次数
{
}
}
}
}
}
}
2.相机需要添加(以下组件)

3.被点击物体一定要包含检测组键
比如:box ,meshColider,2dClips,等等
4.第一个方法是全局的,随便挂在一个物体下就行,全局就只能出现一个不然事件触发会触发多次****
第二种方式:Trigger(组件可以事件,比较繁琐)
本文详细介绍了在Unity中实现点击事件的方法,包括使用射线投射检测屏幕上的物体。通过代码示例,讲解了如何在物体被点击时获取物体名称,并更新UI文本。同时,提到了相机组件、碰撞体组件的重要性,以及触发器组件的另一种实现方式。

1万+

被折叠的 条评论
为什么被折叠?



