Tip: This article shows the click event, and there are other self-research
foreword
很多情况下我们需要对场景内的物体进行交互,今天我们采用射线检测的方法进行点击。
For example: click on the Cube in the scene to start rotating
提示:以下是本篇文章正文内容,下面案例可供参考
1. Principle
Shoot a ray from the camera, trigger the Collider of the object, and execute the method after triggering.
2. Use steps
1. Prerequisites
1. Add the PhysicsRaycaster script to the MainCamera in the scene.
2. Add the event system EventSystem in the scene.
3. Create a Cube with Collide.
4. Add an EventTrigger script to the cube, click Add New Event Type, and select PointerClick.
5. Create a new script.
The code is as follows (example):
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class CubeClick: MonoBehaviour
{
public void OnClick()
{
Debug.Log("点击Cube");
}
}
2. Using scripts
Mount the script on the Cube, drag the Cube to
Select the written method OnClick
Summarize
Click the object in the scene here to complete, and click the cube to print.