当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- 深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
- Spin animation of Cocos performance optimization
- Wireless sensor networks -- ZigBee and 6LoWPAN
- Learn good-looking custom scroll bars in 1 minute
- Shader basic UV operations, translation, rotation, scaling
- Iterator and for of.. loop
- Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
- asyncio 概念和用法
- Three. JS introductory learning notes 04: external model import - no material obj model
- Streaming end, server end, player end
猜你喜欢

融云斩获 2022 中国信创数字化办公门户卓越产品奖!
![Application example of infinite list [uigridview]](/img/11/3be1c63680e6de8f068e79690ecf12.jpg)
Application example of infinite list [uigridview]

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

Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc

Iterator and for of.. loop

喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配

PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()

Three. JS introductory learning notes 19: how to import FBX static model

JS array foreach source code parsing

Odoo集成Plausible埋码监控平台
随机推荐
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
Introduction to pyGame games
Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
Function: JS Click to copy content function
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
The unity vector rotates at a point
有钱人买房就是不一样
Three. JS introductory learning notes 18: how to export JSON files with Blender
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
A wave of open source notebooks is coming
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
C Alibaba cloud OSS file upload, download and other operations (unity is available)
LeetCode3_ Longest substring without duplicate characters
After UE4 is packaged, mesh has no material problem
招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
torch.numel作用
















