Unity3D经典案例游戏:TANKS! Unity Tutorial - Phase 4 of 8 - Tank Health——TankHealth 相关源C#代码解析

该源代码转载自Unity游戏案例中的TANKS代码中

------------来自第二次使用Unity3D制作游戏的游戏制作新人小白

一、代码自我解析

二、油管学习地址

三、Unity3D源代码

 

 

一、源代码自我解析

using UnityEngine;
using UnityEngine.UI;

public class TankHealth : MonoBehaviour
{
    public float m_StartingHealth = 100f;                      // 坦克初始生命值
    public Slider m_Slider;                                            // 设置滑块表示坦克当前的生命值
    public Image m_FillImage;                                      // 滑块的图像组件
    public Color m_FullHealthColor = Color.green;       // 当血量健康的时候显示的颜色
    public Color m_ZeroHealthColor = Color.red;         // 当血量不健康的时候显示的颜色
    public GameObject m_ExplosionPrefab;                // 将在Awake中实例化的预制组件,然后在坦克死亡时使用。
    

    private AudioSource m_ExplosionAudio;                 // 坦克爆炸音效
    private ParticleSystem m_ExplosionParticles;         // 坦克爆炸粒子特效
    private float m_CurrentHealth;                                 // 坦克实时拥有的生命值
    private bool m_Dead;                                               // 坦克的生命值已经降至零以上了吗?


    private void Awake()
    {
        // 实例化爆炸预制件并参考上面的粒子系统
        m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent<ParticleSystem>();
        // 获取对已实例化预置的音频源的引用。
        m_ExplosionAudio = m_ExplosionParticles.GetComponent<AudioSource>();

        // 禁用预制件,以便在需要时可以激活它。
        m_ExplosionParticles.gameObject.SetActive(false);
    }


    private void OnEnable()
    {
        // 当坦克启用时,重置坦克的生命值以及它是否已死。
        m_CurrentHealth = m_StartingHealth;
        m_Dead = false;

        // 更新生命值滑块的值和颜色
        SetHealthUI();
    }


    public void TakeDamage(float amount)
    {
        // Adjust the tank's current health, update the UI based on the new health and check whether or not the tank is dead.
        // 重新计算生命值(被攻击后)
        m_CurrentHealth -= amount;

        // 适当地更改UI元素
        SetHealthUI();

        // 如果当前的生命值为零或低于零,并且还没有登记,那么就启用死亡。
        if (m_CurrentHealth <= 0f && !m_Dead)
        {
            OnDeath();
        }
    }


    private void SetHealthUI()
    {
        // Adjust the value and colour of the slider.
        // 适当地建立滑块的值
        m_Slider.value = m_CurrentHealth;

        //根据当前开始健康状态的百分比,选择颜色之间插值条的颜色
        m_FillImage.color = Color.Lerp(m_ZeroHealthColor , m_FullHealthColor , m_CurrentHealth / m_StartingHealth);

    }


    private void OnDeath()
    {
        // Play the effects for the death of the tank and deactivate it.
        // 设置标记,使此函数只调用一次
        m_Dead = true;

        // 移动实时爆炸的粒子预制件到坦克的位置上
        m_ExplosionParticles.transform.position = transform.position;
        m_ExplosionParticles.gameObject.SetActive(true);

        // 爆炸粒子播放
        m_ExplosionParticles.Play();

        // 爆炸粒子音效播放
        m_ExplosionAudio.Play();

        //将坦克从场景移除
        gameObject.SetActive(false);
    }
}

// 以上只是自己对于该代码的理解,如有误还望指出让我及时改正。

 

二、油管学习Unity地址  

 

https://www.youtube.com/watch?v=paLLfWd2k5A

 

三、Unity3D中该案例源代码:

using UnityEngine;
using UnityEngine.UI;

namespace Complete
{
    public class TankHealth : MonoBehaviour
    {
        public float m_StartingHealth = 100f;               // The amount of health each tank starts with.
        public Slider m_Slider;                             // The slider to represent how much health the tank currently has.
        public Image m_FillImage;                           // The image component of the slider.
        public Color m_FullHealthColor = Color.green;       // The color the health bar will be when on full health.
        public Color m_ZeroHealthColor = Color.red;         // The color the health bar will be when on no health.
        public GameObject m_ExplosionPrefab;                // A prefab that will be instantiated in Awake, then used whenever the tank dies.
        
        
        private AudioSource m_ExplosionAudio;               // The audio source to play when the tank explodes.
        private ParticleSystem m_ExplosionParticles;        // The particle system the will play when the tank is destroyed.
        private float m_CurrentHealth;                      // How much health the tank currently has.
        private bool m_Dead;                                // Has the tank been reduced beyond zero health yet?


        private void Awake ()
        {
            // Instantiate the explosion prefab and get a reference to the particle system on it.
            m_ExplosionParticles = Instantiate (m_ExplosionPrefab).GetComponent<ParticleSystem> ();

            // Get a reference to the audio source on the instantiated prefab.
            m_ExplosionAudio = m_ExplosionParticles.GetComponent<AudioSource> ();

            // Disable the prefab so it can be activated when it's required.
            m_ExplosionParticles.gameObject.SetActive (false);
        }


        private void OnEnable()
        {
            // When the tank is enabled, reset the tank's health and whether or not it's dead.
            m_CurrentHealth = m_StartingHealth;
            m_Dead = false;

            // Update the health slider's value and color.
            SetHealthUI();
        }


        public void TakeDamage (float amount)
        {
            // Reduce current health by the amount of damage done.
            m_CurrentHealth -= amount;

            // Change the UI elements appropriately.
            SetHealthUI ();

            // If the current health is at or below zero and it has not yet been registered, call OnDeath.
            if (m_CurrentHealth <= 0f && !m_Dead)
            {
                OnDeath ();
            }
        }


        private void SetHealthUI ()
        {
            // Set the slider's value appropriately.
            m_Slider.value = m_CurrentHealth;

            // Interpolate the color of the bar between the choosen colours based on the current percentage of the starting health.
            m_FillImage.color = Color.Lerp (m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth);
        }


        private void OnDeath ()
        {
            // Set the flag so that this function is only called once.
            m_Dead = true;

            // Move the instantiated explosion prefab to the tank's position and turn it on.
            m_ExplosionParticles.transform.position = transform.position;
            m_ExplosionParticles.gameObject.SetActive (true);

            // Play the particle system of the tank exploding.
            m_ExplosionParticles.Play ();

            // Play the tank explosion sound effect.
            m_ExplosionAudio.Play();

            // Turn the tank off.
            gameObject.SetActive (false);
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值