Unity チュートリアルのタワーディフェンステンプレートを触ってみる(26)

Unity チュートリアルのタワーディフェンステンプレートを触ってみる(25)ではエージェントに追加した HealthBar に付属している HealthVisualizer の解説を行った。今回はエージェントに追加した DeathEffect について解説を行っていく。
1.その他の効果、ビジュアライザー、挙動編 – DeathEffect.cs
DeathEffect.cs について稚拙ながら解説
「DeathEffect.cs」は「Assets/Scripts/Core/Health/DeathEffect.cs」の指しておりスクリプトについては以下の通り。内容としては DeathEffect の再生を行っている。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 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();
}
}
}
36行目 : 「public void AssignDamageable(Damageable damageable)」は前回の AssignDamageable と同様に Damageable の割当を行っている。内容としては引数の Damageable を自身のグローバル変数に代入し、Damageable の died に OnDied 関数を設定している。
前回の記事についてはこちらを参照してほしい。
49行目 : 「protected virtual void Awake()」は Unity 固有の起動処理を行っている。
60行目 : 「void OnDied(HealthChangeInfo healthChangeInfo)」はエフェクトの再生を行っている。