血条预制制作
完成血条(Health Bar)的预制设计。具体要求如下:
- 分别使用 IMGUI 和 UGUI 实现
- 使用 UGUI,血条是游戏对象的一个子元素,任何时候需要面对主摄像机
- 分析两种实现的优缺点
- 给出预制的使用方法
其实在之前的打飞碟游戏中,我完成了一个简易的血条设计,效果图如下:
当时的实现方式是通过在OnGUI中即时显示若干个 # 字符来表示血量的多少,关键性代码如下:
if (isStart) {
if (Input.GetButtonDown("Fire1")) act.hit(Input.mousePosition);
GUI.Label(new Rect(10, 5, 200, 50), "SCORE", textStyle);
GUI.Label(new Rect(10, 50, 200, 50), "LEVEL", textStyle);
GUI.Label(new Rect(Screen.width - 380, 5, 50, 50), "BLOOD", textStyle);
GUI.Label(new Rect(200, 5, 200, 50), act.getScore().ToString(), scoreStyle);
GUI.Label(new Rect(200, 50, 200, 50), act.getLevel().ToString(), scoreStyle);
for (int i = 0; i < blood; i++)
GUI.Label(new Rect(Screen.width - 220 + 20 * i, 5, 50, 50), "#", bStyle);
if (blood == 0) {
GUI.Label(new Rect(Screen.width / 2 - 130, Screen.height / 2 - 120, 100, 100), "Game Over", style);
if (GUI.Button(new Rect(Screen.width / 2 - 40, Screen.height / 2 - 30, 100, 50), "REPLAY")) {
bl