該源代碼轉載自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);
}
}
}