当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
- 【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
- 95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
- asyncio 概念和用法
- TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
- Spin animation of Cocos performance optimization
- Iterator and for of.. loop
- Plate - forme de surveillance par étapes zabbix
- Syntaxhighlight highlights the right scroll bar
- TS typescript type declaration special declaration field number is handled when the key key
猜你喜欢
Three. JS introductory learning notes 03: perspective projection camera
C4D learning notes 2- animation - timeline and time function
Continuous creation depends on it!
Enterprise log analysis system elk
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
融云斩获 2022 中国信创数字化办公门户卓越产品奖!
Unity3D_ Class fishing project, bullet rebound effect is achieved
Vite path alias @ configuration
Dotween -- ease function
随机推荐
用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
有钱人买房就是不一样
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
numpy---基础学习笔记
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
Ue4/ue5 multi thread development attachment plug-in download address
一个普通人除了去工厂上班赚钱,还能干什么工作?
Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
Three. JS introductory learning notes 15: threejs frame animation module
Syntax of generator function (state machine)
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
Numpy -- epidemic data analysis case
Virtual memory, physical memory /ram what
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
Three. JS introductory learning notes 03: perspective projection camera
JS array foreach source code parsing
Vite path alias @ configuration
UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)