Skip to content

Commit

Permalink
fix: hiding SyncSettings when there is nothing to sync
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Mar 31, 2023
1 parent 80fb77b commit 53f3c62
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions Assets/Mirage/Editor/SyncSettingsDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,53 @@ namespace Mirage
[CustomPropertyDrawer(typeof(SyncSettings))]
public class SyncSettingsDrawer : PropertyDrawer
{
/// <summary>
/// no syncvar/syncobject/serailize Override
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
private static bool NoSyncInBehaviour(SerializedProperty property)
{
var targets = property.serializedObject.targetObjects;
foreach (var target in targets)
{
Debug.Assert(target is NetworkBehaviour);
var syncAny = NetworkBehaviourInspectorDrawer.SyncsAnything(target);
if (syncAny)
return false;
}
return true;
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (NoSyncInBehaviour(property))
return 0;

var baseHeight = base.GetPropertyHeight(property, label);
if (property.isExpanded)
{
var height = (baseHeight * 3) + (EditorGUIUtility.standardVerticalSpacing * 2);

// Check if the sync is invalid and increase height if it is
if (InvalidDirection(property, out _))
{
height += (EditorGUIUtility.singleLineHeight * 2) + EditorGUIUtility.standardVerticalSpacing; // Increase height to make room for warning message
}

return height;
}
else
{
return baseHeight;
}
}

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (NoSyncInBehaviour(property))
return;

EditorGUI.BeginProperty(position, label, property);

// Draw foldout arrow and label
Expand Down Expand Up @@ -113,27 +158,6 @@ private void DisplayWarningMessageIfSyncIsInvalid(Rect position, SerializedPrope
}
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var baseHeight = base.GetPropertyHeight(property, label);
if (property.isExpanded)
{
var height = (baseHeight * 3) + (EditorGUIUtility.standardVerticalSpacing * 2);

// Check if the sync is invalid and increase height if it is
if (InvalidDirection(property, out _))
{
height += (EditorGUIUtility.singleLineHeight * 2) + EditorGUIUtility.standardVerticalSpacing; // Increase height to make room for warning message
}

return height;
}
else
{
return baseHeight;
}
}

private static bool InvalidDirection(SerializedProperty property, out string reason)
{
var from = (SyncFrom)property.FindPropertyRelative("From").enumValueIndex;
Expand Down

0 comments on commit 53f3c62

Please sign in to comment.