Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 40 additions & 33 deletions CommunityEntity.UI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,40 +331,47 @@ T GetOrAddComponent<T>() where T : Component
}

// bg image
var img = GetOrAddComponent<UnityEngine.UI.Image>();
if ( ShouldUpdateField( "sprite" ) )
img.sprite = FileSystem.Load<Sprite>( obj.GetString( "sprite", "Assets/Content/UI/UI.Background.Tile.psd" ) );
if ( ShouldUpdateField( "material" ) )
img.material = FileSystem.Load<Material>( 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<UnityEngine.UI.Graphic>();
if (graphic == null)
graphic = go.AddComponent<UnityEngine.UI.Image>();

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<Sprite>( obj.GetString( "sprite", "Assets/Content/UI/UI.Background.Tile.psd" ) );
if ( ShouldUpdateField( "material" ) )
img.material = FileSystem.Load<Material>( 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;
}
Expand Down
171 changes: 171 additions & 0 deletions Tests/ButtonGraphicTest.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
]