Repository navigation
Expand file tree
/
Copy pathTags.cs
More file actions
232 lines (199 loc) · 5.93 KB
/
Copy pathTags.cs
File metadata and controls
232 lines (199 loc) · 5.93 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace TouchScript
{
/// <summary>
/// Immutable tag collection for touches.
/// </summary>
[Serializable]
public sealed class Tags : ISerializationCallbackReceiver
{
#region Constants
/// <summary>
/// Touch.
/// </summary>
public const string INPUT_TOUCH = "Touch";
/// <summary>
/// Mouse.
/// </summary>
public const string INPUT_MOUSE = "Mouse";
/// <summary>
/// Pen.
/// </summary>
public const string INPUT_PEN = "Pen";
/// <summary>
/// Object.
/// </summary>
public const string INPUT_OBJECT = "Object";
/// <summary>
/// Empty tag list.
/// </summary>
public static readonly Tags EMPTY = new Tags();
#endregion
#region Public properties
#endregion
#region Private variables
[SerializeField]
private List<string> tagList = new List<string>();
private HashSet<string> tags = new HashSet<string>();
private string stringValue;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Tags"/> class.
/// </summary>
/// <param name="tags"> Tags to copy. </param>
/// <param name="add"> Tags to add. </param>
public Tags(Tags tags, IEnumerable<string> add) : this(tags)
{
if (add == null) return;
foreach (var tag in add)
{
if (string.IsNullOrEmpty(tag)) continue;
this.tags.Add(tag);
}
#if UNITY_EDITOR
syncTagList();
#endif
}
/// <summary>
/// Initializes a new instance of the <see cref="Tags"/> class.
/// </summary>
/// <param name="tags"> Tags to copy. </param>
/// <param name="add"> Tag to add. </param>
public Tags(Tags tags, string add) : this(tags)
{
if (string.IsNullOrEmpty(add)) return;
this.tags.Add(add);
}
/// <summary>
/// Initializes a new instance of the <see cref="Tags"/> class.
/// </summary>
/// <param name="tags"> Tags to copy. </param>
public Tags(Tags tags) : this()
{
if (tags == null) return;
foreach (var tag in tags.tags) this.tags.Add(tag);
#if UNITY_EDITOR
syncTagList();
#endif
}
/// <summary>
/// Initializes a new instance of the <see cref="Tags"/> class.
/// </summary>
/// <param name="tags"> Tags to copy. </param>
public Tags(IEnumerable<string> tags) : this()
{
if (tags == null) return;
foreach (var tag in tags)
{
if (string.IsNullOrEmpty(tag)) continue;
this.tags.Add(tag);
}
#if UNITY_EDITOR
syncTagList();
#endif
}
/// <summary>
/// Initializes a new instance of the <see cref="Tags"/> class.
/// </summary>
/// <param name="tags"> Tags to add. </param>
public Tags(params string[] tags) : this()
{
if (tags == null) return;
var count = tags.Length;
for (var i = 0; i < count; i++)
{
var tag = tags[i];
if (string.IsNullOrEmpty(tag)) continue;
this.tags.Add(tag);
}
#if UNITY_EDITOR
syncTagList();
#endif
}
/// <summary>
/// Initializes a new instance of the <see cref="Tags"/> class.
/// </summary>
/// <param name="tag"> Tag to add. </param>
public Tags(string tag) : this()
{
if (string.IsNullOrEmpty(tag)) return;
tags.Add(tag);
#if UNITY_EDITOR
syncTagList();
#endif
}
/// <summary>
/// Initializes a new instance of the <see cref="Tags"/> class.
/// </summary>
public Tags() {}
#endregion
#region Public methods
/// <summary>
/// Checks if this collection contains a tag.
/// </summary>
/// <param name="tag">Tag.</param>
/// <returns>True if tag is in this collection; false otherwise.</returns>
public bool HasTag(string tag)
{
return tags.Contains(tag);
}
/// <exclude />
public void OnBeforeSerialize()
{
#if !UNITY_EDITOR
tagList.Clear();
tagList.AddRange(tags);
#endif
}
/// <exclude />
public void OnAfterDeserialize()
{
tags.Clear();
foreach (var tag in tagList) tags.Add(tag);
}
/// <inheritdoc />
public override string ToString()
{
if (stringValue == null)
{
if (tags.Count == 0)
{
stringValue = "";
}
else if (tags.Count == 1)
{
foreach (var tag in tags) stringValue = tag; // doh!?
}
else
{
var sb = new StringBuilder(100);
foreach (var tag in tags)
{
sb.Append(tag);
sb.Append(", ");
}
stringValue = sb.ToString(0, sb.Length - 2);
}
}
return stringValue;
}
#endregion
#region Private functions
#if UNITY_EDITOR
// When Tags is created in editor as a component's property need to copy all tags to tagList so Unity could serialize them.
private void syncTagList()
{
tagList.Clear();
tagList.AddRange(tags);
}
#endif
#endregion
}
}