has been used Ngui HUD text plugin to do this function, feel a small function on a plug-in is simply insane. Then I wrote a ~ idea and plug-in the same way. But Ah but ah ~ My this small idea seems definitely more convenient than the plug-in AH ~ because Ah, I have this one function Ah ~
Waiter, on the code ~ ~ ~
Camera Cam;//a camera for emitting raysCamera Uicam;//the camera at the UI layerVector3 MP;//Mouse PositionTransform Targettransform;//the selected object . PublicUILabel Lab; voidStart () {Cam= This. Getcomponent<camera>(); Uicam= Gameobject.find ("Camera"). Getcomponent<camera>(); Lab= Gameobject.find ("Label"). Getcomponent<uilabel>(); } voidUpdate () {if(Tarraycast ())//determine if the mouse refers to an object { //when the coordinates are converted, I use the "specify the object y-axis upward 0.3f", of course, only the general guarantee of the matching position, the best way is to manually specify, that is, manually drag an empty object to the "object that needs to be clicked", fixed its suitable position, and then coordinates with the empty object of ~Vector3 pos = Cam. Worldtoviewportpoint (Targettransform.position +NewVector3 (0, Targettransform.localscale.y/2+0.3f,0)); Lab.transform.position=Uicam.viewporttoworldpoint (POS); Lab.text=Targettransform.name; } } //Ray detection part, do not understand can see my previous article ~ Public BOOLTarraycast () {Lab.text=NULL; MP=input.mouseposition; Targettransform=NULL; if(Cam! =NULL) {Raycasthit hitinfo; Ray Ray .= Cam. Screenpointtoray (NewVector3 (mp.x, MP.Y, 0f)); if(Physics.raycast (Ray.origin, Ray.direction, outhitinfo)) {Targettransform=HitInfo.collider.transform; return true; } } return false; }}
"Unity" mouse pointing to an object, displaying the name of the object, etc.