From a41ddf85b37fa35f6d9eb910898c296f7cdfbb66 Mon Sep 17 00:00:00 2001 From: Kulltero <33698270+Kulltero@users.noreply.github.com> Date: Sun, 5 Oct 2025 05:27:42 +0200 Subject: [PATCH] Allow Button to have any graphic component Unity's Button Component already supports having the target graphic be of any Graphic type, but the handling of the old json api limited us to only images and only with the limited part of the image api that was explicitly defined This change allows Using Images with the newer API aswell as RawImages and Text Component, The old API part is maintained specifically for images but does not make an effort to include new fields or allow applying changes to other types to drive developers to use the dedicated methods for creating their Graphics --- CommunityEntity.UI.cs | 73 ++++++++------- Tests/ButtonGraphicTest.json | 171 +++++++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+), 33 deletions(-) create mode 100644 Tests/ButtonGraphicTest.json diff --git a/CommunityEntity.UI.cs b/CommunityEntity.UI.cs index b49933c..4ba96c0 100644 --- a/CommunityEntity.UI.cs +++ b/CommunityEntity.UI.cs @@ -331,40 +331,47 @@ T GetOrAddComponent() where T : Component } // bg image - var img = GetOrAddComponent(); - if ( ShouldUpdateField( "sprite" ) ) - img.sprite = FileSystem.Load( obj.GetString( "sprite", "Assets/Content/UI/UI.Background.Tile.psd" ) ); - if ( ShouldUpdateField( "material" ) ) - img.material = FileSystem.Load( obj.GetString( "material", "Assets/Icons/IconMaterial.mat" ) ); - if ( ShouldUpdateField( "color" ) ) - img.color = ColorEx.Parse( obj.GetString( "color", "1.0 1.0 1.0 1.0" ) ); - if ( ShouldUpdateField( "imagetype" ) ) - img.type = ParseEnum( obj.GetString( "imagetype", "Simple" ), UnityEngine.UI.Image.Type.Simple ); + var graphic = go.GetComponent(); + if (graphic == null) + graphic = go.AddComponent(); - 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 ); + // backwards compatability + if (graphic is UnityEngine.UI.Image img) + { + if ( ShouldUpdateField( "sprite" ) ) + img.sprite = FileSystem.Load( obj.GetString( "sprite", "Assets/Content/UI/UI.Background.Tile.psd" ) ); + if ( ShouldUpdateField( "material" ) ) + img.material = FileSystem.Load( obj.GetString( "material", "Assets/Icons/IconMaterial.mat" ) ); + if ( ShouldUpdateField( "color" ) ) + img.color = ColorEx.Parse( obj.GetString( "color", "1.0 1.0 1.0 1.0" ) ); + if ( ShouldUpdateField( "imagetype" ) ) + img.type = ParseEnum( obj.GetString( "imagetype", "Simple" ), UnityEngine.UI.Image.Type.Simple ); + } + + c.targetGraphic = graphic; + + // 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( graphic, obj ); break; } diff --git a/Tests/ButtonGraphicTest.json b/Tests/ButtonGraphicTest.json new file mode 100644 index 0000000..9a9b9fd --- /dev/null +++ b/Tests/ButtonGraphicTest.json @@ -0,0 +1,171 @@ +[ + { + "name": "UI", + "parent":"Overlay", + "components": + [ + { + "type":"UnityEngine.UI.RawImage", + "color": "0.04 0.04 0.04 1.0" + }, + { + "type":"RectTransform", + "anchormin": "0 0", + "anchormax": "1 1" + }, + { + "type":"NeedsCursor" + } + ] + }, + { + "name": "OldButton", + "parent":"UI", + "components": + [ + { + "type":"UnityEngine.UI.Button", + "color": "0.7 0.6 0.3 0.8" + }, + { + "type":"RectTransform", + "anchormin": "0.5 0.8", + "anchormax": "0.5 0.8", + "offsetmin": "-200 -48", + "offsetmax": "200 0" + } + ] + }, + { + "parent": "OldButton", + "components": + [ + { + "type":"UnityEngine.UI.Text", + "text":"Old Button", + "fontSize":16, + "align": "MiddleCenter" + } + ] + }, + { + "name": "NewImageButton", + "parent":"UI", + "components": + [ + { + "type":"UnityEngine.UI.Image", + "sprite": "assets/content/ui/ui.box.tga", + "color": "0.7 0.6 0.3 0.8" + }, + { + "type":"UnityEngine.UI.Button" + }, + { + "type":"RectTransform", + "anchormin": "0.5 0.8", + "anchormax": "0.5 0.8", + "offsetmin": "-200 -100", + "offsetmax": "200 -52" + } + ] + }, + { + "parent": "NewImageButton", + "components": + [ + { + "type":"UnityEngine.UI.Text", + "text":"New Image Button", + "fontSize":16, + "align": "MiddleCenter" + } + ] + }, + { + "name": "NewRawImageButton", + "parent":"UI", + "components": + [ + { + "type":"UnityEngine.UI.RawImage", + "sprite": "assets/content/ui/ui.box.tga", + "color": "0.7 0.6 0.3 0.8" + }, + { + "type":"UnityEngine.UI.Button" + }, + { + "type":"RectTransform", + "anchormin": "0.5 0.8", + "anchormax": "0.5 0.8", + "offsetmin": "-200 -152", + "offsetmax": "200 -104" + } + ] + }, + { + "parent": "NewRawImageButton", + "components": + [ + { + "type":"UnityEngine.UI.Text", + "text":"New Raw Image Button", + "fontSize":16, + "align": "MiddleCenter" + } + ] + }, + { + "name": "NewRawImageButton", + "parent":"UI", + "components": + [ + { + "type":"UnityEngine.UI.Text", + "text":"New Raw Image Button", + "fontSize":16, + "align": "MiddleCenter" + }, + { + "type":"UnityEngine.UI.Button" + }, + { + "type":"RectTransform", + "anchormin": "0.5 0.8", + "anchormax": "0.5 0.8", + "offsetmin": "-200 -204", + "offsetmax": "200 -156" + } + ] + }, + { + "name": "Button88", + "parent": "UI", + "components": + [ + { + "type":"UnityEngine.UI.Button", + "close":"UI", + "color": "0.9 0.8 0.3 0.8" + }, + { + "type":"RectTransform", + "anchormin": "0 0", + "anchormax": "0.03 0.04" + } + ] + }, + { + "parent": "Button88", + "components": + [ + { + "type":"UnityEngine.UI.Text", + "text":"x", + "fontSize":18, + "align": "MiddleCenter" + } + ] + } +] \ No newline at end of file