Skip to content

Commit

Permalink
feat: SyncSet and SyncDictionary now show in inspector (#1561)
Browse files Browse the repository at this point in the history
SyncSet and SyncDictionary now show in inspector
  • Loading branch information
James-Frowen committed Mar 20, 2020
1 parent 6b0fa18 commit 5510711
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
16 changes: 15 additions & 1 deletion Assets/Mirror/Editor/InspectorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,23 @@ public static bool IsSerializeField(this FieldInfo field)
object[] fieldMarkers = field.GetCustomAttributes(typeof(SerializeField), true);
return fieldMarkers.Length > 0;
}
public static bool IsVisibleInInspector(this FieldInfo field)
public static bool IsVisibleField(this FieldInfo field)
{
return field.IsPublic || IsSerializeField(field);
}

public static bool IsSyncObject(this FieldInfo field)
{
return typeof(SyncObject).IsAssignableFrom(field.FieldType);
}
public static bool HasShowInInspector(this FieldInfo field)
{
object[] fieldMarkers = field.GetCustomAttributes(typeof(ShowInInspectorAttribute), true);
return fieldMarkers.Length > 0;
}
public static bool IsVisibleSyncObject(this FieldInfo field)
{
return field.IsPublic || HasShowInInspector(field);
}
}
}
12 changes: 6 additions & 6 deletions Assets/Mirror/Editor/NetworkBehaviourInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ void OnEnable()
syncVarNames = new List<string>();
foreach (FieldInfo field in InspectorHelper.GetAllFields(scriptClass, typeof(NetworkBehaviour)))
{
if (field.IsSyncVar() && field.IsVisibleInInspector())
if (field.IsSyncVar() && field.IsVisibleField())
{
syncVarNames.Add(field.Name);
}
}

int numSyncLists = scriptClass.GetFields().Count(
field => field.FieldType.BaseType != null &&
field.FieldType.BaseType.Name.Contains("SyncList"));
int numSyncLists = InspectorHelper.GetAllFields(serializedObject.targetObject.GetType(), typeof(NetworkBehaviour))
.Count(field => field.IsSyncObject() && field.IsVisibleSyncObject());

if (numSyncLists > 0)
{
showSyncLists = new bool[numSyncLists];
Expand All @@ -79,9 +79,9 @@ public override void OnInspectorGUI()
DrawDefaultInspector();
// find SyncLists.. they are not properties.
int syncListIndex = 0;
foreach (FieldInfo field in serializedObject.targetObject.GetType().GetFields())
foreach (FieldInfo field in InspectorHelper.GetAllFields(serializedObject.targetObject.GetType(), typeof(NetworkBehaviour)))
{
if (field.FieldType.BaseType != null && field.FieldType.BaseType.Name.Contains("SyncList"))
if (field.IsSyncObject() && field.IsVisibleSyncObject())
{
showSyncLists[syncListIndex] = EditorGUILayout.Foldout(showSyncLists[syncListIndex], "SyncList " + field.Name + " [" + field.FieldType.Name + "]");
if (showSyncLists[syncListIndex])
Expand Down
7 changes: 7 additions & 0 deletions Assets/Mirror/Runtime/CustomAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,11 @@ public class ClientCallbackAttribute : Attribute { }
/// Converts a string property into a Scene property in the inspector
/// </summary>
public class SceneAttribute : PropertyAttribute { }

/// <summary>
/// Used to show private SyncList in the inspector,
/// <para> Use instead of SerializeField for non Serializable types </para>
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class ShowInInspectorAttribute : Attribute { }
}

0 comments on commit 5510711

Please sign in to comment.