当前位置:网站首页>Unity3d click events added to 3D objects in the scene
Unity3d click events added to 3D objects in the scene
2022-07-07 16:07:00 【Le_ Sam】
Unity3D - Scene 3D Add mouse click event to the object

That is, after implementing the interface in our clicked class , In the implementation method OnPointerClick, Operate the post click processing in this method .
using UnityEngine;
using UnityEngine.EventSystems;
public class EventClick : MonoBehaviour,IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
print(" Click. ::"+this.name);
}
}
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ClickEventTrigger : MonoBehaviour
{
public void OnClick()
{
print("MyOnClick Click. ::"+this.name);
}
}
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ClickEventTrigger : MonoBehaviour {
// Use this for initialization
void Start () {
// Get or add EventTrigger Components
EventTrigger trigger = transform.GetComponent();
if (trigger == null)
{
trigger = transform.gameObject.AddComponent();
}
// initialization EventTrigger.Entry Array of If the event trigger array is initialized here , So in ide Statically added events will be lost
//trigger.triggers = new List();
// Create a variety of EventTrigger.Entry The type of
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerEnter;// Set up Entry Of eventID type namely EventTriggerType Various enumerations of ( For example, mouse click , slide , Drag, etc )
UnityAction callback = new UnityAction(OnPointerEnter); // Registration agent
entry.callback.AddListener(callback);// Add proxy events to EventTrigger.Entry
EventTrigger.Entry entry2 = new EventTrigger.Entry();
entry2.eventID = EventTriggerType.PointerDown;
UnityAction callback1 = new UnityAction(OnPointerDown);
entry2.callback.AddListener(callback1);
// stay EventTrigger.Entry Add to the array of EventTrigger.Entry
trigger.triggers.Add(entry);
trigger.triggers.Add(entry2);
}
private void OnPointerDown(BaseEventData arg0)
{
Debug.Log("OnPointerDown");
}
private void OnPointerEnter(BaseEventData arg0)
{
Debug.Log("OnPointerEnter");
}
}
using UnityEngine;
public class ClickRayCastHitControl : MonoBehaviour {
Ray ray;
RaycastHit hit;
GameObject obj;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log(" Left click ");
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Debug.Log(hit.collider.gameObject.name);
obj = hit.collider.gameObject;
// By name
if (obj.name.Equals("BeiJiChuan"))
{
Debug.Log(" Point in " + obj.name);
}
// Through the label
if (obj.tag == "ClicObj")
{
Debug.Log(" Point in " + obj.name);
}
}
}
}
}
边栏推荐
- 深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
- Whole process analysis of unity3d rendering pipeline
- Cut ffmpeg as needed, and use emscripten to compile and run
- Unity3D_ Class fishing project, bullet rebound effect is achieved
- 通知Notification使用全解析
- 持续创作,还得靠它!
- 安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
- Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
- Simple understanding and application of TS generics
- [wechat applet] Chapter (5): basic API interface of wechat applet
猜你喜欢

神经网络c语言中的指针是怎么回事

Vite path alias @ configuration

L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des

Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"

【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境

Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?

A wave of open source notebooks is coming

Three. JS introductory learning notes 04: external model import - no material obj model

Three. JS introductory learning notes 03: perspective projection camera

谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
随机推荐
Three. JS introductory learning notes 13: animation learning
Enterprise log analysis system elk
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
Mysql database backup script
喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
Dotween -- ease function
持续创作,还得靠它!
Summary of knowledge points of xlua hot update solution
招标公告:盘锦市人民医院盘锦医院数据库维保项目
[wechat applet] Chapter (5): basic API interface of wechat applet
UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
Numpy --- basic learning notes
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
Odoo集成Plausible埋码监控平台
一大波开源小抄来袭
有钱人买房就是不一样
TCP framework___ Unity
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
















