INPUT:
We need to show 100 instances of a prefab on a mobile screen.
Prefab consists of (just for example, the real one is a complex UI prefab):
- Label
- Sprite1
- Sprite2
- Sprite3
Each prefab instance can have 3 base-states:
- Label {color: red}; Sprite1 {enabled}; Sprite2 {disabled}; Sprite3 {disabled};
- Label {color: blue}; Sprite1 {disabled}; Sprite2 {enabled}; Sprite3 {disabled};
- Label {color: green}; Sprite1 {disabled}; Sprite2 {disabled}; Sprite3 {enabled};
Also there is “sub-states” managed by Animator and AnimationClip. This states applicable to all base-states of original prefab because it only animates the process of Collapsing/Expanding of UI Prefab.
- Collapsed - Label Only
- Expanded - Lablel and Sprite
WHAT WE NEED
Because we have 3 base-states we have to create 3 pools with 100 prefab istances in each. Each pool instantiates own prefab wich represent a base-state but inside they are pretty all same.
As you see there is a vast optimisation possibility.
We can describe each base-state by code and apply it so we whould have only one pool of 100 prefab instances but it’s not handy for production pipeline.
We need to be able to serialize current prefab state and save it in any format and then apply it in runtime.
So the workflow of UI programmer and designer should be:
- Compose Prefab (create all elements)
- Add VisualStateManager component to prefabs’ root game object
- Set color to label
- Hide unused sprites
- Leave used sprites enabled
- Click “Save as new state” button in a VisualStateManager inspector.
- Enter style name
- GO TO 3. repeat needed number of times.
Later at runtime programmer will just have to do something like:
Animator animator = GetComponent<Animator>();
VisualStateManager VSM = GetComponent<VisualStateManager>();
VSM.SetState("Collapsed");
if(_currentMapZoomLevel > 0.3) {
animator.Play("CollapsedState")
}else {
animator.Play("ExpandedState")
}
WHAT WE ALREADY TRIED
We tried to create a second layer for animator and 1-frame AnimationClip which could set all needed values in a first frame but mechanim needs avatar so layer could work… This could be a workaround
if only there were a way to make layers work without avatars.