CompositeToggle & StyleSystem are components that manage snapshots of properties.
They supports every properties including Image's Sprite, Graphic's Color, Text's text, GameObject's SetActive, etc...
Set values to properties you need, at same time, without other script.
- Unity5.3+
- No other SDK
- Download CompositeToggle.unitypackage and install to your project.
- AddComponent
CompositeToggle
orStyle
to the GameObject. - Enjoy!
In Unity, how to control Images and Text components at the same time?
How to control the properties, such as sprite, text, color or fontSize?
It's common case in game development.
Do you use UnityEvent? Unfortunately, structures such as Color are not supported.
So, we need a script as following to control the properties.
The script controls 'state (On / Off)' and 'property (sprite etc.)'.
Therefore, as the state or properties increase, we need to add a script.
Considering work bottleneck and maintainability, it is not good.
[SerializeField] Sprite sptireOn;
[SerializeField] Sprite sptireOff;
[SerializeField] Color colorOn;
[SerializeField] Color colorOff;
[SerializeField] int fontSizeOn;
[SerializeField] int fontSizeOff;
void SwitchOn()
{
GetComponent<Image>().sprite = sptireOn;
GetComponent<Image>().color = colorOn;
GetComponent<Text>().fontSize = fontSizeOn;
}
void SwitchOff()
{
GetComponent<Image>().sprite = sptireOff;
GetComponent<Image>().color = colorOff;
GetComponent<Text>().fontSize = fontSizeOff;
}
...
CompositeToggle implemented to separate 'state' and 'property'.
The state is defined by a script, and the property is defined by a scene or a prefab.
The script should only turn the toggle on or off. Yes, it is very simple.
[SerializeField] CompositeToggle toggle;
void SetToggleValue(bool isOn)
{
toggle.booleanValue = isOn;
}
CompositeToggle manage snapshots of properties.
Set values to properties you need, at same time, without other script.
StyleSystem collectively manages the properties of Component and separates layout and design like CSS.
A style have some properties you need, and can be referred to by multiple GameObjects.
When the properties of the style are changed, they are immediately applied to the referencing GameObjects.
In addition, styles can apply at runtime :)
CompositeToggle internally uses reflection.
Reflection is slow? -Yes, that's right.
You can avoid reflection by baking the property to the script.
Bake the properties from the Style or StyleAsset inspector.
- Fixed: InspectorGUI issue.
- Fixed: Demo scene.
- Feature: Supports every properties of all components.
- Feature: Set values to properties you need, at same time, without other script.
- Feature: Bake properties to script to improve invocation performance.
- Feature: (CompositeToggle) 4 kind of value type.
- Boolean
- Index
- Count
- Flag
- Feature: (CompositeToggle) Synchronization mode
- Parent-child relationships
- Grouping
- Sync-Other
- Feature: (CompositeToggle) Some features for every toggles.
- Comment
- GameObject activation
- UnityEvent
- Feature: (CompositeToggle) Auto grouping construction based on hierarchy children.
- Feature: (StyleSystem) Style inheritance.