From c13fd69e1dfe3a545474e1dc3a64d73ec6d746e5 Mon Sep 17 00:00:00 2001 From: Huisama Date: Sun, 16 Oct 2022 18:11:59 +0800 Subject: [PATCH 1/4] [feat] export complex custom type export as json string --- .../Examples/Overhead/Maps/TestOverhead.tmx | 88 +++++---- .../Examples/Overhead/Maps/propertytypes.xml | 16 ++ .../Scripts/CustomProperty.cs | 22 ++- .../Extensions/CustomPropertyExtensions.cs | 10 - .../Editor/Extensions/GameObjectExtensions.cs | 2 +- .../Editor/Extensions/XmlExtensions.cs | 36 ++-- .../Editor/Importers/TiledAssetImporter.cs | 8 +- .../Editor/Loaders/CustomPropertyLoader.cs | 185 ++++++++++++++++-- .../Scripts/Editor/Loaders/TilesetLoader.cs | 4 +- .../Editor/Settings/CustomPropertiesWindow.cs | 8 +- .../Extensions/CustomPropertyExtensions.cs | 12 +- 11 files changed, 299 insertions(+), 92 deletions(-) create mode 100644 SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/TestOverhead.tmx b/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/TestOverhead.tmx index 5272569b..ed12225c 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/TestOverhead.tmx +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/TestOverhead.tmx @@ -1,63 +1,63 @@ - + - - - + + + -600,601,602,709,709,709,709,709,709,709,709,709,709,629,630,630,630,630,630,630,631,193,193,193,193, -632,665,634,709,709,709,709,709,709,709,709,709,709,629,630,630,630,630,630,630,631,193,193,193,193, -632,633,634,709,709,709,709,709,709,709,709,709,709,629,630,630,630,630,630,630,631,193,193,193,193, -632,633,634,709,709,709,709,709,709,709,709,709,709,629,630,630,630,630,630,630,631,193,193,193,193, -632,633,634,709,709,709,709,709,709,709,709,709,709,629,630,630,630,630,630,630,631,193,193,193,193, -632,633,634,709,709,709,709,709,709,709,709,709,709,661,662,662,662,662,662,662,663,205,205,205,205, -632,633,634,709,709,709,709,709,709,709,709,709,709,709,25,26,26,26,26,26,26,26,26,26,26, -632,633,634,709,709,709,709,709,709,709,709,709,709,709,37,1,1,1,1,1,1,1,1,1,1, -632,633,634,709,709,709,709,709,709,709,709,709,709,709,306,306,1,1,1,1,1,1,1,1,38, -632,633,634,709,709,709,709,709,709,709,709,709,709,709,37,1,1,1,1,1,1,1,1,1,1, -632,665,634,709,709,709,709,709,709,709,709,709,709,709,49,123,1,1,1,1,38,1,1,1,1, -632,633,634,709,709,709,709,709,709,709,709,709,709,709,709,49,50,123,1,1,1,1,1,1,1, -632,633,634,536,537,537,537,537,537,537,537,538,709,709,709,709,709,49,123,1,1,1,1,1,1, -632,665,634,568,633,633,633,633,633,633,633,570,709,709,709,709,709,709,49,50,123,1,1,1,1, -600,601,602,600,601,601,601,601,601,601,601,602,627,598,598,598,598,598,598,628,49,50,50,50,50 +577,578,579,646,646,646,646,646,646,646,646,646,646,606,607,607,607,607,607,607,608,193,193,193,193, +609,642,611,646,646,646,646,646,646,646,646,646,646,606,607,607,607,607,607,607,608,193,193,193,193, +609,610,611,646,646,646,646,646,646,646,646,646,646,606,607,607,607,607,607,607,608,193,193,193,193, +609,610,611,646,646,646,646,646,646,646,646,646,646,606,607,607,607,607,607,607,608,193,193,193,193, +609,610,611,646,646,646,646,646,646,646,646,646,646,606,607,607,607,607,607,607,608,193,193,193,193, +609,610,611,646,646,646,646,646,646,646,646,646,646,638,639,639,639,639,639,639,640,205,205,205,205, +609,610,611,646,646,646,646,646,646,646,646,646,646,646,25,26,26,26,26,26,26,26,26,26,26, +609,610,611,646,646,646,646,646,646,646,646,646,646,646,37,1,1,1,1,1,1,1,1,1,1, +609,610,611,646,646,646,646,646,646,646,646,646,646,646,283,283,1,1,1,1,1,1,1,1,38, +609,610,611,646,646,646,646,646,646,646,646,646,646,646,37,1,1,1,1,1,1,1,1,1,1, +609,642,611,646,646,646,646,646,646,646,646,646,646,646,49,123,1,1,1,1,38,1,1,1,1, +609,610,611,646,646,646,646,646,646,646,646,646,646,646,646,49,50,123,1,1,1,1,1,1,1, +609,610,611,513,514,514,514,514,514,514,514,515,646,646,646,646,646,49,123,1,1,1,1,1,1, +609,642,611,545,610,610,610,610,610,610,610,547,646,646,646,646,646,646,49,50,123,1,1,1,1, +577,578,579,577,578,578,578,578,578,578,578,579,604,575,575,575,575,575,575,605,49,50,50,50,50 -0,0,0,585,0,0,0,0,0,0,0,586,587,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,585,0,0,0,0,0,0,0,586,587,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,585,0,0,0,0,0,0,0,586,587,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,591,267,0,0,265,0,266,0,560,557,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,591,0,0,0,265,0,0,0,0,589,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,591,0,0,0,265,265,265,265,265,589,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,591,0,0,0,265,0,0,0,265,0,560,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,591,267,0,267,265,267,0,550,265,0,266,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,591,0,0,0,265,265,265,265,265,265,265,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,591,0,267,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,591,592,496,496,496,496,496,496,496,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,586,586,586,586,586,586,586,586,586,591,0,266,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,591,0,0,0,269,0,0,0,0,0,0,0,0, -0,0,0,0,271,0,550,0,242,243,243,0,495,496,496,496,496,496,0,0,0,0,0,0,0, +0,0,0,562,0,0,0,0,0,0,0,563,564,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,562,0,0,0,0,0,0,0,563,564,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,562,0,0,0,0,0,0,0,563,564,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,568,244,0,0,242,0,243,0,537,534,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,568,0,0,0,242,0,0,0,0,566,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,568,0,0,0,242,242,242,242,242,566,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,568,0,0,0,242,0,0,0,242,0,537,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,568,244,0,244,242,244,0,527,242,0,243,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,568,0,0,0,242,242,242,242,242,242,242,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,568,0,244,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,568,569,473,473,473,473,473,473,473,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,563,563,563,563,563,563,563,563,563,568,0,243,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,568,0,0,0,246,0,0,0,0,0,0,0,0, +0,0,0,0,248,0,527,0,219,220,220,0,472,473,473,473,473,473,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,316,317,318,319,320,321,322,323,324,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,275,0,348,349,350,351,352,353,354,355,356,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,380,381,382,383,668,385,386,387,388,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,244,0,0,0,0,0,0,0,0,244,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,293,294,295,296,297,298,299,300,301,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,252,0,325,326,327,328,329,330,331,332,333,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,357,358,359,360,645,362,363,364,365,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,221,0,0,0,0,0,0,0,0,221,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,244,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,221,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,275,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,252,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 @@ -70,7 +70,7 @@ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,1189,0,0,0,1190,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,647,0,0,0,648,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, @@ -82,6 +82,14 @@ + + + + + + + + diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml b/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml new file mode 100644 index 00000000..3220437a --- /dev/null +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/CustomProperty.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/CustomProperty.cs index 4478f0d7..61169101 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/CustomProperty.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/CustomProperty.cs @@ -1,18 +1,38 @@ using System; using System.Collections.Generic; +using Unity.Plastic.Newtonsoft.Json.Linq; +using UnityEngine; namespace SuperTiled2Unity { [Serializable] public class CustomProperty { + private static Dictionary JObjectDictionary_ = new Dictionary(); + public string m_Name; public string m_Type; public string m_Value; + public JObject m_JObject + { + get => JObjectDictionary_[m_Name]; + set => JObjectDictionary_[m_Name] = value; + } + + public CustomProperty CloneProperty() + { + var newObject = new CustomProperty(); + newObject.m_Name = m_Name; + newObject.m_Type = m_Type; + newObject.m_JObject = m_JObject; + + return newObject; + } + public bool IsEmpty { - get { return string.IsNullOrEmpty(m_Name); } + get { return string.IsNullOrEmpty(m_Name) && m_JObject != null; } } } diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/CustomPropertyExtensions.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/CustomPropertyExtensions.cs index 8b121d1e..9b99b0ca 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/CustomPropertyExtensions.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/CustomPropertyExtensions.cs @@ -6,16 +6,6 @@ namespace SuperTiled2Unity.Editor { public static class CustomPropertyExtensions { - public static CustomProperty CloneProperty(this CustomProperty property) - { - var cloned = new CustomProperty(); - cloned.m_Name = property.m_Name; - cloned.m_Type = property.m_Type; - cloned.m_Value = property.m_Value; - - return cloned; - } - public static int CombineFromSource(this List list, List source) { int numAdded = 0; diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/GameObjectExtensions.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/GameObjectExtensions.cs index 2b3adc00..aaed3bd3 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/GameObjectExtensions.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/GameObjectExtensions.cs @@ -165,7 +165,7 @@ public static void BroadcastProperty(this GameObject go, CustomProperty property } else { - objValue = property.GetValueAsString(); + objValue = property.m_JObject.ToString(); } // Use properties on all types in hierary that inherit from MonoBehaviour diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/XmlExtensions.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/XmlExtensions.cs index 3eb3fbc4..ef0ce7df 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/XmlExtensions.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/XmlExtensions.cs @@ -23,10 +23,10 @@ public static Vector2[] GetAttributeAsVector2Array(this XElement element, string } var vectors = from v in data.Split(' ') - let a = v.Split(',').ToArray() - let x = Convert.ToSingle(a[0], CultureInfo.InvariantCulture) - let y = Convert.ToSingle(a[1], CultureInfo.InvariantCulture) - select new Vector2(x, y); + let a = v.Split(',').ToArray() + let x = Convert.ToSingle(a[0], CultureInfo.InvariantCulture) + let y = Convert.ToSingle(a[1], CultureInfo.InvariantCulture) + select new Vector2(x, y); return vectors.ToArray(); } @@ -76,7 +76,7 @@ public static T GetAttributeAs(this XElement element, string name, T defaultV } } - return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture); + return (T) Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture); } public static T GetAttributeAs(this XElement element, string name) where T : IConvertible @@ -84,14 +84,16 @@ public static T GetAttributeAs(this XElement element, string name) where T : return element.GetAttributeAs(name, default(T)); } - public static T GetPropertyAttributeAs(this XElement element, string name, T defaultValue) where T : IConvertible + public static T GetPropertyAttributeAs(this XElement element, string name, T defaultValue) + where T : IConvertible { var xProperties = element.Element("properties"); if (xProperties != null) { foreach (var xProperty in xProperties.Elements("property")) { - if (string.Equals(xProperty.GetAttributeAs("name"), name, StringComparison.OrdinalIgnoreCase)) + if (string.Equals(xProperty.GetAttributeAs("name"), name, + StringComparison.OrdinalIgnoreCase)) { return xProperty.GetAttributeAs("value", defaultValue); } @@ -134,16 +136,22 @@ public static void CombineWithTemplate(this XElement xObject, string template) // Combine the properties from source and template var properties = new Dictionary(); - // Collect the properties from the template first, by named key - foreach (var prop in xTemplate.Descendants("property")) + if (xTemplate.Element("properties") is { } propertiesTemplate) { - properties.Add(prop.GetAttributeAs("name"), prop); + // Collect the properties from the template first, by named key + foreach (var prop in propertiesTemplate.Elements("property")) + { + properties.Add(prop.GetAttributeAs("name"), prop); + } } - // Collect the properties on the source, overriding by named key - foreach (var prop in xObject.Descendants("property")) + if (xObject.Element("properties") is { } propertiesElem) { - properties[prop.GetAttributeAs("name")] = prop; + // Collect the properties on the source, overriding by named key + foreach (var prop in propertiesElem.Elements("property")) + { + properties[prop.GetAttributeAs("name")] = prop; + } } // Put the combined properties into the object @@ -161,4 +169,4 @@ public static void CombineWithTemplate(this XElement xObject, string template) } } } -} +} \ No newline at end of file diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs index 9253f2a9..3f87a4f9 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs @@ -46,7 +46,7 @@ public void AddSuperCustomProperties(GameObject go, XElement xProperties, SuperT { // Load our "local" properties first var component = go.AddComponent(); - var properties = CustomPropertyLoader.LoadCustomPropertyList(xProperties); + var properties = CustomPropertyLoader.LoadCustomPropertyListWithExpansion(xProperties, SuperImportContext); // Do we have any properties from a tile to add? if (tile != null) @@ -55,7 +55,7 @@ public void AddSuperCustomProperties(GameObject go, XElement xProperties, SuperT } // Add properties from our object type (this should be last) - properties.AddPropertiesFromType(typeName, SuperImportContext); + // properties.AddPropertiesFromType(typeName, SuperImportContext); // Sort the properties alphabetically component.m_Properties = properties.OrderBy(p => p.m_Name).ToList(); @@ -140,7 +140,7 @@ protected void AssignUnityTag(SuperCustomProperties properties) CustomProperty prop; if (properties.TryGetCustomProperty(StringConstants.Unity_Tag, out prop)) { - string tag = prop.m_Value; + string tag = prop.GetValueAsString(); CheckTagName(tag); properties.gameObject.tag = tag; } @@ -152,7 +152,7 @@ protected void AssignUnityLayer(SuperCustomProperties properties) CustomProperty prop; if (properties.TryGetCustomProperty(StringConstants.Unity_Layer, out prop)) { - string layer = prop.m_Value; + string layer = prop.GetValueAsString(); if (!UnityEditorInternal.InternalEditorUtility.layers.Contains(layer)) { string report = string.Format("Layer '{0}' is not defined in the Tags and Layers settings.", layer); diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs index dabf1205..a4861def 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs @@ -1,10 +1,147 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; using System.Xml.Linq; +using Unity.Plastic.Newtonsoft.Json; +using Unity.Plastic.Newtonsoft.Json.Linq; +using UnityEngine.Assertions; namespace SuperTiled2Unity.Editor { public static class CustomPropertyLoader { + public static List LoadCustomPropertyListWithExpansion(XElement xProperties, SuperImportContext context) + { + List properties = new List(); + if (xProperties != null) + { + foreach (var xProperty in xProperties.Elements("property")) + { + var property = LoadCustomPropertyWithExpansion(xProperty, context); + property.m_Value = property.m_JObject.ToString(Formatting.None); + + if (!property.IsEmpty) + { + properties.Add(property); + } + } + } + + return properties; + } + + private static CustomProperty LoadCustomPropertyWithExpansion(XElement xProperty, SuperImportContext context) + { + CustomProperty property = new CustomProperty(); + + property.m_Name = xProperty.GetAttributeAs("name", ""); + var type = xProperty.GetAttributeAs("type", "string"); + + if (type == "class") + { + property.m_Type = xProperty.GetAttributeAs("propertytype", ""); + Assert.IsFalse(property.m_Type.IsEmpty()); + } + else + { + property.m_Type = type; + } + + property.m_JObject = ParseXmlDescPropertiesToJObjectWithExpansion( + xProperty, context, type == "class", property.m_Type); + + Assert.IsNotNull(property.m_JObject); + + return property; + } + + private static JObject ParseXmlDescPropertiesToJObjectWithExpansion( + XElement xProperty, SuperImportContext context, bool isCustomClass, string propertyType) + { + + var jObj = new JObject(); + + Assert.IsFalse(isCustomClass && propertyType.IsEmpty()); + jObj["type"] = isCustomClass ? "class" : propertyType; + + if (isCustomClass) + { + var realType = xProperty.GetAttributeAs("propertytype", ""); + Assert.IsFalse(realType.IsEmpty()); + jObj["realType"] = realType; + + var properties = xProperty.Element("properties"); + + // Get default + if (context.Settings.CustomObjectTypes.TryGetCustomObjectType(realType, out var predefined)) + { + foreach (var predefinedMCustomProperty in predefined.m_CustomProperties) + { + jObj[predefinedMCustomProperty.m_Name] = + GetExpandedJObject(predefinedMCustomProperty.m_JObject, context); + } + } + + // replace default + if (properties is { }) + { + foreach (var xSubProperty in properties.Elements("property")) + { + var name = xSubProperty.GetAttributeAs("name"); + var isSubCustomClass = xSubProperty.GetAttributeAs("type") == "class"; + var subRealType = + isSubCustomClass + ? xSubProperty.GetAttributeAs("propertytype", "") + : xSubProperty.GetAttributeAs("type"); + + jObj[name] = ParseXmlDescPropertiesToJObjectWithExpansion( + xSubProperty, context, isSubCustomClass, subRealType); + } + } + } + else + { + GetPlaintValueFromXmlEelment(xProperty, jObj); + } + + return jObj; + } + + private static JObject GetExpandedJObject(JObject root, SuperImportContext context) + { + var expanded = new JObject(); + + var type = root["type"].ToString(); + + if (type == "class") + { + var realType = root["realType"].ToString(); + + var sub = new JObject(); + // Get default + if (context.Settings.CustomObjectTypes.TryGetCustomObjectType(realType, out var predefined)) + { + foreach (var predefinedMCustomProperty in predefined.m_CustomProperties) + { + sub[predefinedMCustomProperty.m_Name] = + GetExpandedJObject(predefinedMCustomProperty.m_JObject, context); + } + } + + expanded["realType"] = realType; + expanded["type"] = "class"; + expanded["value"] = sub; + } + else + { + expanded["type"] = type; + expanded["realType"] = type; + expanded["value"] = root["value"]; + } + + return expanded; + } + public static List LoadCustomPropertyList(XElement xProperties) { List properties = new List(); @@ -31,23 +168,49 @@ public static CustomProperty LoadCustomProperty(XElement xProperty) property.m_Name = xProperty.GetAttributeAs("name", ""); property.m_Type = xProperty.GetAttributeAs("type", "string"); + property.m_JObject = ParseXmlDescPropertiesToJObject(xProperty); + + Assert.IsNotNull(property.m_JObject); + + return property; + } + + public static JObject ParseXmlDescPropertiesToJObject(XElement xProperty) + { + var propType = xProperty.GetAttributeAs("type", ""); - // In some cases, value may be in the default attribute - property.m_Value = xProperty.GetAttributeAs("default", ""); + var jobj = new JObject(); - // A value attribute overrides a default attribute - if (!string.IsNullOrEmpty(xProperty.Value)) + Assert.IsFalse(propType.IsEmpty()); + jobj["type"] = propType; + + if (propType == "class") { - // Using inner text - property.m_Value = xProperty.Value; + var realType = xProperty.GetAttributeAs("propertytype", ""); + Assert.IsFalse(realType.IsEmpty()); + jobj["realType"] = realType; } else { - // Using value attribute - property.m_Value = xProperty.GetAttributeAs("value", property.m_Value); + GetPlaintValueFromXmlEelment(xProperty, jobj); } + + return jobj; + } - return property; + private static void GetPlaintValueFromXmlEelment(XElement xProperty, JObject jobj) + { + var defaultVal = xProperty.GetAttributeAs("default"); + if (defaultVal != null) + { + jobj["value"] = defaultVal; + } + else + { + var value = xProperty.GetAttributeAs("value", ""); + Assert.IsFalse(value.IsEmpty()); + jobj["value"] = value; + } } } -} +} \ No newline at end of file diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/TilesetLoader.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/TilesetLoader.cs index 324c0fb6..31916b09 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/TilesetLoader.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/TilesetLoader.cs @@ -388,8 +388,8 @@ private void AssignCollisionObjectProperties(CollisionObject collision, SuperTil if (layerProperty != null) { // Explicit request to assign a layer to a collision. Report errors if the layer is missing. - m_Importer.CheckLayerName(layerProperty.m_Value); - collision.m_PhysicsLayer = layerProperty.m_Value; + m_Importer.CheckLayerName(layerProperty.GetValueAsString()); + collision.m_PhysicsLayer = layerProperty.GetValueAsString(); } else if (!string.IsNullOrEmpty(collision.m_ObjectName)) { diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Settings/CustomPropertiesWindow.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Settings/CustomPropertiesWindow.cs index d31b71a8..7089b2f1 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Settings/CustomPropertiesWindow.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Settings/CustomPropertiesWindow.cs @@ -6,7 +6,8 @@ namespace SuperTiled2Unity.Editor { public class CustomPropertiesWindow : EditorWindow { - private static readonly GUIContent m_TitleContent = new GUIContent("Custom Object Types Properties", "These are the objects created in the Object Types Editor in Tiled."); + private static readonly GUIContent m_TitleContent = new GUIContent("Custom Object Types Properties", + "These are the objects created in the Object Types Editor in Tiled."); private Vector2 m_ScrollPosition; @@ -61,7 +62,8 @@ private void OnGUICustomObjectType(CustomObjectType obj) using (new GUILayout.HorizontalScope()) { EditorGUILayout.LabelField(customProperty.m_Name); - EditorGUILayout.LabelField(customProperty.m_Value, EditorStyles.textField); + EditorGUILayout.LabelField(customProperty.m_JObject.ToString(), + EditorStyles.textField); EditorGUILayout.LabelField(customProperty.m_Type); } } @@ -105,4 +107,4 @@ internal static void EditorGUILayout_ColorFieldNoEdit(GUIContent label, Color co } #endif } -} +} \ No newline at end of file diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/CustomPropertyExtensions.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/CustomPropertyExtensions.cs index 46c81e33..c56cd6d2 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/CustomPropertyExtensions.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/CustomPropertyExtensions.cs @@ -6,32 +6,32 @@ public static class CustomPropertyExtensions { public static string GetValueAsString(this CustomProperty property) { - return property.m_Value; + return property.m_JObject["value"]!.ToString(); } public static Color GetValueAsColor(this CustomProperty property) { - return property.m_Value.ToColor(); + return property.m_JObject["value"]!.ToString().ToColor(); } public static int GetValueAsInt(this CustomProperty property) { - return property.m_Value.ToInt(); + return property.m_JObject["value"]!.ToString().ToInt(); } public static float GetValueAsFloat(this CustomProperty property) { - return property.m_Value.ToFloat(); + return property.m_JObject["value"]!.ToString().ToFloat(); } public static bool GetValueAsBool(this CustomProperty property) { - return property.m_Value.ToBool(); + return property.m_JObject["value"]!.ToString().ToBool(); } public static T GetValueAsEnum(this CustomProperty property) { - return property.m_Value.ToEnum(); + return property.m_JObject["value"]!.ToString().ToEnum(); } } } From cbee16e5f37c8289bd32a2b85b9eed642fe95fa4 Mon Sep 17 00:00:00 2001 From: Huisama Date: Sun, 16 Oct 2022 18:57:01 +0800 Subject: [PATCH 2/4] [fix] fix export bug --- .../Examples/Overhead/Maps/TestOverhead.tmx | 2 +- .../Overhead/Maps/propertytypes.xml.meta | 7 ++++ .../Scripts/Editor/Config/ST2U Settings.asset | 34 +++++++++++++++++-- .../Editor/Loaders/CustomPropertyLoader.cs | 11 +++--- 4 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml.meta diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/TestOverhead.tmx b/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/TestOverhead.tmx index ed12225c..4a9614b7 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/TestOverhead.tmx +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/TestOverhead.tmx @@ -86,7 +86,7 @@ - + diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml.meta b/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml.meta new file mode 100644 index 00000000..6614abc5 --- /dev/null +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 3f5cc66e901a6884e889e72b17751b14 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Config/ST2U Settings.asset b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Config/ST2U Settings.asset index 6d7b2a1c..818aabd6 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Config/ST2U Settings.asset +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Config/ST2U Settings.asset @@ -17,7 +17,7 @@ MonoBehaviour: m_AnimationFramerate: 20 m_DefaultMaterial: {fileID: 0} m_MaterialMatchings: [] - m_ObjectTypesXml: {fileID: 0} + m_ObjectTypesXml: {fileID: 4900000, guid: 3f5cc66e901a6884e889e72b17751b14, type: 3} m_ParseXmlError: m_CollisionGeometryType: 1 m_LayerColors: @@ -53,5 +53,35 @@ MonoBehaviour: - {r: 1, g: 0.64705884, b: 0, a: 1} - {r: 0, g: 1, b: 1, a: 1} - {r: 0.7372549, g: 0.56078434, b: 0.56078434, a: 1} - m_CustomObjectTypes: [] + m_CustomObjectTypes: + - m_Name: Script + m_Color: {r: 0.6666667, g: 0, b: 0, a: 1} + m_CustomProperties: + - m_Name: CorrespondType + m_Type: int + m_Value: + - m_Name: ScriptFile + m_Type: file + m_Value: + - m_Name: TestClass + m_Color: {r: 0.627451, g: 0.627451, b: 0.6431373, a: 1} + m_CustomProperties: + - m_Name: Field1 + m_Type: int + m_Value: + - m_Name: Field2 + m_Type: int + m_Value: + - m_Name: Field3 + m_Type: string + m_Value: + - m_Name: SubClass + m_Type: class + m_Value: + - m_Name: TestSubClass + m_Color: {r: 0.627451, g: 0.627451, b: 0.6431373, a: 1} + m_CustomProperties: + - m_Name: Value + m_Type: string + m_Value: m_PrefabReplacements: [] diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs index a4861def..5fdb2419 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs @@ -88,13 +88,10 @@ private static JObject ParseXmlDescPropertiesToJObjectWithExpansion( foreach (var xSubProperty in properties.Elements("property")) { var name = xSubProperty.GetAttributeAs("name"); - var isSubCustomClass = xSubProperty.GetAttributeAs("type") == "class"; - var subRealType = - isSubCustomClass - ? xSubProperty.GetAttributeAs("propertytype", "") - : xSubProperty.GetAttributeAs("type"); - - jObj[name] = ParseXmlDescPropertiesToJObjectWithExpansion( + var isSubCustomClass = jObj[name]!["type"]!.ToString() == "class"; + var subRealType = jObj[name]!["realType"]!.ToString(); + + jObj[name] = ParseXmlDescPropertiesToJObjectWithExpansion( xSubProperty, context, isSubCustomClass, subRealType); } } From f665d2552df266a10ca4e25bc74897fec492d81b Mon Sep 17 00:00:00 2001 From: Hui Date: Sun, 16 Oct 2022 19:02:00 +0800 Subject: [PATCH 3/4] [chore] remove unused meta file --- .../Examples/Overhead/Maps/propertytypes.xml.meta | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml.meta diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml.meta b/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml.meta deleted file mode 100644 index 6614abc5..00000000 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Examples/Overhead/Maps/propertytypes.xml.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 3f5cc66e901a6884e889e72b17751b14 -TextScriptImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: From ea52025a1e92dfe8b1db6bed1064cdf13c5372af Mon Sep 17 00:00:00 2001 From: Huisama Date: Sun, 16 Oct 2022 19:09:44 +0800 Subject: [PATCH 4/4] [chore] fix intent format --- .../Scripts/Editor/Loaders/CustomPropertyLoader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs index 5fdb2419..776cc805 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Loaders/CustomPropertyLoader.cs @@ -91,7 +91,7 @@ private static JObject ParseXmlDescPropertiesToJObjectWithExpansion( var isSubCustomClass = jObj[name]!["type"]!.ToString() == "class"; var subRealType = jObj[name]!["realType"]!.ToString(); - jObj[name] = ParseXmlDescPropertiesToJObjectWithExpansion( + jObj[name] = ParseXmlDescPropertiesToJObjectWithExpansion( xSubProperty, context, isSubCustomClass, subRealType); } }