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