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

Unity チュートリアルのタワーディフェンステンプレートを触ってみる(41)では本拠地の設定に必要な PlayerHomeBase の解説を行っていく。今回はそのステージで使用できるタワーを設定するのに用いる TowerLibrary の解説を行っていく。
1.タワーディフェンステンプレートのステージの設定編 – TowerLibrary.cs
TowerLibrary.cs について稚拙ながら解説
「TowerLibrary.cs」は「Assets/Scripts/TowerDefense/Towers/Data/TowerLibrary.cs」の指しておりスクリプトについては以下の通り。内容としてはタワーのデータ管理を行っている。
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace TowerDefense.Towers.Data
{
/// <summary>
/// The asset which holds the list of different towers
/// </summary>
[CreateAssetMenu(fileName = "TowerLibrary.asset", menuName = "TowerDefense/Tower Library", order = 1)]
public class TowerLibrary : ScriptableObject, IList<Tower>, IDictionary<string, Tower>
{
/// <summary>
/// The list of all the towers
/// </summary>
public List<Tower> configurations;
/// <summary>
/// The internal reference to the dictionary made from the list of towers
/// with the name of tower as the key
/// </summary>
Dictionary<string, Tower> m_ConfigurationDictionary;
/// <summary>
/// The accessor to the towers by index
/// </summary>
/// <param name="index"></param>
public Tower this[int index]
{
get { return configurations[index]; }
}
public void OnBeforeSerialize()
{
}
/// <summary>
/// Convert the list (m_Configurations) to a dictionary for access via name
/// </summary>
public void OnAfterDeserialize()
{
if (configurations == null)
{
return;
}
m_ConfigurationDictionary = configurations.ToDictionary(t => t.towerName);
}
public bool ContainsKey(string key)
{
return m_ConfigurationDictionary.ContainsKey(key);
}
public void Add(string key, Tower value)
{
m_ConfigurationDictionary.Add(key, value);
}
public bool Remove(string key)
{
return m_ConfigurationDictionary.Remove(key);
}
public bool TryGetValue(string key, out Tower value)
{
return m_ConfigurationDictionary.TryGetValue(key, out value);
}
Tower IDictionary<string, Tower>.this[string key]
{
get { return m_ConfigurationDictionary[key]; }
set { m_ConfigurationDictionary[key] = value; }
}
public ICollection<string> Keys
{
get { return ((IDictionary<string, Tower>) m_ConfigurationDictionary).Keys; }
}
ICollection<Tower> IDictionary<string, Tower>.Values
{
get { return m_ConfigurationDictionary.Values; }
}
IEnumerator<KeyValuePair<string, Tower>> IEnumerable<KeyValuePair<string, Tower>>.GetEnumerator()
{
return m_ConfigurationDictionary.GetEnumerator();
}
public void Add(KeyValuePair<string, Tower> item)
{
m_ConfigurationDictionary.Add(item.Key, item.Value);
}
public bool Remove(KeyValuePair<string, Tower> item)
{
return m_ConfigurationDictionary.Remove(item.Key);
}
public bool Contains(KeyValuePair<string, Tower> item)
{
return m_ConfigurationDictionary.Contains(item);
}
public void CopyTo(KeyValuePair<string, Tower>[] array, int arrayIndex)
{
int count = array.Length;
for (int i = arrayIndex; i < count; i++)
{
Tower config = configurations[i - arrayIndex];
KeyValuePair<string, Tower> current = new KeyValuePair<string, Tower>(config.towerName, config);
array[i] = current;
}
}
public int IndexOf(Tower item)
{
return configurations.IndexOf(item);
}
public void Insert(int index, Tower item)
{
configurations.Insert(index, item);
}
public void RemoveAt(int index)
{
configurations.RemoveAt(index);
}
Tower IList<Tower>.this[int index]
{
get { return configurations[index]; }
set { configurations[index] = value; }
}
public IEnumerator<Tower> GetEnumerator()
{
return configurations.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) configurations).GetEnumerator();
}
public void Add(Tower item)
{
configurations.Add(item);
}
public void Clear()
{
configurations.Clear();
}
public bool Contains(Tower item)
{
return configurations.Contains(item);
}
public void CopyTo(Tower[] array, int arrayIndex)
{
configurations.CopyTo(array, arrayIndex);
}
public bool Remove(Tower item)
{
return configurations.Remove(item);
}
public int Count
{
get { return configurations.Count; }
}
public bool IsReadOnly
{
get { return ((ICollection<Tower>) configurations).IsReadOnly; }
}
}
}
上記の処理は、登場するタワーデータをリストとして管理している。詳細については DLL の処理を用いているため、ここでは解説を行うことができない。よってここではソースコードの提示のみとさせていただく。