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

Unity チュートリアルのタワーディフェンステンプレートを触ってみる(34)では NavigationNodes に追加した Node について解説を行った。今回は NavigationNodes に追加した FixedNodeSelector の継承関係にある NodeSelector について解説を行っていく。
1.タワーディフェンステンプレートのステージの設定編 – NodeSelector.cs
NodeSelector.cs について稚拙ながら解説
「NodeSelector.cs」は「Assets/Scripts/TowerDefense/Nodes/NodeSelector.cs」の指しておりスクリプトについては以下の通り。内容としては GetNextNode 関数の定義と Node のリストの保持を行っている。
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 using System.Collections.Generic;
using UnityEngine;
namespace TowerDefense.Nodes
{
/// <summary>
/// Provides a way to select a node for agents to navigate towards
/// </summary>
public abstract class NodeSelector : MonoBehaviour
{
/// <summary>
/// A list of Nodes that can be selected by this NodeSelector
/// </summary>
public List<Node> linkedNodes;
/// <summary>
/// Gets the next node in the fixed list of nodes
/// </summary>
/// <returns>The next node in the list of Nodes, null if the node is the endpoint</returns>
public abstract Node GetNextNode();
#if UNITY_EDITOR
/// <summary>
/// Draws the links between nodes for editor purposes
/// </summary>
protected virtual void OnDrawGizmos()
{
if (linkedNodes == null)
{
return;
}
int count = linkedNodes.Count;
for (int i = 0; i < count; i++)
{
Node node = linkedNodes[i];
if (node != null)
{
Gizmos.DrawLine(transform.position, node.transform.position);
}
}
}
#endif
}
}
20行目 : 「public abstract Node GetNextNode();」は具象クラスで GetNextNode 関数を作成する必要があることを指している。
その他については Gizmo の表示や Node リストの保持を行っている。