当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- numpy--数据清洗
- Postman generate timestamp, future timestamp
- numpy--疫情数据分析案例
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
- 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"?
- numpy---基础学习笔记
- 企业级日志分析系统ELK
- TS typescript type declaration special declaration field number is handled when the key key
- 用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
猜你喜欢

Streaming end, server end, player end

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

Three. JS introductory learning notes 18: how to export JSON files with Blender

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

JS array foreach source code parsing

A wave of open source notebooks is coming

You Yuxi, coming!

Enterprise log analysis system elk

航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆

Use of SVN
随机推荐
Detailed explanation of unity hot update knowledge points and introduction to common solution principles
Three. Introduction to JS learning notes 17: mouse control of 3D model rotation of JSON file
招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
Cut ffmpeg as needed, and use emscripten to compile and run
Learn good-looking custom scroll bars in 1 minute
Annexb and avcc are two methods of data segmentation in decoding
A wave of open source notebooks is coming
Plate - forme de surveillance par étapes zabbix
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
企业级日志分析系统ELK
Unity3D_ Class fishing project, bullet rebound effect is achieved
深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
How does geojson data merge the boundaries of regions?
Please supervise the 2022 plan
How to implement backspace in shell
20th anniversary of agile: a failed uprising
There are many ways to realize the pause function in JS
统计学习方法——感知机
















