Unity3D教程:如何在Unity中绘制准星
发表于2017-02-08
射击游戏中需要有准星瞄准,那么在做开发时准星要如何绘制呢,考虑到有些人还不会,下面就给大家介绍下在Unity中绘制准星的方法,一起来看看吧。
1、首先从互联网上下载一个准星图片,并将其拖入到Unity工程中,然后编写以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using UnityEngine; using System.Collections; public class DrawStar : MonoBehaviour { public Texture2D texture; // Use this for initialization void OnGUI() { Rect rect = new Rect(Input.mousePosition.x - (texture.width/2), Screen.height - Input.mousePosition.y - (texture.height/2), texture.width, texture.height); GUI.DrawTexture(rect, texture); } } |
2、把脚本拖曳到主相机上,把工程中的图片拖曳到主相机的Texture下,运行程序,移动鼠标就会发现准星跟随鼠标移动,如下图所示: