Skip to content

Commit 4168bca

Browse files
update
Reverted the change in HiddenScriptEditor. Moved away from a NetworkAnimator custom editor to a custom property drawer to handle displaying the Animator parameters to be synchronized. Handling the population of parameters and populating the NetworkAnimator.AnimatorParameterEntries during OnValidate. Had to re-apply unchecking the excluded parameter in some prefabs for testing purposes.
1 parent c59ab12 commit 4168bca

File tree

8 files changed

+394
-203
lines changed

8 files changed

+394
-203
lines changed

com.unity.netcode.gameobjects/Editor/HiddenScriptEditor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ public override void OnInspectorGUI()
155155
/// <summary>
156156
/// Internal use. Hides the script field for NetworkAnimator.
157157
/// </summary>
158-
//[CustomEditor(typeof(NetworkAnimator), true)]
159-
//public class NetworkAnimatorEditor : HiddenScriptEditor
160-
//{
158+
[CustomEditor(typeof(NetworkAnimator), true)]
159+
public class NetworkAnimatorEditor : HiddenScriptEditor
160+
{
161161

162-
//}
162+
}
163163
#endif
164164

165165
#if COM_UNITY_MODULES_PHYSICS

com.unity.netcode.gameobjects/Editor/NetworkAnimatorEditor.cs

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
using Unity.Netcode.Components;
3+
using UnityEditor;
4+
//using UnityEditor.UIElements;
5+
//using UnityEngine.UIElements;
6+
using UnityEngine;
7+
8+
namespace Unity.Netcode.Editor
9+
{
10+
[CustomPropertyDrawer(typeof(NetworkAnimator.AnimatorParametersListContainer))]
11+
internal class NetworkAnimatorParameterEntryEditor : PropertyDrawer
12+
{
13+
// Draw the property inside the given rect
14+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
15+
{
16+
EditorGUI.BeginProperty(position, label, property);
17+
18+
// Draw the foldout for the list
19+
SerializedProperty items = property.FindPropertyRelative(nameof(NetworkAnimator.AnimatorParameterEntries.ParameterEntries));
20+
position.height = EditorGUIUtility.singleLineHeight;
21+
property.isExpanded = EditorGUI.Foldout(position, property.isExpanded, label);
22+
23+
if (property.isExpanded)
24+
{
25+
// Set the indention level down
26+
EditorGUI.indentLevel++;
27+
for (int i = 0; i < items.arraySize; i++)
28+
{
29+
position.y += EditorGUIUtility.singleLineHeight + 2;
30+
SerializedProperty element = items.GetArrayElementAtIndex(i);
31+
var nameField = element.FindPropertyRelative(nameof(NetworkAnimator.AnimatorParameterEntry.name));
32+
// Draw the foldout for the item
33+
element.isExpanded = EditorGUI.Foldout(position, element.isExpanded, nameField.stringValue);
34+
if (!element.isExpanded)
35+
{
36+
continue;
37+
}
38+
// Draw the contents of the item
39+
position.y += EditorGUIUtility.singleLineHeight + 2;
40+
// Set the indention level down
41+
EditorGUI.indentLevel++;
42+
// Calculate rects
43+
var nameHashRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
44+
position.y += EditorGUIUtility.singleLineHeight + 2;
45+
var paramRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
46+
position.y += EditorGUIUtility.singleLineHeight + 2;
47+
var syncRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
48+
49+
// Get the three properties we want to visualize in the inspector view
50+
var synchronizeField = element.FindPropertyRelative(nameof(NetworkAnimator.AnimatorParameterEntry.Synchronize));
51+
var nameHashField = element.FindPropertyRelative(nameof(NetworkAnimator.AnimatorParameterEntry.NameHash));
52+
var parameterTypeField = element.FindPropertyRelative(nameof(NetworkAnimator.AnimatorParameterEntry.ParameterType));
53+
54+
// Draw the read only fields
55+
GUI.enabled = false;
56+
EditorGUI.PropertyField(nameHashRect, nameHashField);
57+
EditorGUI.PropertyField(paramRect, parameterTypeField);
58+
GUI.enabled = true;
59+
// Draw the read/write fields
60+
EditorGUI.PropertyField(syncRect, synchronizeField);
61+
// Set the indention level up
62+
EditorGUI.indentLevel--;
63+
}
64+
// Set the indention level up
65+
EditorGUI.indentLevel--;
66+
}
67+
EditorGUI.EndProperty();
68+
}
69+
70+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
71+
{
72+
var totalHeight = EditorGUIUtility.singleLineHeight;
73+
if (!property.isExpanded)
74+
{
75+
return totalHeight;
76+
}
77+
var singleLineWithSpace = EditorGUIUtility.singleLineHeight + 2;
78+
SerializedProperty items = property.FindPropertyRelative(nameof(NetworkAnimator.AnimatorParameterEntries.ParameterEntries));
79+
80+
totalHeight += singleLineWithSpace;
81+
for (int i = 0; i < items.arraySize; i++)
82+
{
83+
SerializedProperty element = items.GetArrayElementAtIndex(i);
84+
if (element.isExpanded)
85+
{
86+
totalHeight += (singleLineWithSpace * 4);
87+
}
88+
else
89+
{
90+
totalHeight += EditorGUIUtility.singleLineHeight;
91+
}
92+
}
93+
return totalHeight;
94+
}
95+
}
96+
}
97+

com.unity.netcode.gameobjects/Runtime/Components/NetworkAnimator.cs

Lines changed: 100 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,21 @@ public enum AuthorityModes
249249
#endif
250250
public AuthorityModes AuthorityMode;
251251

252+
[Tooltip("The animator that this NetworkAnimator component will be synchronizing.")]
253+
[SerializeField] private Animator m_Animator;
254+
255+
/// <summary>
256+
/// The <see cref="Animator"/> associated with this <see cref="NetworkAnimator"/> instance.
257+
/// </summary>
258+
public Animator Animator
259+
{
260+
get { return m_Animator; }
261+
set
262+
{
263+
m_Animator = value;
264+
}
265+
}
266+
252267
/// <summary>
253268
/// Used to build the destination state to transition info table
254269
/// </summary>
@@ -282,31 +297,33 @@ private void BuildDestinationToTransitionInfoTable()
282297
[Serializable]
283298
internal class AnimatorParameterEntry
284299
{
285-
public bool Synchronize;
300+
#pragma warning disable IDE1006
301+
[HideInInspector]
302+
public string name;
303+
#pragma warning restore IDE1006
286304
public int NameHash;
287-
public string Name;
305+
public bool Synchronize;
288306
public AnimatorControllerParameterType ParameterType;
289-
#if UNITY_EDITOR
290-
public AnimatorParameterEntry(AnimatorControllerParameter parameter)
291-
{
292-
Synchronize = true;
293-
NameHash = parameter.nameHash;
294-
Name = parameter.name;
295-
ParameterType = parameter.type;
296-
}
297-
#endif
298-
public AnimatorParameterEntry() { }
299307
}
300308

301-
[HideInInspector]
309+
[Serializable]
310+
internal class AnimatorParametersListContainer
311+
{
312+
public List<AnimatorParameterEntry> ParameterEntries = new List<AnimatorParameterEntry>();
313+
}
314+
302315
[SerializeField]
303-
internal List<AnimatorParameterEntry> AnimatorParameterEntries;
316+
internal AnimatorParametersListContainer AnimatorParameterEntries;
304317

305318
internal Dictionary<int, AnimatorParameterEntry> AnimatorParameterEntryTable = new Dictionary<int, AnimatorParameterEntry>();
306319

307320
#if UNITY_EDITOR
321+
[HideInInspector]
322+
[SerializeField]
308323
internal bool AnimatorParametersExpanded;
309324

325+
internal Dictionary<int, AnimatorControllerParameter> ParameterToNameLookup = new Dictionary<int, AnimatorControllerParameter>();
326+
310327
private void ParseStateMachineStates(int layerIndex, ref AnimatorController animatorController, ref AnimatorStateMachine stateMachine)
311328
{
312329
for (int y = 0; y < stateMachine.states.Length; y++)
@@ -397,13 +414,81 @@ private void BuildTransitionStateInfoList()
397414
}
398415
}
399416

417+
internal void ProcessParameterEntries()
418+
{
419+
if (!Animator)
420+
{
421+
if (AnimatorParameterEntries != null && AnimatorParameterEntries.ParameterEntries.Count > 0)
422+
{
423+
AnimatorParameterEntries.ParameterEntries.Clear();
424+
}
425+
return;
426+
}
427+
428+
var parameters = Animator.parameters;
429+
430+
var parametersToRemove = new List<AnimatorParameterEntry>();
431+
ParameterToNameLookup.Clear();
432+
foreach (var parameter in parameters)
433+
{
434+
ParameterToNameLookup.Add(parameter.nameHash, parameter);
435+
}
436+
437+
// Rebuild the parameter entry table for the inspector view
438+
AnimatorParameterEntryTable.Clear();
439+
foreach (var parameterEntry in AnimatorParameterEntries.ParameterEntries)
440+
{
441+
// Check for removed parameters.
442+
if (!ParameterToNameLookup.ContainsKey(parameterEntry.NameHash))
443+
{
444+
parametersToRemove.Add(parameterEntry);
445+
// Skip this removed entry
446+
continue;
447+
}
448+
449+
// Build the list of known parameters
450+
if (!AnimatorParameterEntryTable.ContainsKey(parameterEntry.NameHash))
451+
{
452+
AnimatorParameterEntryTable.Add(parameterEntry.NameHash, parameterEntry);
453+
}
454+
455+
var parameter = ParameterToNameLookup[parameterEntry.NameHash];
456+
parameterEntry.name = parameter.name;
457+
parameterEntry.ParameterType = parameter.type;
458+
}
459+
460+
// Update for removed parameters
461+
foreach (var parameterEntry in parametersToRemove)
462+
{
463+
AnimatorParameterEntries.ParameterEntries.Remove(parameterEntry);
464+
}
465+
466+
// Update any newly added parameters
467+
foreach (var parameterLookUp in ParameterToNameLookup)
468+
{
469+
if (!AnimatorParameterEntryTable.ContainsKey(parameterLookUp.Value.nameHash))
470+
{
471+
var animatorParameterEntry = new AnimatorParameterEntry()
472+
{
473+
name = parameterLookUp.Value.name,
474+
NameHash = parameterLookUp.Value.nameHash,
475+
ParameterType = parameterLookUp.Value.type,
476+
Synchronize = true,
477+
};
478+
AnimatorParameterEntries.ParameterEntries.Add(animatorParameterEntry);
479+
AnimatorParameterEntryTable.Add(parameterLookUp.Value.nameHash, animatorParameterEntry);
480+
}
481+
}
482+
}
483+
400484
/// <summary>
401485
/// In-Editor Only
402486
/// Virtual OnValidate method for custom derived NetworkAnimator classes.
403487
/// </summary>
404488
protected virtual void OnValidate()
405489
{
406490
BuildTransitionStateInfoList();
491+
ProcessParameterEntries();
407492
}
408493
#endif
409494

@@ -557,21 +642,6 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
557642
}
558643
}
559644

560-
[Tooltip("The animator that this NetworkAnimator component will be synchronizing.")]
561-
[SerializeField] private Animator m_Animator;
562-
563-
/// <summary>
564-
/// The <see cref="Animator"/> associated with this <see cref="NetworkAnimator"/> instance.
565-
/// </summary>
566-
public Animator Animator
567-
{
568-
get { return m_Animator; }
569-
set
570-
{
571-
m_Animator = value;
572-
}
573-
}
574-
575645
/// <summary>
576646
/// Determines whether the <see cref="NetworkAnimator"/> is <see cref="AuthorityModes.Server"/> or <see cref="AuthorityModes.Owner"/> based on the <see cref="AuthorityMode"/> field.
577647
/// Optionally, you can still derive from <see cref="NetworkAnimator"/> and override the <see cref="OnIsServerAuthoritative"/> method.
@@ -679,7 +749,7 @@ protected virtual void Awake()
679749
return;
680750
}
681751

682-
foreach (var parameterEntry in AnimatorParameterEntries)
752+
foreach (var parameterEntry in AnimatorParameterEntries.ParameterEntries)
683753
{
684754
if (!AnimatorParameterEntryTable.ContainsKey(parameterEntry.NameHash))
685755
{

0 commit comments

Comments
 (0)