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

Unity チュートリアルのタワーディフェンステンプレートを触ってみる(24)では「その他の効果、ビジュアライザー、挙動編」を進めた。今回はエージェントに追加した HealthBar に付属している HealthVisualizer の解説を行っていく。
1.その他の効果、ビジュアライザー、挙動編 – HealthVisualizer.cs
HealthVisualizer.cs について稚拙ながら解説
「HealthVisualizer.cs」は「Assets/Scripts/Core/Health/HealthVisualizer.cs」の指しておりスクリプトについては以下の通り。内容としては HealthBar の更新処理を行っている。
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 using UnityEngine;
namespace Core.Health
{
/// <summary>
/// Class to visualizer the health of a damageable
/// </summary>
public class HealthVisualizer : 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>
/// The object whose X-scale we change to decrease the health bar. Should have a default uniform scale
/// </summary>
public Transform healthBar;
/// <summary>
/// The object whose X-scale we change to increase the health bar background. Should have a default uniform scale
/// </summary>
public Transform backgroundBar;
/// <summary>
/// Whether to show this health bar even when it is full
/// </summary>
public bool showWhenFull;
/// <summary>
/// Camera to face the visualization at
/// </summary>
protected Transform m_CameraToFace;
/// <summary>
/// Damageable whose health is visualized
/// </summary>
protected Damageable m_Damageable;
/// <summary>
/// Updates the visualization of the health
/// </summary>
/// <param name="normalizedHealth">Normalized health value</param>
public void UpdateHealth(float normalizedHealth)
{
Vector3 scale = Vector3.one;
if (healthBar != null)
{
scale.x = normalizedHealth;
healthBar.transform.localScale = scale;
}
if (backgroundBar != null)
{
scale.x = 1 - normalizedHealth;
backgroundBar.transform.localScale = scale;
}
SetVisible(showWhenFull || normalizedHealth < 1.0f);
}
/// <summary>
/// Sets the visibility status of this visualiser
/// </summary>
public void SetVisible(bool visible)
{
gameObject.SetActive(visible);
}
/// <summary>
/// Assigns the damageable, subscribing to the damaged event
/// </summary>
/// <param name="damageable">Damageable to assign</param>
public void AssignDamageable(Damageable damageable)
{
if (m_Damageable != null)
{
m_Damageable.healthChanged -= OnHealthChanged;
}
m_Damageable = damageable;
m_Damageable.healthChanged += OnHealthChanged;
}
/// <summary>
/// Turns us to face the camera
/// </summary>
protected virtual void Update()
{
Vector3 direction = m_CameraToFace.transform.forward;
transform.forward = -direction;
}
/// <summary>
/// Assigns a damageable if damageableBehaviour is populated
/// </summary>
protected virtual void Awake()
{
if (damageableBehaviour != null)
{
AssignDamageable(damageableBehaviour.configuration);
}
}
/// <summary>
/// Caches the main camera
/// </summary>
protected virtual void Start()
{
m_CameraToFace = UnityEngine.Camera.main.transform;
}
void OnHealthChanged(HealthChangeInfo healthChangeInfo)
{
UpdateHealth(m_Damageable.normalisedHealth);
}
}
}
45行目 : 「public void UpdateHealth(float normalizedHealth)」は体力ゲージの更新を行っている。内容としては引数として正規化(0 ~ 1)に変換された体力を受け取っており、体力バーとバックグラウンドバーがあれば正規化した割合で更新を行っている。
76行目 : 「public void AssignDamageable(Damageable damageable)」は Damageable の割当を行っている。内容としては引数の Damageable を自身のグローバル変数に代入し、Damageable の healthChanged に OnHealthChanged 関数を設定している。
89行目 : 「protected virtual void Update()」は Unity 固有の更新処理を行っている。内容としては HealthBar を正面に向けるように調整している。
98行目 : 「protected virtual void Awake()」は Unity 固有の起動処理を行っている。
109行目 : 「protected virtual void Start()」は Unity 固有の開始処理処理を行っている。
114行目 : 「void OnHealthChanged(HealthChangeInfo healthChangeInfo)」は UpdateHealth の処理を行っている。