From cccd842d956d7b0c69d37a2ce0b885996581fc85 Mon Sep 17 00:00:00 2001 From: 0xF Date: Thu, 2 Oct 2025 20:08:37 +0200 Subject: [PATCH 1/2] Pivot for RectTransform component With the introduction of ContentSizeFitter, there is now a need for pivot control, as ContentSizeFitter shifts the position dynamically in a way that is not expected. --- CommunityEntity.UI.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CommunityEntity.UI.cs b/CommunityEntity.UI.cs index b49933c..753f6a5 100644 --- a/CommunityEntity.UI.cs +++ b/CommunityEntity.UI.cs @@ -470,6 +470,8 @@ T GetOrAddComponent() where T : Component rt.offsetMin = Vector2Ex.Parse( obj.GetString( "offsetmin", "0.0 0.0" ) ); if ( ShouldUpdateField( "offsetmax" ) ) rt.offsetMax = Vector2Ex.Parse( obj.GetString( "offsetmax", "1.0 1.0" ) ); + if ( ShouldUpdateField( "pivot" ) ) + rt.pivot = Vector2Ex.Parse( obj.GetString( "pivot", "0.5 0.5" ) ); if ( ShouldUpdateField( "rotation" ) ) rt.rotation = Quaternion.Euler( 0, 0, obj.GetFloat("rotation", 0) ); From 545e1c954757be5a5691ef7eced8aa635af0486a Mon Sep 17 00:00:00 2001 From: 0xF <140760029+codefling-0xf@users.noreply.github.com> Date: Fri, 2 Jan 2026 02:24:37 +0100 Subject: [PATCH 2/2] Fixed the order for setting the pivot. Setting offsets before setting the pivot causes a shift, as the offsets were set for the previous pivot (0.5 0.5 by default). --- CommunityEntity.UI.cs | 189 +++++++++++++++++++++--------------------- 1 file changed, 95 insertions(+), 94 deletions(-) diff --git a/CommunityEntity.UI.cs b/CommunityEntity.UI.cs index 753f6a5..172912b 100644 --- a/CommunityEntity.UI.cs +++ b/CommunityEntity.UI.cs @@ -30,12 +30,12 @@ public static void DestroyServerCreatedUI() UiDict.Clear(); requestingTextureImages.Clear(); ScrollViews.Clear(); - UnloadTextureCache(); - - // Cleanup any custom vitals we created - if (NoticeArea.Instance != null) - { - NoticeArea.Instance.DeleteAllCustomVitals(); + UnloadTextureCache(); + + // Cleanup any custom vitals we created + if (NoticeArea.Instance != null) + { + NoticeArea.Instance.DeleteAllCustomVitals(); } } @@ -165,12 +165,12 @@ private void CreateComponents( GameObject go, JSON.Object obj, bool allowUpdate bool ShouldUpdateField( string fieldName ) { return !allowUpdate || obj.ContainsKey( fieldName ); - } - - // Only checks field name (if we want to keep default value) - bool HasField( string fieldName) - { - return obj.ContainsKey(fieldName); + } + + // Only checks field name (if we want to keep default value) + bool HasField( string fieldName) + { + return obj.ContainsKey(fieldName); } T GetOrAddComponent() where T : Component @@ -341,29 +341,29 @@ T GetOrAddComponent() where T : Component if ( ShouldUpdateField( "imagetype" ) ) img.type = ParseEnum( obj.GetString( "imagetype", "Simple" ), UnityEngine.UI.Image.Type.Simple ); - c.image = img; - - // Modify the color of the button when hovered - // Have to grab colorBlock, modify then reassign - var colors = c.colors; - - if (HasField("normalColor")) - colors.normalColor = ColorEx.Parse(obj.GetString("normalColor", "1.0 1.0 1.0 1.0")); - if (HasField("highlightedColor")) - colors.highlightedColor = ColorEx.Parse(obj.GetString("highlightedColor", "1.0 1.0 1.0 1.0")); - if (HasField("pressedColor")) - colors.pressedColor = ColorEx.Parse(obj.GetString("pressedColor", "1.0 1.0 1.0 1.0")); - if (HasField("selectedColor")) - colors.selectedColor = ColorEx.Parse(obj.GetString("selectedColor", "1.0 1.0 1.0 1.0")); - if (HasField("disabledColor")) - colors.disabledColor = ColorEx.Parse(obj.GetString("disabledColor", "0.5 0.5 0.5 0.5")); - if (HasField("colorMultiplier")) - colors.colorMultiplier = obj.GetFloat("colorMultiplier", 1.0f); - if (HasField("fadeDuration")) - colors.fadeDuration = obj.GetFloat("fadeDuration", 0.1f); - - c.colors = colors; - + c.image = img; + + // Modify the color of the button when hovered + // Have to grab colorBlock, modify then reassign + var colors = c.colors; + + if (HasField("normalColor")) + colors.normalColor = ColorEx.Parse(obj.GetString("normalColor", "1.0 1.0 1.0 1.0")); + if (HasField("highlightedColor")) + colors.highlightedColor = ColorEx.Parse(obj.GetString("highlightedColor", "1.0 1.0 1.0 1.0")); + if (HasField("pressedColor")) + colors.pressedColor = ColorEx.Parse(obj.GetString("pressedColor", "1.0 1.0 1.0 1.0")); + if (HasField("selectedColor")) + colors.selectedColor = ColorEx.Parse(obj.GetString("selectedColor", "1.0 1.0 1.0 1.0")); + if (HasField("disabledColor")) + colors.disabledColor = ColorEx.Parse(obj.GetString("disabledColor", "0.5 0.5 0.5 0.5")); + if (HasField("colorMultiplier")) + colors.colorMultiplier = obj.GetFloat("colorMultiplier", 1.0f); + if (HasField("fadeDuration")) + colors.fadeDuration = obj.GetFloat("fadeDuration", 0.1f); + + c.colors = colors; + GraphicComponentCreated( img, obj ); break; @@ -416,11 +416,11 @@ T GetOrAddComponent() where T : Component c.text = obj.GetString( "text", "Text" ); if ( ShouldUpdateField( "readOnly" ) ) c.readOnly = obj.GetBoolean( "readOnly", false ); - - if (ShouldUpdateField("placeholderId")) - { - AssignInputFieldPlaceholder(c, obj.GetString("placeholderId")); - } + + if (ShouldUpdateField("placeholderId")) + { + AssignInputFieldPlaceholder(c, obj.GetString("placeholderId")); + } if ( obj.TryGetBoolean( "password", out var password ) ) { @@ -462,6 +462,8 @@ T GetOrAddComponent() where T : Component var rt = go.GetComponent(); if ( rt ) { + if ( ShouldUpdateField( "pivot" ) ) + rt.pivot = Vector2Ex.Parse( obj.GetString( "pivot", "0.5 0.5" ) ); if ( ShouldUpdateField( "anchormin" ) ) rt.anchorMin = Vector2Ex.Parse( obj.GetString( "anchormin", "0.0 0.0" ) ); if ( ShouldUpdateField( "anchormax" ) ) @@ -470,9 +472,7 @@ T GetOrAddComponent() where T : Component rt.offsetMin = Vector2Ex.Parse( obj.GetString( "offsetmin", "0.0 0.0" ) ); if ( ShouldUpdateField( "offsetmax" ) ) rt.offsetMax = Vector2Ex.Parse( obj.GetString( "offsetmax", "1.0 1.0" ) ); - if ( ShouldUpdateField( "pivot" ) ) - rt.pivot = Vector2Ex.Parse( obj.GetString( "pivot", "0.5 0.5" ) ); - if ( ShouldUpdateField( "rotation" ) ) + if ( ShouldUpdateField( "rotation" ) ) rt.rotation = Quaternion.Euler( 0, 0, obj.GetFloat("rotation", 0) ); @@ -764,7 +764,7 @@ T GetOrAddComponent() where T : Component if(ShouldUpdateField("decelerationRate")) scrollRect.decelerationRate = obj.GetFloat("decelerationRate", 0.135f); if(ShouldUpdateField("scrollSensitivity")) - scrollRect.scrollSensitivity = obj.GetFloat("scrollSensitivity", 1f); + scrollRect.scrollSensitivity = obj.GetFloat("scrollSensitivity", 1f); // add scrollbars if objects are present GameObject barGO; @@ -805,50 +805,50 @@ T GetOrAddComponent() where T : Component scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport; BuildScrollbar(scrollbar, scrollObj, true); - } - - // Add ability to set scroll progress - if (ShouldUpdateField("horizontalNormalizedPosition")) - scrollRect.horizontalNormalizedPosition = obj.GetFloat("horizontalNormalizedPosition", 0f); - - if (ShouldUpdateField("verticalNormalizedPosition")) - scrollRect.verticalNormalizedPosition = obj.GetFloat("verticalNormalizedPosition", 0f); + } + + // Add ability to set scroll progress + if (ShouldUpdateField("horizontalNormalizedPosition")) + scrollRect.horizontalNormalizedPosition = obj.GetFloat("horizontalNormalizedPosition", 0f); + + if (ShouldUpdateField("verticalNormalizedPosition")) + scrollRect.verticalNormalizedPosition = obj.GetFloat("verticalNormalizedPosition", 0f); break; } } - } - - private void AssignInputFieldPlaceholder(InputField input, string panelId) - { - // If clearing placeholder - if (string.IsNullOrEmpty(panelId)) - { - input.placeholder = null; - return; - } - - // Search for panel - var panel = FindPanel(panelId); - - if (panel == null) - { - Debug.LogWarning($"[AddUI] Unable to find placeholder panel '{panelId}' for InputField '{input.name}'"); - input.placeholder = null; - return; - } - - // Get graphic component - var graphic = panel.GetComponent(); - - if (graphic == null) - { - Debug.LogWarning($"[AddUI] Unable to find Graphic component on placeholder panel '{panelId}' for InputField '{input.name}'"); - input.placeholder = null; - return; - } - - // Assign if it's all good - input.placeholder = graphic; + } + + private void AssignInputFieldPlaceholder(InputField input, string panelId) + { + // If clearing placeholder + if (string.IsNullOrEmpty(panelId)) + { + input.placeholder = null; + return; + } + + // Search for panel + var panel = FindPanel(panelId); + + if (panel == null) + { + Debug.LogWarning($"[AddUI] Unable to find placeholder panel '{panelId}' for InputField '{input.name}'"); + input.placeholder = null; + return; + } + + // Get graphic component + var graphic = panel.GetComponent(); + + if (graphic == null) + { + Debug.LogWarning($"[AddUI] Unable to find Graphic component on placeholder panel '{panelId}' for InputField '{input.name}'"); + input.placeholder = null; + return; + } + + // Assign if it's all good + input.placeholder = graphic; } private void BuildScrollbar(Scrollbar scrollbar, JSON.Object obj, bool vertical){ @@ -953,15 +953,15 @@ private void GraphicComponentCreated( UnityEngine.UI.Graphic c, JSON.Object obj { c.canvasRenderer.SetAlpha( 0f ); c.CrossFadeAlpha( 1f, obj.GetFloat( "fadeIn", 0 ), true ); - } - - if (obj.ContainsKey("placeholderParentId")) - { - var panel = FindPanel(obj.GetString("placeholderParentId")); - if (panel != null && panel.TryGetComponent( out var input)) - { - input.placeholder = c; - } + } + + if (obj.ContainsKey("placeholderParentId")) + { + var panel = FindPanel(obj.GetString("placeholderParentId")); + if (panel != null && panel.TryGetComponent( out var input)) + { + input.placeholder = c; + } } } @@ -1041,3 +1041,4 @@ private void DestroyPanel( string pnlName ) } #endif +