Unity チュートリアルのタワーディフェンステンプレートを触ってみる(29)
Unity チュートリアルのタワーディフェンステンプレートを触ってみる(28)では Tower ゲームオブジェクトに追加した、SlowAffector の継承関係にある PassiveAffector の解説を行った。今回は Tower ゲームオブジェクトに追加した、SlowAffector の解説を行っていく。
1.その他の効果、ビジュアライザー、挙動編 – SlowAffector.cs
SlowAffector.cs について稚拙ながら解説
「SlowAffector.cs」は「Assets/Scripts/TowerDefense/Affectors/SlowAffector.cs」の指しておりスクリプトについては以下の通り。内容としては Slow の設定削除を行っている。
[cce_csharp]using ActionGameFramework.Health;
using TowerDefense.Agents;
using UnityEngine;
namespace TowerDefense.Affectors
{
/// <summary>
/// Uses a trigger to attach and remove <see cref="AgentSlower" /> components to agents
/// </summary>
public class SlowAffector : PassiveAffector
{
/// <summary>
/// A normalized value to slow agents by
/// </summary>
[Range(0, 1)]
public float slowFactor;
/// <summary>
/// The slow factor for displaying to the UI
/// </summary>
public string slowFactorFormat = "<b>Slow Factor:</b> {0}";
/// <summary>
/// The particle system that plays when an entity enters the sphere
/// </summary>
public ParticleSystem enterParticleSystem;
public GameObject slowFxPrefab;
/// <summary>
/// The audio source that plays when an entity enters the sphere
/// </summary>
public AudioSource audioSource;
/// <summary>
/// Subsribes to the relevant targetter events
/// </summary>
protected void Awake()
{
towerTargetter.targetEntersRange += OnTargetEntersRange;
towerTargetter.targetExitsRange += OnTargetExitsRange;
}
/// <summary>
/// Unsubsribes from the relevant targetter events
/// </summary>
void OnDestroy()
{
towerTargetter.targetEntersRange -= OnTargetEntersRange;
towerTargetter.targetExitsRange -= OnTargetExitsRange;
}
/// <summary>
/// Attaches a <see cref="AgentSlower" /> to the agent
/// </summary>
/// <param name="target">The agent to attach the slower to</param>
protected void AttachSlowComponent(Agent target)
{
var slower = target.GetComponent<AgentSlower>();
if (slower == null)
{
slower = target.gameObject.AddComponent<AgentSlower>();
}
slower.Initialize(slowFactor, slowFxPrefab, target.appliedEffectOffset, target.appliedEffectScale);
if (enterParticleSystem != null)
{
enterParticleSystem.Play();
}
if (audioSource != null)
{
audioSource.Play();
}
}
/// <summary>
/// Removes the <see cref="AgentSlower" /> from the agent once it leaves the area
/// </summary>
/// <param name="target">The agent to remove the slower from</param>
protected void RemoveSlowComponent(Agent target)
{
if (target == null)
{
return;
}
var slowComponent = target.gameObject.GetComponent<AgentSlower>();
if (slowComponent != null)
{
slowComponent.RemoveSlow(slowFactor);
}
}
/// <summary>
/// Fired when the targetter aquires a new targetable
/// </summary>
protected void OnTargetEntersRange(Targetable other)
{
var agent = other as Agent;
if (agent == null)
{
return;
}
AttachSlowComponent(agent);
}
/// <summary>
/// Fired when the targetter aquires loses a targetable
/// </summary>
protected void OnTargetExitsRange(Targetable other)
{
var searchable = other as Agent;
if (searchable == null)
{
return;
}
RemoveSlowComponent(searchable);
}
}
}[cce_csharp]
38行目 : 「protected void Awake()」は Unity 固有の起動処理を行っている。内容としては Targetter に標的が入ってきた時と出ていった時の処理の設定を行っている。
47行目 : 「void OnDestroy()」は Unity 固有の破壊処理を行っている。内容としては Targetter に標的が入ってきた時と出ていった時の処理の解除を行っている。
57行目 : 「protected void AttachSlowComponent(Agent target)」は AgentSlower の追加を行っている。内容としてはAgentSlower の追加後、スローにする処理の ParticleSystem の実行と音声の再生を行っている。
80行目 : 「protected void RemoveSlowComponent(Agent target)」は AgentSlower の削除を行っている。内容としてはAgentSlower の追加されていれば、削除を行っている。
96行目 : 「protected void OnTargetEntersRange(Targetable other)」は 標的が Targetter の内に入ってきた際の処理を行っている。内容としては Targetter の内に入ってきた標的に AgentSlower を設定している。
109行目 : 「protected void OnTargetExitsRange(Targetable other)」は 標的が Targetter から出ていった際の処理を行っている。内容としては Targetter から出ていった標的から AgentSlower を削除している。