3D_HW9

3D游戏编程 HW9

血条(Health Bar)设计

IMGUI

这种实现比较容易, 直接在脚本里面添加相关的GUI即可.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IMGUI : MonoBehaviour {
    public float health;
    private float change_health;
    private Rect blood_rec;
    private Rect blood_inc;
    private Rect blood_dec;

    private int blood_rec_x;
    private int blood_rec_y;
    private int blood_rec_len;
    private int blood_rec_height;
    private float blood_unit;

    void Start() {
        health = 0;
        change_health = 0;
        blood_unit = 0.2f;
        blood_rec_x = 700;
        blood_rec_y = 270;
        blood_rec_len = 300;
        blood_rec_height = 30;

        blood_rec = new Rect(blood_rec_x, blood_rec_y, blood_rec_len, blood_rec_height);
        blood_inc = new Rect(blood_rec_x, blood_rec_y - 33, 80, 20);
        blood_dec = new Rect(blood_rec_x + 100, blood_rec_y - 33, 80, 20);
    }

    void OnGUI() {
        if (GUI.Button(blood_inc, "increase")) {
            change_health = change_health + blood_unit > 1.0f ? 1.0f : change_health + blood_unit;
        }
        if (GUI.Button(blood_dec, "decrease")) {
            change_health = change_health - blood_unit < 0.0f ? 0.0f : change_health - blood_unit;
        }
        health = Mathf.Lerp(health, change_health, 0.05f);
        GUI.HorizontalScrollbar(blood_rec, 0.0f, health, 0.0f, 1.0f);
    }

    private void Update() {
        
    }
}

演示效果

gif动图静态图片
在这里插入图片描述在这里插入图片描述

UGUI

我们为上图的小恐龙添加一个血条.

  • 添加UI -> Canvas
  • Canvas,添加Slider
  • 删除Handle Slider Area
  • 删除Background
  • 修改颜色
  • 设置maxValue

演示效果

在这里插入图片描述

两者比较

IMGUI的文档如下: Unity originally had just two GUI Components. These have since been replaced by both the modern UI System, and the scripting-driven IMGUI System. The original GUI components are kept for legacy purposes but should not be used for new projects
IMGUI的特点是性能比较低, 主要用于游戏调试与自定义的Inspector面板.
而UGUI的运行效率会比IMGUI强, 性能比较好, 但是缺点是功能不是很完善, 目前主要用于游戏中的交互.

预制的使用方法

  • 将预制拖入scene中.
  • 导入相应的资源, 构建自己需要的场景.
  • 然后将Canvas prefab拖到物体上, 变为子对象.
  • 最后修改相应的属性.

视频网址

http://www.iqiyi.com/w_19say2kr65.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值