场景&道具 触屏选中Unity3D场景中的GameObject对象代码

如何通过iphone触屏选中Unity3D场景中的GameObject对象代码?

1.去camera的api里面看,有将2D的touch点产生一条从屏幕出发的ray,再用这个进行3D的碰撞,进而选取obj。

2.最简单的方法是对物体先赋予Collider,任意一种碰撞器皆可。然后赋予一个脚本,脚本里写OnMouseDown就行,直接支持触摸。


代码如下:

  1. <p style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;"><font color="#616161" face="微软雅黑"><span style="font-size: 14px; line-height: 30px;">Ray ray = mCamera.ScreenPointToRay (new Vector3 (touch.position.x, touch.position.y,0));</span></font></p><p style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;"><font color="#616161" face="微软雅黑"><span style="font-size: 14px; line-height: 30px;">RaycastHit[] hits;</span></font></p><p style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;"><span style="font-size: 14px; line-height: 30px;"><font color="#616161" face="微软雅黑"></font></span></p><p style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial;"><font color="#616161" face="微软雅黑"><font color="#616161" face="微软雅黑"><span style="font-size: 14px; line-height: 30px;">hits = Physics.RaycastAll (ray, 50);</span></font></font></p>
复制代码

更多教程请关注我学院woxueyuan。com
作者 1994922 发表于 2016-9-29 12:29:52
您需要登录后才可以回帖 登录 | 立即注册
1994922

关注0

粉丝3

发布159

热门作品推荐
glow
InTerra ~ Terrain Features 3.1.0 物体与
glow
Low Poly Cartoon House Interiors 1.41低
glow
15000幅故宫博物院馆藏国画合集 山水人物花
glow
【更新】Volumetric Light Beam 2.2.3体积
glow
Victorian VOL.1 - Furniture (Nanite and
glow
Anemoia Manor 适配UE5.4+怀旧复古庄园室内
glow
【更新】GIBLION 2024 Anime-NPR-Toon Fram
glow
【更新】Auto Hand - VR Physics Interacti
glow
【更新】Ragdoll Animator 2 1.0.3
glow
【更新】BG Database Data editor with Go