SyncVarDrawer.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // SyncVar<T> looks like this in the Inspector:
  2. // Health
  3. // Value: 42
  4. // instead, let's draw ._Value directly so it looks like this:
  5. // Health: 42
  6. //
  7. // BUG: Unity also doesn't show custom drawer for readonly fields (#1368395)
  8. using UnityEditor;
  9. using UnityEngine;
  10. namespace Mirror
  11. {
  12. [CustomPropertyDrawer(typeof(SyncVar<>))]
  13. public class SyncVarDrawer : PropertyDrawer
  14. {
  15. static readonly GUIContent syncVarIndicatorContent = new GUIContent("SyncVar<T>", "This variable is a SyncVar<T>.");
  16. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  17. {
  18. Vector2 syncVarIndicatorRect = EditorStyles.miniLabel.CalcSize(syncVarIndicatorContent);
  19. float valueWidth = position.width - syncVarIndicatorRect.x;
  20. Rect valueRect = new Rect(position.x, position.y, valueWidth, position.height);
  21. Rect labelRect = new Rect(position.x + valueWidth, position.y, syncVarIndicatorRect.x, position.height);
  22. EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("_Value"), label, true);
  23. GUI.Label(labelRect, syncVarIndicatorContent, EditorStyles.miniLabel);
  24. }
  25. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  26. {
  27. return EditorGUI.GetPropertyHeight(property.FindPropertyRelative("_Value"));
  28. }
  29. }
  30. }