简体   繁体   English

重叠3D对象的OnPointerDown

[英]OnPointerDown for Overlapping 3D Objects

I have two identical 3D objects in my scene. 我的场景中有两个相同的3D对象。 Both of them have a boolean property: isSelected . 它们都有一个布尔属性: isSelected For one of the objects, isSelected is true. 对于其中一个对象,isSelected为true。 For the other - isSelected is false. 另一方面-isSelected为false。

Both objects have a script attached, which implements the IPointerDownHandler interface. 这两个对象都附加了一个脚本,该脚本实现了IPointerDownHandler接口。

At some point in the game, both 3D objects have the same coordinates (absolutely the same coordinates, the objects overlap each other). 在游戏中的某个时刻,两个3D对象都具有相同的坐标(绝对相同的坐标,这些对象彼此重叠)。 If I click an object with my mouse, is there a way for me to predict for which object the OnPointerDown event will been triggered? 如果我用鼠标单击一个对象,是否可以预测OnPointerDown事件将针对哪个对象触发? I only want the OnPointerDown event to trigger for the object with isSelected == true. 我只希望OnPointerDown事件为isSelected == true的对象触发。

Is there any way to achieve this? 有什么办法可以做到这一点? Should I use raycasts? 我应该使用射线广播吗? Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

Your question seems so easy I might misunderstand you! 您的问题似乎很简单,我可能会误解您!

if (!isSelected) return;
.. your handling code ..

Another idea is, in Unity you should really use components a lot, they are a natural part of things. 另一个想法是,在Unity中,您确实应该大量使用组件,它们是事物的自然组成部分。 Don't forget each of your c# scripts is indeed a "component". 不要忘记每个c#脚本的确是一个“组件”。

Google "Unity3d disable a component" for a basic tutorial Google“ Unity3d禁用组件”的基本教程

yourComponent.enabled = false;

You say 你说

I only want the OnPointerDown event to trigger for the object with isSelected == true. 我只希望OnPointerDown事件为isSelected == true的对象触发。

Thus, perhaps just turn off that whole component with the boolean. 因此,也许只是用布尔值关闭整个组件

Another cool idea is this: turn off the collider with the boolean. 另一个很酷的主意是:用布尔值关闭对撞机

Collider c = GetComponent<Collider>();
c.enabled = false;

Then you truly won't get the event, you see? 那您真的不会参加该活动吗?

Regarding your separate question 关于您的单独问题

there a way for me to predict for which object the OnPointerDown event will been triggered 有一种方法可以预测OnPointerDown事件将针对哪个对象触发

Since you're talking 3D objects, 由于您正在谈论3D对象,

1) If one is closer to the camera , that's the one 1)如果一个更靠近相机 ,那就是那个

2) If they are in truly identical 3D positions, which should never happen: No, there's absolutely no way to tell, it's "random". 2)如果它们处于真正相同的3D位置,这是永远都不会发生的:不,绝对没有办法说这是“随机的”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM