当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- 安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
- 深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
- There are many ways to realize the pause function in JS
- Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
- [excelexport], Excel to Lua, JSON, XML development tool
- 分步式监控平台zabbix
- Detailed explanation of unity hot update knowledge points and introduction to common solution principles
- Three. JS introductory learning notes 04: external model import - no material obj model
- Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
- Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
猜你喜欢
Three. JS introductory learning notes 04: external model import - no material obj model
统计学习方法——感知机
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
分步式监控平台zabbix
Numpy -- data cleaning
C4D learning notes 2- animation - timeline and time function
It's different for rich people to buy a house
Unity drawing plug-in = = [support the update of the original atlas]
Syntax of generator function (state machine)
Three. JS introductory learning notes 10:three JS grid
随机推荐
Numpy -- data cleaning
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
Three. JS introductory learning notes 10:three JS grid
Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
Whole process analysis of unity3d rendering pipeline
Three. JS introductory learning notes 11:three JS group composite object
Ue4/ue5 multi thread development attachment plug-in download address
What about the pointer in neural network C language
TCP framework___ Unity
如何在shell中实现 backspace
Unity drawing plug-in = = [support the update of the original atlas]
有钱人买房就是不一样
Shader basic UV operations, translation, rotation, scaling
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
Step by step monitoring platform ZABBIX
LeetCode1_ Sum of two numbers
After UE4 is packaged, mesh has no material problem
Particle effect for ugui
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"