zoukankan      html  css  js  c++  java
  • 《杜增强讲Unity之Tanks坦克大战》7-坦克血条

    7 坦克血条

    点击菜单GameObject->UI->Slider创建Slider

     

    选中EventSystem,设置Horizontal Axis为HorzontalUI,Vertical Axis为VerticalUI

     
    image

    选中Canvas,设置Render Mode为World Space, Reference Pixels Per Unit为1

     
    image

    将Canvas拖到Tank中,使其成为Tank的子对象,相关参数设置如下

     
    image

    将Slider改名为HealthSlider,取消选择Interactable,设置Transition为None,Max Value改为100

     
    image

    在Hierarchy里面,按住Alt键点击HealthSlider前面的三角箭头

     
    image

    然后删除Handle Slide Area

    同时选中HealthSlider,Background,FillArea,Fill, 点击Rect Transform里面的Sketch,按着Alt键选中右下角的水平垂直拉伸.

     
    image

    选中Background,设置Source Image为Health Wheel,Color为红色,Alpha为80

     
    image
     
    image

    选中Fill,设置Source Image为Health Wheel,Color为绿色,Alpha为150,Image Type为Filled,Fill Method为Radial 360,Fill Origin为Left,取消选择Clockwise.

    [图片上传中...(image-d0dada-1539997823498-3)]

    新建Health.cs,声明float变量currentBlood为当前血值.

    public float currentBlood = 100; // 当前血值

    添加healthSlider显示当前血值

    public Slider slider; // 血槽

    为Health.cs添加TakeDamage方法

    public void TakeDamage (float damage) { // 受到伤害,开始掉血

        currentBlood -= damage; // 掉血
    
        slider.value = currentBlood; // 更新血槽显示
    
    }
    

    当坦克收到伤害,血值不断减少到小于等于0的时候,坦克播放爆炸效果和爆炸音效.

    从Prefabs里面找到TankExplosion坦克爆炸效果添加到坦克上面

     
    image

    private ParticleSystem ps; // 爆炸效果

    private AudioSource audioSource; // 声源
    

    还需要添加一个爆炸音效

    public AudioClip explosionAudio; // 爆炸音效

    [图片上传中...(image-a2ce6e-1539997823498-1)]

    在Start里面获取ps和audioSource

    ps = GetComponentInChildren <ParticleSystem> (); // 获取子对象的ParticleSystem

        audioSource = GetComponent<AudioSource> (); // 获取音源
    

    然后在血值减为0时播放爆炸效果

    if( currentBlood <= 0){

            ps.transform.parent = null; // 将爆炸效果从Shell里面移出
    
            ps.Play (); // 播放爆炸效果
    
            audioSource.Play (); // 播放爆炸音效
    
            Destroy (ps.gameObject, ps.duration); // 爆炸效果播放完毕之后移出爆炸效果的gameObject
    
            Destroy (gameObject); // 移出Shell的gameObject
    
        }
    

    最终代码

    [图片上传中...(image-fcf493-1539997823497-0)]

    Health.cs

    using UnityEngine;

    using UnityEngine.UI;

    using System.Collections;

    public class Health : MonoBehaviour {

    public Slider slider; // 血槽
    
    public float currentBlood = 100;  // 当前血值
    
    private ParticleSystem ps; // 爆炸效果
    
    private AudioSource audioSource; // 声源
    
    public AudioClip explosionAudio; // 爆炸音效
    
    // Use this for initialization
    
    void Start () {
    
        ps = GetComponentInChildren <ParticleSystem> (); // 获取子对象的ParticleSystem
    
        audioSource = GetComponent<AudioSource> (); // 获取音源
    
    }
    
    public void TakeDamage (float damage) { // 受到伤害,开始掉血
    
        currentBlood -= damage; // 掉血
    
        slider.value = currentBlood; // 更新血槽显示
    
        if( currentBlood <= 0){
    
            ps.transform.parent = null; // 将爆炸效果从Shell里面移出
    
            ps.Play (); // 播放爆炸效果
    
            audioSource.Play (); // 播放爆炸音效
    
            Destroy (ps.gameObject, ps.duration); // 爆炸效果播放完毕之后移出爆炸效果的gameObject
    
            Destroy (gameObject); // 移出Shell的gameObject
    
        }
    
    }
    

    }


    /// <-----------summary----------->
    /// < ---------杜增强的博客--------->
    /// < -专注于Unity游戏,VR/AR开发->
    /// <-----------summary----------->
  • 相关阅读:
    高效是如何来的
    find 删除指定日期的文件
    MySQL基础教程
    grep search information
    关于进程的问题
    linux useradd 命令
    host and ip 的关系
    git cherry-pick 教程
    正则练习
    正则表达式-获取
  • 原文地址:https://www.cnblogs.com/duzq/p/9832177.html
Copyright © 2011-2022 走看看