Unity チュートリアルのタワーディフェンステンプレートを触ってみる(26)
Unity チュートリアルのタワーディフェンステンプレートを触ってみる(25)ではエージェントに追加した HealthBar に付属している HealthVisualizer の解説を行った。今回はエージェントに追加した DeathEffect について解説を行っていく。
1.その他の効果、ビジュアライザー、挙動編 – DeathEffect.cs
DeathEffect.cs について稚拙ながら解説
「DeathEffect.cs」は「Assets/Scripts/Core/Health/DeathEffect.cs」の指しておりスクリプトについては以下の通り。内容としては DeathEffect の再生を行っている。
[cce_csharp]using Core.Utilities;
using UnityEngine;
namespace Core.Health
{
/// <summary>
/// Simple class to instantiate a ParticleSystem on a given Damageable's death
/// </summary>
public class DeathEffect : MonoBehaviour
{
/// <summary>
/// The DamageableBehaviour that will be used to assign the damageable
/// </summary>
[Tooltip("This field does not need to be populated here, it can be set up in code using AssignDamageable")]
public DamageableBehaviour damageableBehaviour;
/// <summary>
/// Death particle system
/// </summary>
public ParticleSystem deathParticleSystemPrefab;
/// <summary>
/// World space offset of the <see cref="deathParticleSystemPrefab"/> position
/// </summary>
public Vector3 deathEffectOffset;
/// <summary>
/// The damageable
/// </summary>
protected Damageable m_Damageable;
/// <summary>
/// Subscribes to the damageable's died event
/// </summary>
/// <param name="damageable"></param>
public void AssignDamageable(Damageable damageable)
{
if (m_Damageable != null)
{
m_Damageable.died -= OnDied;
}
m_Damageable = damageable;
m_Damageable.died += OnDied;
}
/// <summary>
/// If damageableBehaviour is populated, assigns the damageable
/// </summary>
protected virtual void Awake ()
{
if (damageableBehaviour != null)
{
AssignDamageable(damageableBehaviour.configuration);
}
}
/// <summary>
/// Instantiate a death particle system
/// </summary>
void OnDied(HealthChangeInfo healthChangeInfo)
{
if (deathParticleSystemPrefab == null)
{
return;
}
var pfx = Poolable.TryGetPoolable<ParticleSystem>(deathParticleSystemPrefab.gameObject);
pfx.transform.position = transform.position + deathEffectOffset;
pfx.Play();
}
}
}[/cce_csharp]
36行目 : 「public void AssignDamageable(Damageable damageable)」は前回の AssignDamageable と同様に Damageable の割当を行っている。内容としては引数の Damageable を自身のグローバル変数に代入し、Damageable の died に OnDied 関数を設定している。
前回の記事についてはこちらを参照してほしい。
49行目 : 「protected virtual void Awake()」は Unity 固有の起動処理を行っている。
60行目 : 「void OnDied(HealthChangeInfo healthChangeInfo)」はエフェクトの再生を行っている。
