Unity3d 札记-Tanks Tutorial 知识点汇总---如何设置生命条

对于坦克大战中的坦克,我们需要做一个如下图所示的生命条。

 

1\UI Slider

 

需要用到  UI组件 -Slider     Slider本身是一个可拖动的进度条,分为三部分 Background 背景(底部层)  FillArea(填充层 )  Handle Slide Area(滑动条)

   

这次用不着 Handle Slide Area  ,Delete 删去

相关属性如下:

Property:Function:
Interactable Will this component accept input? See Interactable.
Transition Properties that determine the way the control responds visually to user actions. See Transition Options.
Navigation Properties that determine the sequence of controls. See Navigation Options.
Fill Rect The graphic used for the fill area of the control.
Handle Rect The graphic used for the sliding “handle” part of the control
Direction The direction in which the slider’s value will increase when the handle is dragged. The options are Left To Right, Right To Left, Bottom To Top and Top To Bottom.
Min Value The value of the slider when the handle is at its extreme lower end (determined by the Direction property).
Max Value The value of the slider when the handle is at its extreme upper end (determined by the Direction property).
Whole Numbers Should the slider be constrained to integer values?
Value Current numeric value of the slider. If the value is set in the inspector it will be used as the initial value, but this will change at runtime when the value changes.

由于 这次作为血条 ,并不需要读取输入 ,所以  Interactable 不勾选     Transition 选择None 

将Canvas和Slider相关位置属性设置好,并使其成为Tank的子对象,居于坦克正下方。

Background 和Fill  选择预先导入的  环形图片

Fill 中 ImageType 设为Filled     ,Fill Origin 进度条起点 和Clockwise 看个人喜好设置。

 

UI 基本设置完毕。

 

2\血条旋转问题

 

由于血条 本身是 Tank 的子对象  ,所以 坦克的旋转会使  血条跟着旋转,因而视觉上会给玩家造成困扰。

所以需要添加一个  UIRotationController 

using UnityEngine;

public class UIDirectionControl : MonoBehaviour
{
    public bool m_UseRelativeRotation = true;  


    private Quaternion m_RelativeRotation;     


    private void Start()
    {
        m_RelativeRotation = transform.parent.localRotation;
      //获取Tank初始旋转角度
    }


    private void Update()
    {
        if (m_UseRelativeRotation)
            transform.rotation = m_RelativeRotation;
          //恒成立语句 使自身旋转角始终等于  Tank初始旋转角度
    }
}

3\ public static Color Lerp(Color a, Color b, float t);

 

返回的颜色  由 Color a   --------> Color b 

取决于  float  t    t 为 0时 为Color a   t为 1时 为Color  b

在这边也就是   t  = 实际生命值/初始生命值

 

 

posted @ 2016-08-23 15:41  dongdongdongdong  阅读(692)  评论(0编辑  收藏  举报