From c74edaf78b8f1a6259c0f75aa7b3d811302f14ff Mon Sep 17 00:00:00 2001 From: Ville Nurmi Date: Wed, 13 Apr 2022 22:47:55 +0300 Subject: [PATCH 1/4] SuperCustomProperties and their Unity effects persist over object prefab replacement --- .../Scripts/Editor/Importers/TiledAssetImporter.cs | 5 +++++ .../Scripts/Editor/Importers/TmxAssetImporter.cs | 9 +++++++++ .../SuperTiled2Unity/Scripts/SuperCustomProperties.cs | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs index 9253f2a9..7eea0fb9 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs @@ -60,6 +60,11 @@ public void AddSuperCustomProperties(GameObject go, XElement xProperties, SuperT // Sort the properties alphabetically component.m_Properties = properties.OrderBy(p => p.m_Name).ToList(); + ApplyMagicSuperCustomProperties(component); + } + + public void ApplyMagicSuperCustomProperties(SuperCustomProperties component) + { AssignUnityTag(component); AssignUnityLayer(component); } diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs index 0abc240f..8143ea32 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs @@ -412,6 +412,15 @@ private void DoPrefabReplacements() // Keep the name from Tiled. instance.name = so.gameObject.name; + // Transfer and reapply custom property magic on the instantiated prefab. + var soSuperCustomProperties = so.gameObject.GetComponent(); + if (soSuperCustomProperties != null) + { + var instanceSuperCustomProperties = instance.AddComponent(); + instanceSuperCustomProperties.CopyFrom(soSuperCustomProperties); + ApplyMagicSuperCustomProperties(instanceSuperCustomProperties); + } + // Update bookkeeping for later custom property replacement. goToDestroy.Add(so.gameObject); objectsById[so.m_Id] = instance; diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/SuperCustomProperties.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/SuperCustomProperties.cs index b9ebdebe..076c83e5 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/SuperCustomProperties.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/SuperCustomProperties.cs @@ -21,5 +21,12 @@ bool PredicateRemoveCustomProperty(CustomProperty customProperty) { return name.Equals(customProperty.m_Name); } } + + public void CopyFrom(SuperCustomProperties other) + { + m_Properties.Clear(); + m_Properties.Capacity = other.m_Properties.Count; + m_Properties.AddRange(other.m_Properties); + } } } From 442f9dbd9fa8612ae8fa2a0d84481bc6532411af Mon Sep 17 00:00:00 2001 From: Ville Nurmi Date: Fri, 15 Apr 2022 22:36:33 +0300 Subject: [PATCH 2/4] Fixed null reference in previous commit --- .../Scripts/Editor/Importers/TmxAssetImporter.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs index 8143ea32..c5a77538 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs @@ -413,12 +413,13 @@ private void DoPrefabReplacements() instance.name = so.gameObject.name; // Transfer and reapply custom property magic on the instantiated prefab. - var soSuperCustomProperties = so.gameObject.GetComponent(); - if (soSuperCustomProperties != null) + var soComponent = so.gameObject.GetComponent(); + if (soComponent != null && !soComponent.m_Properties.IsEmpty()) { - var instanceSuperCustomProperties = instance.AddComponent(); - instanceSuperCustomProperties.CopyFrom(soSuperCustomProperties); - ApplyMagicSuperCustomProperties(instanceSuperCustomProperties); + var component = instance.AddComponent(); + component.m_Properties = new List(); + component.m_Properties.CombineFromSource(soComponent.m_Properties); + ApplyMagicSuperCustomProperties(component); } // Update bookkeeping for later custom property replacement. From 39bcd04ac48efce91660df05b727940840113a3d Mon Sep 17 00:00:00 2001 From: Ville Nurmi Date: Fri, 15 Apr 2022 22:44:06 +0300 Subject: [PATCH 3/4] Fixed unwanted layer inheritance for prefab replacements --- .../Scripts/Editor/Importers/TiledAssetImporter.cs | 11 +++-------- .../Scripts/Editor/Importers/TmxAssetImporter.cs | 3 ++- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs index 7eea0fb9..2c45a831 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TiledAssetImporter.cs @@ -60,13 +60,8 @@ public void AddSuperCustomProperties(GameObject go, XElement xProperties, SuperT // Sort the properties alphabetically component.m_Properties = properties.OrderBy(p => p.m_Name).ToList(); - ApplyMagicSuperCustomProperties(component); - } - - public void ApplyMagicSuperCustomProperties(SuperCustomProperties component) - { AssignUnityTag(component); - AssignUnityLayer(component); + AssignUnityLayer(component, defaultInheritFromParent: true); } public void AssignTilemapSorting(TilemapRenderer renderer) @@ -151,7 +146,7 @@ protected void AssignUnityTag(SuperCustomProperties properties) } } - protected void AssignUnityLayer(SuperCustomProperties properties) + protected void AssignUnityLayer(SuperCustomProperties properties, bool defaultInheritFromParent) { // Do we have a 'unity:layer' property? CustomProperty prop; @@ -168,7 +163,7 @@ protected void AssignUnityLayer(SuperCustomProperties properties) properties.gameObject.layer = LayerMask.NameToLayer(layer); } } - else + else if (defaultInheritFromParent) { // Inherit the layer of our parent var parent = properties.gameObject.transform.parent; diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs index c5a77538..2c4362b4 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Importers/TmxAssetImporter.cs @@ -419,7 +419,8 @@ private void DoPrefabReplacements() var component = instance.AddComponent(); component.m_Properties = new List(); component.m_Properties.CombineFromSource(soComponent.m_Properties); - ApplyMagicSuperCustomProperties(component); + AssignUnityTag(component); + AssignUnityLayer(component, defaultInheritFromParent: false); } // Update bookkeeping for later custom property replacement. From ac6867c0c1fe0879c18cb50ed7fd35aa213e1546 Mon Sep 17 00:00:00 2001 From: Ville Nurmi Date: Mon, 18 Apr 2022 11:32:56 +0300 Subject: [PATCH 4/4] Removed unnecessary addition of SuperCustomProperties.CopyFrom --- .../SuperTiled2Unity/Scripts/SuperCustomProperties.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/SuperCustomProperties.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/SuperCustomProperties.cs index 076c83e5..b9ebdebe 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/SuperCustomProperties.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/SuperCustomProperties.cs @@ -21,12 +21,5 @@ bool PredicateRemoveCustomProperty(CustomProperty customProperty) { return name.Equals(customProperty.m_Name); } } - - public void CopyFrom(SuperCustomProperties other) - { - m_Properties.Clear(); - m_Properties.Capacity = other.m_Properties.Count; - m_Properties.AddRange(other.m_Properties); - } } }