zoukankan      html  css  js  c++  java
  • Unity3d 血条脚本

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class BloodControll : MonoBehaviour
    {
        public string playerName = "主角";
        private float height = 1;
    
        public Texture2D hpForeground;//血条的前景贴图
        public Texture2D hpBackground;//血条的背景贴图
        public float HP = 150;
        public float HPMax = 200;
        private float offset = 30;//位置偏移,计算位置时可能出现有一些误差,偏移消除这些误差
    
        public Font fort;//显示名字的文字字体样式
    
        private float HPGUIWidth = 200;
        private float HPGUIHeight = 20;
    
        private void Start()
        {
    
        }
    
        private void OnGUI()
        {
            //计算绘制血条和名称所在的世界坐标,此坐标就是玩家的世界坐标
            Vector3 worldPosition = new Vector3(transform.position.x, transform.position.y + height, transform.position.z);
            //血条和名称所在世界坐标转换成屏幕坐标
            Vector2 position = GameManage.GamePlayCamera.WorldToScreenPoint(worldPosition);
            //计算绘制血条和名称坐在的屏幕坐标,由于屏幕坐标屏幕在右下角是(0,0)点,左上角是(Screen.Width,Screen.Height)所以使用减法
            position = new Vector2(position.x, Screen.height - position.y);
    
            //计算绘制血条的宽度,这样就可以实现扣血时,血条宽度的变化
            float hpWidth = HP / HPMax * HPGUIWidth;
    
            //绘制血条
            GUI.DrawTexture(new Rect(position.x - HPGUIWidth / 2, position.y - HPGUIHeight - offset, HPGUIWidth, HPGUIHeight), hpBackground);
            GUI.DrawTexture(new Rect(position.x - HPGUIWidth / 2, position.y - HPGUIHeight - offset, hpWidth, HPGUIHeight), hpForeground);
    
            //计算绘制名称的大小,以确定绘制名称的位置
            Vector2 nameSize = GUI.skin.label.CalcSize(new GUIContent(playerName));
            GUI.color = Color.black;
            //设置字体样式
            GUI.skin.label.font = fort;
            //绘制名称
            GUI.Label(new Rect(position.x - (nameSize.x / 2), position.y - nameSize.y - HPGUIHeight - offset, nameSize.x, nameSize.y), playerName);
        }
    
    }
  • 相关阅读:
    Elasticsearch Mantanence Lessons Learned Today
    RabbitMQ Exchange & Queue Design Trade-off
    Understanding RabbitMQ Exchange & Queue
    Behind RabbitMQ Exchange Types
    七步,搭建基于Windows平台完美Jekyll博客环境
    How to Change RabbitMQ Queue Parameters in Production?
    Android Weekly Notes Issue #237
    Android Weekly Notes Issue #236
    Android Weekly Notes Issue #235
    Android Weekly Notes Issue #234
  • 原文地址:https://www.cnblogs.com/xuhongcai/p/12754198.html
Copyright © 2011-2022 走看看