-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathNetworkTransformEditor.cs
More file actions
319 lines (280 loc) · 15.8 KB
/
NetworkTransformEditor.cs
File metadata and controls
319 lines (280 loc) · 15.8 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using Unity.Netcode.Components;
using UnityEditor;
using UnityEngine;
namespace Unity.Netcode.Editor
{
/// <summary>
/// The <see cref="CustomEditor"/> for <see cref="NetworkTransform"/>
/// </summary>
[CustomEditor(typeof(NetworkTransform), true)]
[CanEditMultipleObjects]
public class NetworkTransformEditor : NetcodeEditorBase<NetworkTransform>
{
private SerializedProperty m_SwitchTransformSpaceWhenParented;
private SerializedProperty m_TickSyncChildren;
private SerializedProperty m_UseUnreliableDeltas;
private SerializedProperty m_SyncPositionXProperty;
private SerializedProperty m_SyncPositionYProperty;
private SerializedProperty m_SyncPositionZProperty;
private SerializedProperty m_SyncRotationXProperty;
private SerializedProperty m_SyncRotationYProperty;
private SerializedProperty m_SyncRotationZProperty;
private SerializedProperty m_SyncScaleXProperty;
private SerializedProperty m_SyncScaleYProperty;
private SerializedProperty m_SyncScaleZProperty;
private SerializedProperty m_PositionThresholdProperty;
private SerializedProperty m_RotAngleThresholdProperty;
private SerializedProperty m_ScaleThresholdProperty;
private SerializedProperty m_InLocalSpaceProperty;
private SerializedProperty m_InterpolateProperty;
private SerializedProperty m_PositionInterpolationTypeProperty;
private SerializedProperty m_RotationInterpolationTypeProperty;
private SerializedProperty m_ScaleInterpolationTypeProperty;
private SerializedProperty m_PositionLerpSmoothing;
private SerializedProperty m_RotationLerpSmoothing;
private SerializedProperty m_ScaleLerpSmoothing;
private SerializedProperty m_PositionMaximumInterpolationTimeProperty;
private SerializedProperty m_RotationMaximumInterpolationTimeProperty;
private SerializedProperty m_ScaleMaximumInterpolationTimeProperty;
private SerializedProperty m_UseQuaternionSynchronization;
private SerializedProperty m_UseQuaternionCompression;
private SerializedProperty m_UseHalfFloatPrecision;
private SerializedProperty m_SlerpPosition;
private SerializedProperty m_AuthorityMode;
private static int s_ToggleOffset = 45;
private static float s_MaxRowWidth = EditorGUIUtility.labelWidth + EditorGUIUtility.fieldWidth + 5;
private static GUIContent s_PositionLabel = EditorGUIUtility.TrTextContent("Position");
private static GUIContent s_RotationLabel = EditorGUIUtility.TrTextContent("Rotation");
private static GUIContent s_ScaleLabel = EditorGUIUtility.TrTextContent("Scale");
/// <inheritdoc/>
public override void OnEnable()
{
m_SwitchTransformSpaceWhenParented = serializedObject.FindProperty(nameof(NetworkTransform.SwitchTransformSpaceWhenParented));
m_TickSyncChildren = serializedObject.FindProperty(nameof(NetworkTransform.TickSyncChildren));
m_UseUnreliableDeltas = serializedObject.FindProperty(nameof(NetworkTransform.UseUnreliableDeltas));
m_SyncPositionXProperty = serializedObject.FindProperty(nameof(NetworkTransform.SyncPositionX));
m_SyncPositionYProperty = serializedObject.FindProperty(nameof(NetworkTransform.SyncPositionY));
m_SyncPositionZProperty = serializedObject.FindProperty(nameof(NetworkTransform.SyncPositionZ));
m_SyncRotationXProperty = serializedObject.FindProperty(nameof(NetworkTransform.SyncRotAngleX));
m_SyncRotationYProperty = serializedObject.FindProperty(nameof(NetworkTransform.SyncRotAngleY));
m_SyncRotationZProperty = serializedObject.FindProperty(nameof(NetworkTransform.SyncRotAngleZ));
m_SyncScaleXProperty = serializedObject.FindProperty(nameof(NetworkTransform.SyncScaleX));
m_SyncScaleYProperty = serializedObject.FindProperty(nameof(NetworkTransform.SyncScaleY));
m_SyncScaleZProperty = serializedObject.FindProperty(nameof(NetworkTransform.SyncScaleZ));
m_PositionThresholdProperty = serializedObject.FindProperty(nameof(NetworkTransform.PositionThreshold));
m_RotAngleThresholdProperty = serializedObject.FindProperty(nameof(NetworkTransform.RotAngleThreshold));
m_ScaleThresholdProperty = serializedObject.FindProperty(nameof(NetworkTransform.ScaleThreshold));
m_InLocalSpaceProperty = serializedObject.FindProperty(nameof(NetworkTransform.InLocalSpace));
m_InterpolateProperty = serializedObject.FindProperty(nameof(NetworkTransform.Interpolate));
m_PositionInterpolationTypeProperty = serializedObject.FindProperty(nameof(NetworkTransform.PositionInterpolationType));
m_PositionMaximumInterpolationTimeProperty = serializedObject.FindProperty(nameof(NetworkTransform.PositionMaxInterpolationTime));
m_RotationInterpolationTypeProperty = serializedObject.FindProperty(nameof(NetworkTransform.RotationInterpolationType));
m_RotationMaximumInterpolationTimeProperty = serializedObject.FindProperty(nameof(NetworkTransform.RotationMaxInterpolationTime));
m_ScaleInterpolationTypeProperty = serializedObject.FindProperty(nameof(NetworkTransform.ScaleInterpolationType));
m_ScaleMaximumInterpolationTimeProperty = serializedObject.FindProperty(nameof(NetworkTransform.ScaleMaxInterpolationTime));
m_PositionLerpSmoothing = serializedObject.FindProperty(nameof(NetworkTransform.PositionLerpSmoothing));
m_RotationLerpSmoothing = serializedObject.FindProperty(nameof(NetworkTransform.RotationLerpSmoothing));
m_ScaleLerpSmoothing = serializedObject.FindProperty(nameof(NetworkTransform.ScaleLerpSmoothing));
m_UseQuaternionSynchronization = serializedObject.FindProperty(nameof(NetworkTransform.UseQuaternionSynchronization));
m_UseQuaternionCompression = serializedObject.FindProperty(nameof(NetworkTransform.UseQuaternionCompression));
m_UseHalfFloatPrecision = serializedObject.FindProperty(nameof(NetworkTransform.UseHalfFloatPrecision));
m_SlerpPosition = serializedObject.FindProperty(nameof(NetworkTransform.SlerpPosition));
m_AuthorityMode = serializedObject.FindProperty(nameof(NetworkTransform.AuthorityMode));
base.OnEnable();
}
private void DisplayNetworkTransformProperties()
{
var networkTransform = target as NetworkTransform;
EditorGUILayout.LabelField("Axis to Synchronize", EditorStyles.boldLabel);
{
GUILayout.BeginHorizontal();
var rect = GUILayoutUtility.GetRect(EditorGUIUtility.fieldWidth, s_MaxRowWidth, EditorGUIUtility.singleLineHeight, EditorGUIUtility.singleLineHeight, EditorStyles.numberField);
var ctid = GUIUtility.GetControlID(7231, FocusType.Keyboard, rect);
rect = EditorGUI.PrefixLabel(rect, ctid, s_PositionLabel);
rect.width = s_ToggleOffset;
DrawToggleProperty(rect, "X", m_SyncPositionXProperty);
rect.x += s_ToggleOffset;
DrawToggleProperty(rect, "Y", m_SyncPositionYProperty);
rect.x += s_ToggleOffset;
DrawToggleProperty(rect, "Z", m_SyncPositionZProperty);
GUILayout.EndHorizontal();
}
if (!m_UseQuaternionSynchronization.boolValue)
{
GUILayout.BeginHorizontal();
var rect = GUILayoutUtility.GetRect(EditorGUIUtility.fieldWidth, s_MaxRowWidth, EditorGUIUtility.singleLineHeight, EditorGUIUtility.singleLineHeight, EditorStyles.numberField);
var ctid = GUIUtility.GetControlID(7231, FocusType.Keyboard, rect);
rect = EditorGUI.PrefixLabel(rect, ctid, s_RotationLabel);
rect.width = s_ToggleOffset;
DrawToggleProperty(rect, "X", m_SyncRotationXProperty);
rect.x += s_ToggleOffset;
DrawToggleProperty(rect, "Y", m_SyncRotationYProperty);
rect.x += s_ToggleOffset;
DrawToggleProperty(rect, "Z", m_SyncRotationZProperty);
GUILayout.EndHorizontal();
}
else
{
m_SyncRotationXProperty.boolValue = true;
m_SyncRotationYProperty.boolValue = true;
m_SyncRotationZProperty.boolValue = true;
}
{
GUILayout.BeginHorizontal();
var rect = GUILayoutUtility.GetRect(EditorGUIUtility.fieldWidth, s_MaxRowWidth, EditorGUIUtility.singleLineHeight, EditorGUIUtility.singleLineHeight, EditorStyles.numberField);
var ctid = GUIUtility.GetControlID(7231, FocusType.Keyboard, rect);
rect = EditorGUI.PrefixLabel(rect, ctid, s_ScaleLabel);
rect.width = s_ToggleOffset;
DrawToggleProperty(rect, "X", m_SyncScaleXProperty);
rect.x += s_ToggleOffset;
DrawToggleProperty(rect, "Y", m_SyncScaleYProperty);
rect.x += s_ToggleOffset;
DrawToggleProperty(rect, "Z", m_SyncScaleZProperty);
GUILayout.EndHorizontal();
}
EditorGUILayout.Space();
EditorGUILayout.LabelField("Authority", EditorStyles.boldLabel);
{
EditorGUILayout.PropertyField(m_AuthorityMode);
}
EditorGUILayout.Space();
EditorGUILayout.LabelField("Thresholds", EditorStyles.boldLabel);
if (networkTransform.SynchronizePosition)
{
EditorGUILayout.PropertyField(m_PositionThresholdProperty);
}
if (networkTransform.SynchronizeRotation)
{
EditorGUILayout.PropertyField(m_RotAngleThresholdProperty);
}
if (networkTransform.SynchronizeScale)
{
EditorGUILayout.PropertyField(m_ScaleThresholdProperty);
}
EditorGUILayout.Space();
EditorGUILayout.LabelField("Delivery", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(m_TickSyncChildren);
EditorGUILayout.PropertyField(m_UseUnreliableDeltas);
EditorGUILayout.Space();
EditorGUILayout.LabelField("Configurations", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(m_SwitchTransformSpaceWhenParented);
if (m_SwitchTransformSpaceWhenParented.boolValue)
{
m_TickSyncChildren.boolValue = true;
}
else
{
// Should only be visible when SwitchTransformSpaceWhenParented is disabled.
EditorGUILayout.PropertyField(m_InLocalSpaceProperty);
}
if (!networkTransform.HideInterpolateValue)
{
if (networkTransform.Interpolate)
{
EditorGUILayout.Space();
}
DrawPropertyField(m_InterpolateProperty, networkTransform.Interpolate ? FontStyle.Bold : FontStyle.Normal);
if (networkTransform.Interpolate)
{
BeginIndent();
if (networkTransform.SynchronizePosition)
{
DrawPropertyField(m_PositionInterpolationTypeProperty);
BeginIndent();
if (networkTransform.PositionInterpolationType != NetworkTransform.InterpolationTypes.SmoothDampening)
{
DrawPropertyField(m_SlerpPosition);
}
DrawPropertyField(m_PositionLerpSmoothing);
if (networkTransform.PositionLerpSmoothing)
{
DrawPropertyField(m_PositionMaximumInterpolationTimeProperty);
}
EndIndent();
}
if (networkTransform.SynchronizeRotation)
{
DrawPropertyField(m_RotationInterpolationTypeProperty);
BeginIndent();
DrawPropertyField(m_RotationLerpSmoothing);
if (networkTransform.RotationLerpSmoothing)
{
DrawPropertyField(m_RotationMaximumInterpolationTimeProperty);
}
EndIndent();
}
if (networkTransform.SynchronizeScale)
{
DrawPropertyField(m_ScaleInterpolationTypeProperty);
BeginIndent();
DrawPropertyField(m_ScaleLerpSmoothing);
if (networkTransform.ScaleLerpSmoothing)
{
DrawPropertyField(m_ScaleMaximumInterpolationTimeProperty);
}
EndIndent();
}
EndIndent();
EditorGUILayout.Space();
}
}
EditorGUILayout.PropertyField(m_UseQuaternionSynchronization);
if (m_UseQuaternionSynchronization.boolValue)
{
EditorGUILayout.PropertyField(m_UseQuaternionCompression);
}
else
{
m_UseQuaternionCompression.boolValue = false;
}
EditorGUILayout.PropertyField(m_UseHalfFloatPrecision);
#if COM_UNITY_MODULES_PHYSICS
// if rigidbody is present but network rigidbody is not present
if (networkTransform.TryGetComponent<Rigidbody>(out _) && networkTransform.TryGetComponent<NetworkRigidbody>(out _) == false)
{
EditorGUILayout.HelpBox("This GameObject contains a Rigidbody but no NetworkRigidbody.\n" +
"Add a NetworkRigidbody component to improve Rigidbody synchronization.", MessageType.Warning);
}
#endif // COM_UNITY_MODULES_PHYSICS
#if COM_UNITY_MODULES_PHYSICS2D
if (networkTransform.TryGetComponent<Rigidbody2D>(out _) && networkTransform.TryGetComponent<NetworkRigidbody2D>(out _) == false)
{
EditorGUILayout.HelpBox("This GameObject contains a Rigidbody2D but no NetworkRigidbody2D.\n" +
"Add a NetworkRigidbody2D component to improve Rigidbody2D synchronization.", MessageType.Warning);
}
#endif // COM_UNITY_MODULES_PHYSICS2D
}
/// <summary>
/// Draw a ToggleLeft property field so it will support multi selection editing if applicable.
/// </summary>
private void DrawToggleProperty(Rect rect, string label, SerializedProperty property)
{
if (property.hasMultipleDifferentValues)
{
EditorGUI.showMixedValue = true;
EditorGUI.BeginChangeCheck();
bool enabled = EditorGUI.ToggleLeft(rect, label, property.boolValue);
if (EditorGUI.EndChangeCheck())
{
property.boolValue = enabled;
}
EditorGUI.showMixedValue = false;
}
else
{
property.boolValue = EditorGUI.ToggleLeft(rect, label, property.boolValue);
}
}
/// <inheritdoc/>
public override void OnInspectorGUI()
{
var networkTransform = target as NetworkTransform;
void SetExpanded(bool expanded)
{
networkTransform.NetworkTransformExpanded = expanded;
}
DrawFoldOutGroup<NetworkTransform>(networkTransform.GetType(), DisplayNetworkTransformProperties, networkTransform.NetworkTransformExpanded, SetExpanded);
base.OnInspectorGUI();
}
}
}