当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- AE learning 02: timeline
- Three. JS introductory learning notes 0: illustration of how webgl and threejs work
- 保证接口数据安全的10种方案
- Three singleton modes of unity (hungry man, lazy man, monobehavior)
- SPI master rx time out中断
- Whole process analysis of unity3d rendering pipeline
- Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
- Streaming end, server end, player end
- 融云斩获 2022 中国信创数字化办公门户卓越产品奖!
- numpy--数据清洗
猜你喜欢

深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)

numpy--疫情数据分析案例

After UE4 is packaged, mesh has no material problem
通知Notification使用全解析

LeetCode2_ Add two numbers

Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
![[wechat applet] Chapter (5): basic API interface of wechat applet](/img/63/e819113c7c93e273525bc850892972.png)
[wechat applet] Chapter (5): basic API interface of wechat applet

Continuous creation depends on it!

Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation

Async and await
随机推荐
招标公告:盘锦市人民医院盘锦医院数据库维保项目
持续创作,还得靠它!
Three. JS introductory learning notes 04: external model import - no material obj model
Odoo集成Plausible埋码监控平台
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Ue4/ue5 multi thread development attachment plug-in download address
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
LeetCode1_ Sum of two numbers
Three. JS introduction learning notes 12: the model moves along any trajectory line
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
Unity3D_ Class fishing project, bullet rebound effect is achieved
How does geojson data merge the boundaries of regions?
Shader Language
Use of SVN
Please supervise the 2022 plan
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
TS as a general cache method
Three. JS introductory learning notes 11:three JS group composite object
















