ray & Click on the 3D object
helloz 4/3/2024 Unity3d
Object click in 3d scene (3d 场景中对象点击)
Event Type: Ray(事件类型:射线检测)
eventSystem:
Mouse | eventType | describe |
---|---|---|
left | 'game.inputMouseDown' | If the ray is detected, return the left click event, otherwise not trigger 如果射线检测到,返回左键点击事件,否则不触发 |
'game.inputMouseDown.'..this:GetInstanceID() | Same as above, the difference: if the ID of the clicked object is the same as the ID of the listener, the event will be triggered. 同上,区别:如果点击对象的ID和监听者的ID一致,将触发事件. | |
right | 'game.inputMouseDown_1' | If the ray is detected, return the left click event, otherwise not trigger 如果射线检测到,返回左键点击事件,否则不触发 |
'game.inputMouseDown_1.'..this:GetInstanceID() | Same as above, the difference: if the ID of the clicked object is the same as the ID of the listener, the event will be triggered. 同上,区别:如果点击对象的ID和监听者的ID一致,将触发事件. |
example1:
Bind the script to an object and, in run mode, click it with the mouse,Triggered when the left mouse button is pressed
function Start()
luaEvent:AddEventListener("game.inputMouseDown",this,onInputMouseDown) luaEvent:AddEventListener("game.inputMouseDown."..this:GetInstanceID(),this,onInputMouseDown1)
end
function onInputMouseDown(info)
if info:CheckData() then
local infodata = info.Data
log('click obj:'..infodata.transform.name)
end
end
function onInputMouseDown1(info)
if info:CheckData() then
local infodata = info.Data
log('click 1 obj:'..infodata.transform.name)
end
end
function OnDestroy()
luaEvent:RemoveAllEventListener(this)
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
example 2
example 3
log('ray')
local _camera =UnityEngine.Camera.main
function Update()
if UnityEngine.Input.GetMouseButtonDown(0) then
log('click')
local ray = UnityEngine.Camera.main:ScreenPointToRay(UnityEngine.Input.mousePosition)
local flag, hit = UnityEngine.Physics.Raycast(ray)
if flag then
log(hit.transform.name)
end
end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13