当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- 招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
- LeetCode2_ Add two numbers
- 持续创作,还得靠它!
- Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
- Dotween -- ease function
- Align individual elements to the right under flex layout
- Mysql database basic operation DQL basic query
- SPI master rx time out中断
- 分类模型评价标准(performance measure)
- Annexb and avcc are two methods of data segmentation in decoding
猜你喜欢
SPI master rx time out中断
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
Three. JS introductory learning notes 18: how to export JSON files with Blender
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
C4D learning notes 3- animation - animation rendering process case
一大波开源小抄来袭
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
Three. JS introductory learning notes 03: perspective projection camera
Cut ffmpeg as needed, and use emscripten to compile and run
随机推荐
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
A link opens the applet code. After compilation, it is easy to understand
Odoo集成Plausible埋码监控平台
Syntax of generator function (state machine)
Postman generate timestamp, future timestamp
Application example of infinite list [uigridview]
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
It's different for rich people to buy a house
VS2005 strange breakpoint is invalid or member variable value cannot be viewed
LeetCode1_ Sum of two numbers
Particle effect for ugui
Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
Numpy -- epidemic data analysis case
Function: JS Click to copy content function
Leetcode-231-2的幂
分类模型评价标准(performance measure)
Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
Three. JS introductory learning notes 13: animation learning