当前位置:网站首页>unity 3D物体添加 点击事件
unity 3D物体添加 点击事件
2022-07-25 05:01:00 【云小川】
方法一:物体 挂在 EventTrigger 选择 PointerClick 注册响应方法
相机 必须挂在 PhysicsRaycaster
场景中必须有:EventSystem(一般创建UI自动添加的)
物体 必须有 Collider
简单粗暴版(相机 必须挂在 PhysicsRaycaster)
using HighlightPlus;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ObjClickEvent : MonoBehaviour
{
public GameObject[] cube1; //3D物体集合
private void Start()
{
//为3D物体添加点击事件
for (int i = 0; i < cube1.Length; i++)
{
AddObjectClickEvent(cube1[i]);
}
}
public void OnClick()
{
Debug.Log("OnClick");
transform.GetComponent<HighlightEffect>().highlighted = false;
}
public void AddObjectClickEvent(GameObject itemObject)
{
var box = itemObject.GetComponent<BoxCollider>();
if (box == null)
{
box = itemObject.AddComponent<BoxCollider>();
}
var item = itemObject.GetComponent<ObjItme>();
if (item == null)
{
item = itemObject.AddComponent<ObjItme>();
}
EventTrigger trigger = itemObject.GetComponent<EventTrigger>();
if (trigger == null)
{
trigger = itemObject.AddComponent<EventTrigger>();
}
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerClick;
entry.callback.AddListener(item.OnClickItem);
trigger.triggers.Clear();
trigger.triggers.Add(entry);
}
}
边栏推荐
- C# 之 FileStream类介绍
- 2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f
- Blog Description & message board
- 深入掌握Pod
- Wechat official account all article download links to get
- Get the parameters of the browser address bar
- If you don't know these 20 classic redis interview questions, don't go to the interview!
- Why does the official not recommend using UUID as MySQL primary key
- Summary of UPR optimization suggestions of unity
- STM32 Development Notes 121: Kalman filter I understand
猜你喜欢

After watching the latest interview with big manufacturers, these six JVM interview questions were asked

Teach you three ways to optimize the performance from 20s to 500ms

中创算力荣获「2022年科技型中小企业」认定
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

2、 Mysql database foundation

Ffmpeg download and installation

Salt and ice particles cannot be distinguished

QT download installation tutorial

【浅析STM32之GPIO寄存器(CRL/CRH)配置 】

二、MySQL数据库基础
随机推荐
When we talk about immutable infrastructure, what are we talking about
This low code reporting tool is needed for data analysis
Getting started with scratch
Teach you three ways to optimize the performance from 20s to 500ms
Logu p3398 hamsters find sugar solution
ThreadLocal Kills 11 consecutive questions
STM32 development note 120: solve the problem that%f in printf cannot be output
【基于stm32f103的SHT30温湿度显示】
小说抓取实战
[untitled]
OA and fansoft Bi cross system users, departments and posts synchronous summary
Libenent and libev
二、MySQL数据库基础
Paper:《Peeking Inside the Black Box: Visualizing Statistical Learning with Plots of Individual Condi
2022-7-15 summary
Bypass XSS filters in Web Applications
Style transfer -- CCPL: contrast coherence preserving loss for versatile style transfer
Token value replacement of burpsuite blasting
[c language] custom type (structure ~ enumeration ~ Union)
Summary of UPR optimization suggestions of unity