From 2e4d16e109842ab6160ca3185c2609692028f425 Mon Sep 17 00:00:00 2001 From: Jeffrey Vella Date: Fri, 24 May 2019 16:37:26 +0200 Subject: [PATCH 1/2] System/Component inspector names for generic types - Systems/Components with generic type parameters correctly display the full type name in the inspector. - Systems/Components implementing ICustomDisplayName show in the inspector with the user-provided name. --- .../Entity/Entity/EntityDrawer.cs | 5 +++- .../DebugSystems/SystemInfo.cs | 13 +++++++--- .../ICustomDisplayName.cs | 7 +++++ .../TypeHelper.cs | 26 +++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/ICustomDisplayName.cs create mode 100644 Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/TypeHelper.cs diff --git a/Addons/Entitas.VisualDebugging.Unity.Editor/Entitas.VisualDebugging.Unity.Editor/Entity/Entity/EntityDrawer.cs b/Addons/Entitas.VisualDebugging.Unity.Editor/Entitas.VisualDebugging.Unity.Editor/Entity/Entity/EntityDrawer.cs index a8081a9f2..84d5b7564 100644 --- a/Addons/Entitas.VisualDebugging.Unity.Editor/Entitas.VisualDebugging.Unity.Editor/Entity/Entity/EntityDrawer.cs +++ b/Addons/Entitas.VisualDebugging.Unity.Editor/Entitas.VisualDebugging.Unity.Editor/Entity/Entity/EntityDrawer.cs @@ -164,7 +164,10 @@ public static void DrawComponents(IEntity entity) public static void DrawComponent(bool[] unfoldedComponents, string[] componentMemberSearch, IEntity entity, int index, IComponent component) { var componentType = component.GetType(); - var componentName = componentType.Name.RemoveComponentSuffix(); + var customNameProvider = component as ICustomDisplayName; + var componentName = customNameProvider?.DisplayName + ?? TypeHelper.GetTypeName(componentType).RemoveComponentSuffix(); + if (EditorLayout.MatchesSearchString(componentName.ToLower(), componentNameSearchString.ToLower())) { var boxStyle = getColoredBoxStyle(entity, index); diff --git a/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/DebugSystems/SystemInfo.cs b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/DebugSystems/SystemInfo.cs index eb6569b7f..b65a07417 100644 --- a/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/DebugSystems/SystemInfo.cs +++ b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/DebugSystems/SystemInfo.cs @@ -62,9 +62,16 @@ public SystemInfo(ISystem system) { _interfaceFlags = getInterfaceFlags(system); var debugSystem = system as DebugSystems; - _systemName = debugSystem != null - ? debugSystem.name - : system.GetType().Name.RemoveSystemSuffix(); + if (debugSystem != null) + { + _systemName = debugSystem.name; + } + else + { + var customNameProvider = system as ICustomDisplayName; + _systemName = customNameProvider?.DisplayName + ?? TypeHelper.GetTypeName(system.GetType()).RemoveSystemSuffix(); + } isActive = true; } diff --git a/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/ICustomDisplayName.cs b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/ICustomDisplayName.cs new file mode 100644 index 000000000..036f33a5d --- /dev/null +++ b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/ICustomDisplayName.cs @@ -0,0 +1,7 @@ +namespace Entitas.VisualDebugging.Unity +{ + public interface ICustomDisplayName + { + string DisplayName { get; } + } +} diff --git a/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/TypeHelper.cs b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/TypeHelper.cs new file mode 100644 index 000000000..031e88142 --- /dev/null +++ b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/TypeHelper.cs @@ -0,0 +1,26 @@ +using System; + +namespace Entitas.VisualDebugging.Unity +{ + public static class TypeHelper + { + /// + /// Returns a type name including generic type parameters. + /// + public static string GetTypeName(Type type) + { + if (type.IsGenericType) + { + var simpleName = type.Name.Substring(0, type.Name.IndexOf('`')); + string genericTypeParams = string.Empty; + for (int i = 0; i < type.GenericTypeArguments.Length; i++) + { + if (i > 0) genericTypeParams += ","; + genericTypeParams += GetTypeName(type.GenericTypeArguments[i]); + } + return string.Format("{0}<{1}>", simpleName, string.Join(", ", genericTypeParams)); + } + return type.Name; + } + } +} From fa0fd30cee39ee2a1d9bc5c1f8069e13819a1696 Mon Sep 17 00:00:00 2001 From: Jeffrey Vella Date: Fri, 24 May 2019 17:09:10 +0200 Subject: [PATCH 2/2] Added files to solution and changes to support .Net <4.5 --- .../Entity/Entity/EntityDrawer.cs | 5 +++-- .../Entitas.VisualDebugging.Unity.csproj | 6 +++--- .../DebugSystems/SystemInfo.cs | 10 ++++++++-- .../Entitas.VisualDebugging.Unity/TypeHelper.cs | 10 +++++++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Addons/Entitas.VisualDebugging.Unity.Editor/Entitas.VisualDebugging.Unity.Editor/Entity/Entity/EntityDrawer.cs b/Addons/Entitas.VisualDebugging.Unity.Editor/Entitas.VisualDebugging.Unity.Editor/Entity/Entity/EntityDrawer.cs index 84d5b7564..ce8a34dd6 100644 --- a/Addons/Entitas.VisualDebugging.Unity.Editor/Entitas.VisualDebugging.Unity.Editor/Entity/Entity/EntityDrawer.cs +++ b/Addons/Entitas.VisualDebugging.Unity.Editor/Entitas.VisualDebugging.Unity.Editor/Entity/Entity/EntityDrawer.cs @@ -165,8 +165,9 @@ public static void DrawComponent(bool[] unfoldedComponents, string[] componentMe { var componentType = component.GetType(); var customNameProvider = component as ICustomDisplayName; - var componentName = customNameProvider?.DisplayName - ?? TypeHelper.GetTypeName(componentType).RemoveComponentSuffix(); + var componentName = customNameProvider != null + ? customNameProvider.DisplayName + : TypeHelper.GetTypeName(componentType).RemoveComponentSuffix(); if (EditorLayout.MatchesSearchString(componentName.ToLower(), componentNameSearchString.ToLower())) { diff --git a/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity.csproj b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity.csproj index ac01f5f3c..ab1f51f4c 100644 --- a/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity.csproj +++ b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity.csproj @@ -44,6 +44,8 @@ + + @@ -51,8 +53,6 @@ Entitas - - - + \ No newline at end of file diff --git a/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/DebugSystems/SystemInfo.cs b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/DebugSystems/SystemInfo.cs index b65a07417..0345a929f 100644 --- a/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/DebugSystems/SystemInfo.cs +++ b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/DebugSystems/SystemInfo.cs @@ -69,8 +69,14 @@ public SystemInfo(ISystem system) { else { var customNameProvider = system as ICustomDisplayName; - _systemName = customNameProvider?.DisplayName - ?? TypeHelper.GetTypeName(system.GetType()).RemoveSystemSuffix(); + if (customNameProvider != null) + { + _systemName = customNameProvider.DisplayName; + } + else + { + _systemName = TypeHelper.GetTypeName(system.GetType()).RemoveSystemSuffix(); + } } isActive = true; diff --git a/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/TypeHelper.cs b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/TypeHelper.cs index 031e88142..cded38560 100644 --- a/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/TypeHelper.cs +++ b/Addons/Entitas.VisualDebugging.Unity/Entitas.VisualDebugging.Unity/TypeHelper.cs @@ -13,12 +13,16 @@ public static string GetTypeName(Type type) { var simpleName = type.Name.Substring(0, type.Name.IndexOf('`')); string genericTypeParams = string.Empty; - for (int i = 0; i < type.GenericTypeArguments.Length; i++) + var args = !type.IsGenericTypeDefinition + ? type.GetGenericArguments() + : Type.EmptyTypes; + + for (int i = 0; i < args.Length; i++) { if (i > 0) genericTypeParams += ","; - genericTypeParams += GetTypeName(type.GenericTypeArguments[i]); + genericTypeParams += GetTypeName(args[i]); } - return string.Format("{0}<{1}>", simpleName, string.Join(", ", genericTypeParams)); + return string.Format("{0}<{1}>", simpleName, genericTypeParams); } return type.Name; }