From b225290b4801ebee75027e7aefd4d05243f19faf Mon Sep 17 00:00:00 2001 From: Soraphis Date: Wed, 19 Jul 2023 19:40:38 +0200 Subject: [PATCH] FIX: Using TextField in AssetGenerator does not work as expected (#409) * fix: pasting FullQualifiedName in the generators text field, not picking up the actual type * fix: replaced TryAdd call to support lower .NET versions --- .../Core/Editor/Editors/AtomGeneratorEditor.cs | 15 ++++++++------- Packages/Core/Editor/Generator/AtomGenerator.cs | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Packages/Core/Editor/Editors/AtomGeneratorEditor.cs b/Packages/Core/Editor/Editors/AtomGeneratorEditor.cs index 8f14ce2c..81203cd5 100644 --- a/Packages/Core/Editor/Editors/AtomGeneratorEditor.cs +++ b/Packages/Core/Editor/Editors/AtomGeneratorEditor.cs @@ -16,8 +16,6 @@ namespace UnityAtoms.Editor private TypeSelectorDropdown typeSelectorDropdown; private SerializedProperty fullQualifiedName; - private SerializedProperty typeNamespace; - private SerializedProperty baseType; private SerializedProperty generatedOptions; private static bool safeSearch = true; @@ -26,8 +24,6 @@ namespace UnityAtoms.Editor { // Find Properties. fullQualifiedName = serializedObject.FindProperty(nameof(AtomGenerator.FullQualifiedName)); - typeNamespace = serializedObject.FindProperty(nameof(AtomGenerator.Namespace)); - baseType = serializedObject.FindProperty(nameof(AtomGenerator.BaseType)); generatedOptions = serializedObject.FindProperty(nameof(AtomGenerator.GenerationOptions)); // Check if the current type is unsafe. @@ -90,8 +86,6 @@ namespace UnityAtoms.Editor serializedObject.Update(); fullQualifiedName.stringValue = selectedType.AssemblyQualifiedName; - typeNamespace.stringValue = selectedType.Namespace; - baseType.stringValue = selectedType.Name; serializedObject.ApplyModifiedProperties(); }); @@ -251,7 +245,14 @@ namespace UnityAtoms.Editor parent.AddChild(dropdownItem); // Use Hash instead of id! If 2 AdvancedDropdownItems have the same name, they will generate the same id (stupid, I know). To ensure searching for a unique identifier, we use the hash instead. - idTypePairs.Add(dropdownItem.GetHashCode(), type); + if(!idTypePairs.TryGetValue(dropdownItem.GetHashCode(), out var preExistingType)) + { + idTypePairs.Add(dropdownItem.GetHashCode(), type); + } + else if(preExistingType.FullName != type.FullName) // type already exists, but it might be just the type itself (e.g. happens for me when using a ECS project) + { + Debug.LogError($"Could not add '{type.FullName}' to list, because it had a hash collision with: {idTypePairs[dropdownItem.GetHashCode()].FullName}"); + } } } diff --git a/Packages/Core/Editor/Generator/AtomGenerator.cs b/Packages/Core/Editor/Generator/AtomGenerator.cs index b39f508b..566e26f3 100644 --- a/Packages/Core/Editor/Generator/AtomGenerator.cs +++ b/Packages/Core/Editor/Generator/AtomGenerator.cs @@ -11,8 +11,8 @@ namespace UnityAtoms.Editor public class AtomGenerator : ScriptableObject { [TextArea] public string FullQualifiedName; - public string Namespace; - public string BaseType; + public string Namespace => string.IsNullOrWhiteSpace(FullQualifiedName) ? "" : Type.GetType(FullQualifiedName)?.Namespace; + public string BaseType => string.IsNullOrWhiteSpace(FullQualifiedName) ? "" : Type.GetType(FullQualifiedName)?.Name; public int GenerationOptions;