From ab1c4dd8a005be6de9bd654be12e7b4e2ec27a01 Mon Sep 17 00:00:00 2001 From: FrankieF Date: Sun, 11 Feb 2024 21:30:43 +0900 Subject: [PATCH] Add support for nested class types --- .../Editor/Extensions/GameObjectExtensions.cs | 10 +++++- .../Editor/Loaders/CustomPropertyLoader.cs | 23 ++++++++++++- .../Runtime/CustomProperty.cs | 32 ++++++++++++++++--- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Extensions/GameObjectExtensions.cs b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Extensions/GameObjectExtensions.cs index 2b3adc00..52bac482 100644 --- a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Extensions/GameObjectExtensions.cs +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Extensions/GameObjectExtensions.cs @@ -132,7 +132,15 @@ private static void ChangeLayersRecursively(this Transform trans, int layer) public static void BroadcastProperty(this GameObject go, CustomProperty property, Dictionary objectsById) { object objValue; - + if (property.m_Type == "class") + { + var classProperty = (ClassCustomProperty)property; + foreach (var entry in classProperty.m_CustomProperties) + { + BroadcastProperty(go, entry.Value, objectsById); + } + return; + } if (property.m_Type == "bool") { objValue = property.GetValueAsBool(); diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Loaders/CustomPropertyLoader.cs b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Loaders/CustomPropertyLoader.cs index dabf1205..f171bc6c 100644 --- a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Loaders/CustomPropertyLoader.cs +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Loaders/CustomPropertyLoader.cs @@ -31,7 +31,28 @@ public static CustomProperty LoadCustomProperty(XElement xProperty) property.m_Name = xProperty.GetAttributeAs("name", ""); property.m_Type = xProperty.GetAttributeAs("type", "string"); - + + if (property.m_Type == "class") + { + var classProperty = new ClassCustomProperty + { + m_Name = property.m_Name, + m_Type = property.m_Type + }; + var elements = xProperty.Descendants(); + if (elements != null) + { + foreach (var element in elements) + { + var childProperty = LoadCustomProperty(element); + if (!childProperty.IsEmpty) + { + classProperty.m_CustomProperties.Add(childProperty.m_Name, childProperty); + } + } + } + return classProperty; + } // In some cases, value may be in the default attribute property.m_Value = xProperty.GetAttributeAs("default", ""); diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Runtime/CustomProperty.cs b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Runtime/CustomProperty.cs index 739c88c4..bc882cbc 100644 --- a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Runtime/CustomProperty.cs +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Runtime/CustomProperty.cs @@ -10,7 +10,26 @@ public class CustomProperty public string m_Type; public string m_Value; - public bool IsEmpty => string.IsNullOrEmpty(m_Name); + public virtual CustomProperty GetCustomProperty(string key) => this; + + public bool IsEmpty + { + get { return string.IsNullOrEmpty(m_Name); } + } + } + + [Serializable] + public class ClassCustomProperty : CustomProperty + { + public Dictionary m_CustomProperties = new Dictionary(); + + public override CustomProperty GetCustomProperty(string key) + { + if (m_CustomProperties.TryGetValue(key, out var customProperty)) + return customProperty; + return null; + } + } // Helper extension methods @@ -20,12 +39,17 @@ public static bool TryGetProperty(this List list, string propert { if (list != null) { - property = list.Find(p => String.Equals(p.m_Name, propertyName, StringComparison.OrdinalIgnoreCase)); - return property != null; + property = list.Find(p => String.Equals(p.GetCustomProperty(propertyName)?.m_Name, propertyName, StringComparison.OrdinalIgnoreCase)); + if (property != null) + { + property = property.GetCustomProperty(propertyName); + return true; + } + return false; } property = null; return false; } } -} +} \ No newline at end of file