简体   繁体   中英

How to draw a crosshair in Unity (2D)

I have a Player class with an OnGUI() method, in which I'm calculating the position of a crosshair I want to draw. Also in the same scene, I have a Canvas GameComponent called Crosshair (on the UI layer), which has an Image child which contains the crosshair sprite/image. I haven't been able to find this anywhere, but how do I tell the code in OnGUI() to take this Crosshair object and render it at a specific position? Old answers suggest using Texture2D but that's been deprecated by now...

Please check if you are converting the position of your crosshair from world to screen By using Camera.ScreenToWorldPoint .

Also look at the link https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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