From f4cfd057cc7d7d99bed7c5d866261d8b55de0fa3 Mon Sep 17 00:00:00 2001 From: mackysoft Date: Sat, 5 Mar 2022 11:39:41 +0900 Subject: [PATCH] Items are now listed directly in the root when there is only one namespace. --- .../Editor/AdvancedTypePopup.cs | 50 +++++++++++++++---- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/AdvancedTypePopup.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/AdvancedTypePopup.cs index cfc9843..25a8c97 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/AdvancedTypePopup.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/AdvancedTypePopup.cs @@ -23,6 +23,8 @@ namespace MackySoft.SerializeReferenceExtensions.Editor { /// public class AdvancedTypePopup : AdvancedDropdown { + const int kMaxNamespaceNestCount = 16; + public static void AddTo (AdvancedDropdownItem root,IEnumerable types) { int itemCount = 0; @@ -32,8 +34,31 @@ namespace MackySoft.SerializeReferenceExtensions.Editor { }; root.AddChild(nullItem); + Type[] typeArray = types.OrderByType().ToArray(); + + // Single namespace if the root has one namespace and the nest is unbranched. + bool isSingleNamespace = true; + string[] namespaces = new string[kMaxNamespaceNestCount]; + foreach (Type type in typeArray) { + string[] splittedTypePath = TypeMenuUtility.GetSplittedTypePath(type); + if (splittedTypePath.Length <= 1) { + continue; + } + for (int k = 0;(splittedTypePath.Length - 1) > k;k++) { + string ns = namespaces[k]; + if (ns == null) { + namespaces[k] = splittedTypePath[k]; + } + else if (ns != splittedTypePath[k]) { + + isSingleNamespace = false; + break; + } + } + } + // Add type items. - foreach (Type type in types.OrderByType()) { + foreach (Type type in typeArray) { string[] splittedTypePath = TypeMenuUtility.GetSplittedTypePath(type); if (splittedTypePath.Length == 0) { continue; @@ -42,16 +67,19 @@ namespace MackySoft.SerializeReferenceExtensions.Editor { AdvancedDropdownItem parent = root; // Add namespace items. - for (int k = 0;(splittedTypePath.Length - 1) > k;k++) { - AdvancedDropdownItem foundItem = GetItem(parent,splittedTypePath[k]); - if (foundItem != null) { - parent = foundItem; - } else { - var newItem = new AdvancedDropdownItem(splittedTypePath[k]) { - id = itemCount++, - }; - parent.AddChild(newItem); - parent = newItem; + if (!isSingleNamespace) { + for (int k = 0;(splittedTypePath.Length - 1) > k;k++) { + AdvancedDropdownItem foundItem = GetItem(parent,splittedTypePath[k]); + if (foundItem != null) { + parent = foundItem; + } + else { + var newItem = new AdvancedDropdownItem(splittedTypePath[k]) { + id = itemCount++, + }; + parent.AddChild(newItem); + parent = newItem; + } } }