-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gable.cs
263 lines (231 loc) · 7.22 KB
/
Gable.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#region copyright
// BuildR 2.0
// Available on the Unity Asset Store https://www.assetstore.unity3d.com/#!/publisher/412
// Copyright (c) 2017 Jasper Stocker http://support.jasperstocker.com
// Support contact email@jasperstocker.com
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
#endregion
using System;
using System.Collections.Generic;
using UnityEngine;
namespace BuildR2
{
public class Gable : ScriptableObject
{
public List<GablePart> _parts = new List<GablePart>();
// [SerializeField]
// private float _thickness = 0.3f;
// [SerializeField]
// private float _additionalHeight = 0.2f;
[SerializeField]
private Surface _surface;
[SerializeField]
private int _segments = 12;
private Mesh _previewMesh;
private BuildRSettings _settings;
public GablePart this[int index]
{
get { return _parts[index]; }
set
{
_parts[index] = value;
if (_parts[index].isModified)
MarkModified();
}
}
public int count { get { return _parts.Count; } }
public GablePart AddNewPart()
{
GablePart output = new GablePart(GablePart.Types.Diagonal);
output.size = Vector2.one;
_parts.Add(output);
MarkModified();
return output;
}
public GablePart InsertNewPart(int index)
{
GablePart output = new GablePart(GablePart.Types.Diagonal);
output.size = Vector2.one;
_parts.Insert(index, output);
MarkModified();
return output;
}
public void RemovePart(GablePart part)
{
_parts.Remove(part);
MarkModified();
}
public void RemovePartAt(int index)
{
_parts.RemoveAt(index);
MarkModified();
}
public Surface surface
{
get { return _surface; }
set
{
if (_surface != value)
{
_surface = value;
MarkModified();
}
}
}
public int segments
{
get { return _segments; }
set
{
if (_segments != value)
{
_segments = value;
MarkModified();
}
}
}
public Mesh previewMesh
{
get
{
if (_previewMesh == null)
UpdatePreviewMesh();
return _previewMesh;
}
}
private void UpdatePreviewMesh()
{
string meshName = string.Format("{0}_Preview_Mesh", name);
BuildRMesh pBMesh = new BuildRMesh(meshName);
if (_previewMesh == null)
_previewMesh = new Mesh();
_previewMesh.name = meshName;
if (_settings == null) _settings = BuildRSettings.GetSettings();
Vector3 left = new Vector3(-_settings.previewGableWidth * 0.5f, -_settings.previewGableHeight * 0.5f, 0);
Vector3 right = new Vector3(_settings.previewGableWidth * 0.5f, -_settings.previewGableHeight * 0.5f, 0);
GableGenerator.Generate(ref pBMesh, this, left, right, _settings.previewGableHeight, _settings.previewGableThickness, new Vector2());
pBMesh.Build(_previewMesh);
}
public void MarkModified()
{
#if UNITY_EDITOR
//tell everyone the good news...
Building[] buildings = FindObjectsOfType<Building>();
foreach (Building building in buildings)
{
UnityEditor.Undo.RecordObject(building, "gable modification");
building.MarkModified();
}
UpdatePreviewMesh();
#endif
SaveData();
}
private void SaveData()
{
#if UNITY_EDITOR
int partCount = _parts.Count;
for (int i = 0; i < partCount; i++)
_parts[i].MarkUnmodified();
UnityEditor.EditorUtility.SetDirty(this);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
#endif
}
#region statics
public static Gable CreateGable()
{
Gable output = CreateInstance<Gable>();
#if UNITY_EDITOR
UnityEditor.AssetDatabase.CreateAsset(output, AssetCreator.GeneratePath("newGable.asset", "Gables"));
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
#endif
return output;
}
#if UNITY_EDITOR
[UnityEditor.MenuItem("Tools/BuildR/Create New Gable Design", false, 60)]
private static Gable MenuCreateNewGable()
{
Gable output = CreateGable();
UnityEditor.Selection.activeObject = output;
return output;
}
[UnityEditor.MenuItem("Assets/Create/BuildR/Create New Gable Design", false, 60)]
private static Gable MenuCreateNewGableB()
{
Gable output = CreateGable();
UnityEditor.Selection.activeObject = output;
return output;
}
#endif
#endregion
}
[Serializable]
public class GablePart
{
public enum Types
{
Horizonal,
Vertical,
Diagonal,
Concave,
Convex
}
public Types _type;
public Vector2 _size;
public bool _modified;
public GablePart(Types type = Types.Diagonal)
{
_type = type;
_size = Vector2.one;
_modified = false;
}
public Types type
{
get { return _type; }
set
{
if (_type != value)
{
_type = value;
_modified = true;
}
}
}
public Vector2 size
{
get { return _size; }
set
{
if (_size != value)
{
_size = value;
_modified = true;
}
}
}
public Vector2 GetSize()
{
Vector2 output = _size;
if (_type == Types.Horizonal) output.y = 0;
if (_type == Types.Vertical) output.x = 0;
return output;
}
public bool isModified
{
get { return _modified; }
}
public void MarkModified()
{
_modified = true;
}
public void MarkUnmodified()
{
_modified = false;
}
}
}